简体   繁体   中英

Socket Communication in C# - IP-Port

I am testing my socket programs at home, in local network.

Server and client programs are running on seperate machines.

Server program socket is binded as: serverSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8999));

Client program (on the other computer) is connected as: clientSocket.Connect(IPAddress.Parse("192.168.2.3"), 8999);

Why can Client not communicate with server? Do I need to make some firewall configuration or something like that? Or am I writing Server Ip incorrectly to the Client? (I got it from cmd->ipconfig of server)

You are only binding to local 127.0.0.1 IP therefore your server would be accessible only from the same machine. Try the following:

serverSocket.Bind(new IPEndPoint(IPAddress.Any), 8999));

Because your server is binding to the localhost loop-back address 127.0.0.1. This means nothing except what's running on the server can communicate with the socket.

You need to:

  • verify the server has a network connection on the same subnet as the client ( 192.168.2.0 or 192.168.0.0 ) - call it the "public" IP address
  • bind your socket to the server's "public" IP address or bind your socket to all interfaces - usually with the special IP address 0.0.0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM