I have a simple TCP client/server program on two laptops. The server should work fine as I use "netstat -anb" and the server shows up as listening, "", then my client connection failed, with "".

The port should match, and for server ip address I set it to "" so it should accept any connection.

When I run the program together on one laptop it works fine. I am having a hard time troubleshooting. I looked up a couple post, most of them suggested setting server ip to "" which i did. And "netstat -anb" shows it is listening.

client side code on connection

    private void btnConnect_Click(object sender, EventArgs e)
        client = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

        IPAddress ip = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
            client.Connect(new IPEndPoint(ip, port));
        catch(Exception ex) {
            MessageBox.Show("Connection failed");
        this.Text = "Connected";

Server Side

    private void btnListen_Click(object sender, EventArgs e)
        Invoke((MethodInvoker)delegate {
            statusLblOne.Text = "Listening..";

        // establish connection with socket.
        server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Bind(new IPEndPoint(0, port));

        // use <acceptStream> to accept data
        acceptStream = server.Accept();
        txtMessage.Enabled = true;

        // allocate a new thread for this acceptstream
        statusLblOne.Text = "Receiving data...";
        Thread th = new Thread(receiveData);

        // close the listener socket

I posted the code for connection at the original question. Really sort of confused right now, I searched a lot of post on stackoverflow for help, but most of them said either use a different port ( should work) or server is not listening(netstat show it is listening). How should I approach this? what more info can I provide to better diagnostic

  • Did you check to make sure you don't have any firewall software running? – Spiff Jul 15 '15 at 2:29
  • I have read that Windows will let you set up a listener on ports below 1024 but will refuse connections to it unless the owner of the listener is privileged. Can you try port 1020 and see if it works? Linux shouldn't even let you create the port. What OS is this? – Paul Jul 15 '15 at 2:37
  • try 1020 and failed. this is on windows using c#. Try 1030 failed too. – Shihao Zhang Jul 15 '15 at 3:21
  • i turned off my firewall software. It is still giving me that active refused message. – Shihao Zhang Jul 15 '15 at 3:23
  • If i run it on same machine it works fine – Shihao Zhang Jul 15 '15 at 3:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.