开发者

Weird C# SocketException

While testing my Client-Server program, I encountered a weird exception when trying to connect to the server on a different router:

"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."

The client can connect to the server perfectly in the local network, however it doesn't work when it is over the internet.

I port forwarded port 1250 (the one I'm using), and using SimplePortForwarding (http://www.simpleportforwarding.com/) I verified that the port was open and working.

I based my implementation on this tutorial: http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

Any idea what is wrong? Thanks!

Here is the server listen method:

    public void startListening(int port)
    {
        lock(_locker)
        {
            _listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                // Bind socket to local endpoint, and listen for incoming connections
                IPEndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, port);
                _listeningSocket.Bind(ipEndpoint);
                _listeningSocket.Listen(10);

                waitForNewClient();

                // successfully started listening
                _isListening = true;

            } catch (SocketException e)
            {
                // failed for some strange reason
                _isListening = false;
            }
        }
    }

Here is the client connect code:

    public String connect(String ipAddress, int port)
    {
        lock(_locker)
        {
            if (!_connecting)
            {
                _socket = new Socket(AddressFamily.InterNetwo开发者_C百科rk, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);

                try
                {
                    _socket.Connect(endpoint);
                    _connected = true;
                    waitForData();

                    _eventManager.queueEvent(new PlayerJoinedEvent(PlayerJoinedEvent.PLAYER_JOINED, name));
                } catch (SocketException e)
                {
                    // Exception is thrown HERE
                    return e.Message;
                }
            }
        }

        return "";
    }


Make it sure that your Server IP address is public unless it is not reachable.

Check this link for private address spaces.


You opened port 1250 on the server or the client router? It needs to be opened on the server router. You may need to make sure your server is connected to your DMZ port and/or have DMZ enabled on your server router.

Hope this helps.


I Fixed the problem.

The IP Address I used was the internal ip address I got from ipconfig, but the IP address I needed to use was the external one, the one you get from services like http://www.whatsmyip.org/.

I'm still confused as to why these two numbers are different.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜