locked
TCPClient and TCPListener connection problem RRS feed

  • Question

  • Hey guys,

    I'll try and keep this short :

    public class Server
    {
            public Server()
            {
                server = new TcpListener(IPAddress.Parse("127.0.0.1"), 1862);
            }

            public void Start()
            {
                server.Start(1);
                ListenForConnections();
            }

            private void ListenForConnections()
            {
                client = server.AcceptTcpClient();
                InitialiseConversation();
            }
    }


    and :
    public class Client

    public bool Connect(string address)
            {
                try
                {
                   TCPClient client = new TCPClient();

    client.Connect(address, 1862);
                }
                catch
                {
                    return false;
                }

                netStream = messenger.GetStream();
                return true;
            }
    }


    Well, as you'd expect, i start the server, then the client.  This works 100% as expected when running the server AND the client locally.

    However, i sent the server to my brother to test it, but I can't connect to it.  I've checked the following so far :

    1)  Started the server on a remote pc.
    2)  'netstat -a' on remote PC shows as 'listening' on port 1862.
    3)  Client attempts to connect using remote pc's remote name.
    4)  'netstat -a' on client PC shows as 'SYN_SENT' to the correct IP Address (which has been translated from the name, therefore obviously the hostname was successfuly translated to ip).
    5.  Client fails to connect due to timeout/no response from server.

    I have forwarded port 1862 of the remote router to the PC of the server i am trying to connect to.

    Now I am at a loss as to what I should try next - any suggestions?  As i mentioned earlier, this works fine when server and client are run locally, but i fail to see what other obstacles could be blocking the connection now.

    Many thanks.




    Wednesday, February 27, 2008 10:58 PM

Answers

  • I think i've solved it.  For anyone that's interested :

    The problem was that i was using the IP Address "127.0.0.1" for the TCPListener.  Apparently, if you want the server to be accessible remotely, then you need to use the remote IP Address of the computer (eg. 192.168.0.3).  I have yet to look up the reason for why 127.0.0.1 won't work, but I imagine it's something to do with it being a local loopback address.

    So, in order to find my computer's remote ip address, I've written the following :

            public static IPAddress GetIPAddress()
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(Environment.MachineName);

                foreach (IPAddress address in hostEntry.AddressList)
                {
                    if (address.AddressFamily == AddressFamily.InterNetwork)
                        return address;
                }

                return null;
            }


    On reflection, i've realised that this code will not work if there are 2 or more connected adapters in the PC, but it'll do for the time being.

    Now, i call 'TCPListener server = new TCPListener(GetIPAddress(), 1862)'

    I also get the 'Windows Firewall' dialog asking me to unblock/block now, which is what i had expected it to do initially.

    Many thanks,
    Nick Moores
    Thursday, February 28, 2008 6:14 PM

All replies

  • So...any ideas?
    Thursday, February 28, 2008 2:26 PM
  • is your brother's machine behind a router? he might need to open port forwarding on this router to the machine he runs the server.

     Nick Moores wrote:
    So...any ideas?
    Thursday, February 28, 2008 2:31 PM
  • Yep, it is, but as I mentioned, i've forwarded port 1862 from the router to his machine.

    Could Windows Firewall be blocking it?  Netstat -a shows the port as listening, but does this mean that Windows Firewall has allowed it?

    Many thanks.
    Thursday, February 28, 2008 2:36 PM
  • I think i've solved it.  For anyone that's interested :

    The problem was that i was using the IP Address "127.0.0.1" for the TCPListener.  Apparently, if you want the server to be accessible remotely, then you need to use the remote IP Address of the computer (eg. 192.168.0.3).  I have yet to look up the reason for why 127.0.0.1 won't work, but I imagine it's something to do with it being a local loopback address.

    So, in order to find my computer's remote ip address, I've written the following :

            public static IPAddress GetIPAddress()
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(Environment.MachineName);

                foreach (IPAddress address in hostEntry.AddressList)
                {
                    if (address.AddressFamily == AddressFamily.InterNetwork)
                        return address;
                }

                return null;
            }


    On reflection, i've realised that this code will not work if there are 2 or more connected adapters in the PC, but it'll do for the time being.

    Now, i call 'TCPListener server = new TCPListener(GetIPAddress(), 1862)'

    I also get the 'Windows Firewall' dialog asking me to unblock/block now, which is what i had expected it to do initially.

    Many thanks,
    Nick Moores
    Thursday, February 28, 2008 6:14 PM
  • have you tried instead using TCPListener(IPAddress.Any, 1862).
    Wednesday, June 1, 2011 11:40 PM