开发者

Client does not connect to server over internet

开发者 https://www.devze.com 2023-03-31 16:08 出处:网络
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.

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.


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 of 10057 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:

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.

You may also want to use Wireshark to see what packets are sent.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号