简体   繁体   English

客户端无法通过Internet连接到服务器

[英]Client does not connect to server over internet

I've set up a simple client/server system, but for some reason the client won't connect via the internet. 我已经建立了一个简单的客户端/服务器系统,但是由于某种原因,客户端无法通过Internet连接。 I've got them communicating on the same machine, using both the localhost address (127.0.0.1) and my LAN IP address (192.168.2.2). 我让他们使用本地主机地址(127.0.0.1)和我的LAN IP地址(192.168.2.2)在同一台计算机上进行通信。

I've also confirmed that the port is open using http://www.whatsmyip.org/ports/ , and when I use this site, the callback function is triggered on the server, so I assume the server is working properly. 我还确认使用http://www.whatsmyip.org/ports/来打开该端口,并且当我使用此站点时,在服务器上触发了回调函数,因此我认为服务器可以正常工作。

However, when I try to connect to the same (internet) IP address/port from my client, the server detects nothing and the client throws an exception at the OnConnect callback. 但是,当我尝试从客户端连接到相同的(互联网)IP地址/端口时,服务器未检测到任何内容,并且客户端在OnConnect回调中引发异常。 I've noticed that the RemoteEndPoint property of clientSocket is not being set correctly by the BeginConnect statement - it throws a SocketException (10057) when I look at it. 我注意到BeginConnect语句未正确设置clientSocket的RemoteEndPoint属性-当我查看它时,它抛出SocketException(10057)。 (When connecting via the LAN address, it works fine.) (通过LAN地址连接时,它可以正常工作。)

private void ConnectToServer()
{
    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPAddress ipAddress = IPAddress.Parse(txtIPAddress.Text);

    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(txtPort.Text));
    clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);
}

What could stop the client connecting to the server via the external IP address, given that it can connect via the LAN IP address and that the server is accepting connections over the internet? 考虑到客户端可以通过LAN IP地址连接并且服务器正在接受Internet上的连接,该如何阻止客户端通过外部IP地址连接到服务器?

I'm using Windows 7 and .NET 3.0. 我正在使用Windows 7和.NET 3.0。

Any advice gratefully appreciated. 任何建议表示感谢。 Thanks. 谢谢。

Firstly, you should not expect the socket to be connected right after you have called BeginConnect . 首先,您不应该期望在调用BeginConnect之后立即连接套接字。 It usually takes a few round-trips to establish a connection. 通常需要几次往返来建立连接。 So an error code of 10057 Not Connected is expected when asking about the remote endpoint before the connection has been established. 因此,在建立连接之前询问远程端点时,将出现错误代码10057 Not Connected

As to why your network set-up does not work. 至于为什么您的网络设置不起作用。 If I understand your set-up correctly, you are trying to send packets in the following manner: 如果我正确理解您的设置,则您尝试通过以下方式发送数据包:

Your machine (LAN) --> Port-forwarding device (WAN) --> Your machine (LAN)

Have you verified that the port-forwarding device supports this set-up? 您是否已验证端口转发设备支持此设置?

You state that you have verified the following: 您声明已验证以下内容:

Remote machine (WAN) --> Port-forwarding device (WAN) --> Your machine (LAN)

Your port-forwarding device may lack support for LAN -> WAN -> LAN port-forwarding. 您的端口转发设备可能不支持LAN -> WAN -> LAN端口转发。

You may also want to use Wireshark to see what packets are sent. 您可能还想使用Wireshark查看发送了什么数据包。

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

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