关于Socket和127.0.0.1的一个简单问题?
我的Socket服务端在"127.0.0.1"地址上监听,假设我的机器的地址是"192.168.1.200"
按以前的经验,客户端用"127.0.0.1"和“192.168.1.200"这两个地址中的任何一个地址去连服务端应该都可以的,但在C#中难道不行吗???(只能用"127.0.0.1"地址去连)
服务端:
System.Net.Sockets.Socket TcpServerSocket;
IPAddress TcpServerIPAddress;
TcpServerIPAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(TcpServerIPAddress, int.Parse("6000"));
TcpServerSocket = new System.Net.Sockets.Socket(/*iep.AddressFamily*/ System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
TcpServerSocket.Bind(iep);
TcpServerSocket.Listen(200);
System.Net.Sockets.Socket tempSocket = TcpServerSocket.Accept();
客户端:
Int32 port = 6000;
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("192.168.1.200", port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
System.Net.Sockets.NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
错误信息:由于目标机器积极拒绝,无法连接192.168.1.200:600
当IP地址改成“127.0.0.1”时,无此错误