客户端无法通过互联网连接到服务器
我已经设置了一个简单的客户端/服务器系统,但由于某种原因客户端无法通过互联网连接。我让它们在同一台机器上进行通信,使用本地主机地址 (127.0.0.1) 和我的 LAN IP 地址 (192.168.2.2)。
我还使用 http://www.whatsmyip.org/ports/,当我使用这个网站时,回调函数会在服务器上触发,所以我假设服务器工作正常。
但是,当我尝试从客户端连接到相同的(互联网)IP 地址/端口时,服务器检测不到任何内容,并且客户端在 OnConnect 回调中引发异常。我注意到 BeginConnect 语句未正确设置 clientSocket 的 RemoteEndPoint 属性 - 当我查看它时,它会抛出 SocketException (10057)。 (当通过 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);
}
假设客户端可以通过 LAN IP 地址进行连接,并且服务器正在接受通过互联网的连接,那么什么可以阻止客户端通过外部 IP 地址连接到服务器呢?
我使用的是 Windows 7 和 .NET 3.0。
任何建议,不胜感激。谢谢。
I've set up a simple client/server system, but for some reason the client won't connect via the 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).
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.
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. 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. (When connecting via the LAN address, it works fine.)
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?
I'm using Windows 7 and .NET 3.0.
Any advice gratefully appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不应期望在调用
BeginConnect
后立即连接套接字。通常需要几次往返才能建立连接。因此,在建立连接之前询问远程端点时,预计会出现10057 Not Connected
错误代码。至于为什么您的网络设置不起作用。如果我正确理解您的设置,您正尝试以以下方式发送数据包:
您是否已验证端口转发设备支持此设置?
您声明您已验证以下内容:
您的端口转发设备可能不支持
LAN ->广域网-> LAN
端口转发。您可能还想使用 Wireshark 来查看发送了哪些数据包。
Firstly, you should not expect the socket to be connected right after you have called
BeginConnect
. It usually takes a few round-trips to establish a connection. So an error code of10057 Not Connected
is expected when asking about the remote endpoint before the connection has been established.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:
Have you verified that the port-forwarding device supports this set-up?
You state that you have verified the following:
Your port-forwarding device may lack support for
LAN -> WAN -> LAN
port-forwarding.You may also want to use Wireshark to see what packets are sent.