C#TCP IP连接
我用过这个。当我尝试连接两台不同的机器192.168.1.4和192.168.1.9时。我在服务器代码中更改了这一部分C#TCP IP连接,c#,tcp,client-server,C#,Tcp,Client Server,我用过这个。当我尝试连接两台不同的机器192.168.1.4和192.168.1.9时。我在服务器代码中更改了这一部分 TcpListener myList = new TcpListener(IPAddress.Any, 27505); 而不是 IPAddress ipAd = IPAddress.Parse("192.168.1.9"); TcpListener myList = new TcpListener(ipAd, 27505); 我在客户端中得到了这个错误 Unhandled
TcpListener myList = new TcpListener(IPAddress.Any, 27505);
而不是
IPAddress ipAd = IPAddress.Parse("192.168.1.9");
TcpListener myList = new TcpListener(ipAd, 27505);
我在客户端中得到了这个错误
Unhandled Exception: System.Net.Sockets.SocketException: A connection attempt fa
iled because the connected party did not properly respond after a period of time
, or established connection failed because connected host has failed to respond
192.168.1.4:8001
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at Client.Client.send() in C:\Users\John\Documents\Visual Studio 2010\Project
s\Server Client\Client\Client.cs:line 111
at Client.Client.Main(String[] args) in C:\Users\John\Documents\Visual Studio
2010\Projects\Server Client\Client\Client.cs:line 147
我试着在同一台机器上运行它们,结果成功了。
如何修复它?侦听器的地址必须是您自己的主机地址之一。TcpListener的第一个参数不是您希望连接到的地址 我的服务器代码(可用于使用我机器的IP或任意IP)(我没有在局域网上的两台机器上试用过):
试试看{
IPAddress ipAd=IPAddress.Parse(“192.168.0.100”);
//使用本地m/c IP地址,以及
//在客户端中使用相同的方法
/*初始化侦听器*/
//TcpListener myList=新的TcpListener(IPAddress.Any,8001);
TcpListener myList=新的TcpListener(ipAd,8001);
/*在指定的端口开始侦听*/
myList.Start();
WriteLine(“服务器正在端口8001上运行…”);
Console.WriteLine(“本地端点为:”+
myList.LocalEndpoint);
WriteLine(“等待连接…”);
套接字s=myList.AcceptSocket();
Console.WriteLine(“从“+s.RemoteEndPoint”接受连接);
字节[]b=新字节[100];
int k=s.Receive(b);
Console.WriteLine(“接收…”);
对于(int i=0;该错误已丢失。请进行修复,以便我们能够诊断该问题。这不是错误,只是错误附带的堆栈跟踪的一小部分。抱歉,David,我没有注意到,我已对其进行了编辑。我发现问题与我的windows防火墙有关,我不知道如何关闭该问题。非常感谢所有人。你的意思是服务器正在侦听错误的地址。服务器不应该侦听所有可能的地址吗?是的,这就是IPAddress。Any是。您指定了地址192.168.1.9,这是执行代码的计算机吗?192.168.1.9是我尝试连接到192.168.1.4(服务器)的客户端计算机。希望我回答了你的问题,我不太理解你的评论。我可能需要查看一些代码。我可能误解了这个问题。完整的代码在链接中。如果你想通过上述方式查看修改后的代码,我如何将其发送给你。
try {
IPAddress ipAd = IPAddress.Parse("192.168.0.100");
// use local m/c IP address, and
// use the same in the client
/* Initializes the Listener */
// TcpListener myList=new TcpListener(IPAddress.Any,8001);
TcpListener myList=new TcpListener(ipAd,8001);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint );
Console.WriteLine("Waiting for a connection.....");
Socket s=myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b=new byte[100];
int k=s.Receive(b);
Console.WriteLine("Recieved...");
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen=new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
try {
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("192.168.0.100",8001);
// use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
// String str=Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes("hello from client");
Console.WriteLine("Transmitting.....");
stm.Write(ba,0,ba.Length);
byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(bb[i]));
tcpclnt.Close();
}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}