简体   繁体   English

C#中的套接字通信-IP端口

[英]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)); 服务器程序套接字绑定为: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); 客户端程序(在另一台计算机上)连接为: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? 还是我将Server Ip错误地写入客户端? (I got it from cmd->ipconfig of server) (我从服务器的cmd-> ipconfig中得到的)

You are only binding to local 127.0.0.1 IP therefore your server would be accessible only from the same machine. 您仅绑定到本地127.0.0.1 IP,因此只能从同一台计算机访问您的服务器。 Try the following: 请尝试以下操作:

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

Because your server is binding to the localhost loop-back address 127.0.0.1. 因为您的服务器绑定到本地主机回送地址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 验证服务器与客户端在同一子网中的网络连接( 192.168.2.0192.168.0.0 )-将其称为“公共” IP地址
  • 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 将套接字绑定到服务器的“公共” IP地址或将套接字绑定到所有接口-通常使用特殊IP地址0.0.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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