使用 TCP Listener/Sockets 从另一台计算机连接到服务器

Connecting to Server on One Computer from another with TCP Listener/Sockets

我有一个简单的服务器,我可以在一台计算机上 运行 简单的聊天客户端可以连接到该服务器。当我 运行 本地服务器时,我也可以从本地客户端连接到它。问题是我似乎无法从另一台计算机上的客户端连接到服务器。

来自客户端的一些示例代码:

static string ipAddr = "<server local IP address>";
static Int32 port = 4296;
static System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient();
tcpClient.Connect(IPAddress.Parse(ipAddr), port);

远程客户端在最后一行发生异常。具体错误:

An unhandled exception of type 'System.Net.Sockets.SocketException occurred in System.dll

Additional information: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

服务器 正在以类似的方式在同一端口上创建连接,但在本地主机 IP 127.0.0.1 上,以供参考。

我已经尝试在我的路由器 (TCP) 上转发​​端口,并在我的防火墙中为 TCP 打开入站和出站端口(我知道这是不可持续的 - 只是试图让它在片刻)。

我非常有信心问题是远程客户端看不到服务器,但是在端口上执行 netstat 表明它在服务器 运行ning 时正在侦听,并且是 运行当本地客户端连接时 ning。通过Canyouseeme查看端口居然说端口没有打开,我是不是把端口转发错了?

有什么建议吗?非常感谢!

The server is creating a connection in a similar fashion, on the same port, but on the localhost IP 127.0.0.1, for reference.

服务器绑定到环回地址。因此,当您从与服务器相同的机器上的客户端 运行 连接时,连接成功。如果客户端在服务器机器的外部,则它无法连接到另一台机器上 127.0.0.1 上的服务器。

将服务器绑定到客户端可访问的有效 IP 地址。