Lou
Lou

Reputation: 2509

TCP Client program won't connect to my server

I'm trying to create a basic chat server-client program using TCP/IP and port forwarding in VB.NET. The code is derived almost entirely from Carlo De Silva's YouTube tutorials. I'm consistently having an issue connecting the two clients. When I open the client on my computer and another client on the other computer, I get the error "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 [ip]:5757"

There are three different programs: the server, the client, and the client on the other end (the friend client.) The server and the client both use my local IP, which is accessed programmatically, so it couldn't be due to a typo. The friend client uses my external IP, which I've checked as of today (2018-09-09) and it is correct. I've set up port-forwarding on my router using TCP&UDP with my local IP, which was different when I checked it today, but I've updated the rule and the problem persists. Everything is done over port 5757. The firewall isn't an issue - I tried turning it off on the other computer and the friend client still fails to connect.

I've checked the port-forwarding tester on the website yougetsignal.com, which says that port 5757 is closed on both my local and external IP. But at the time of writing, I've currently got open the server and two client programs (both of which use my local IP,) and I am able to successfully send messages between those two client programs. So if they are able to send messages between the server and back, I don't understand why the website says that the port is closed on my local IP.

Can anyone help me work out why the friend client is failing to connect?

Server code:

Module MainModule

Dim _server As TcpListener
Dim _listOfClients As New List(Of TcpClient)

Dim hostName As String = System.Net.Dns.GetHostName
Dim ip As String = System.Net.Dns.GetHostEntry(hostName).AddressList(0).ToString
Dim extip As String = "86.25.175.94"
Dim port As Integer = 5757

Sub Main()
    Console.Title = "SERVER"
    Try

        _server = New TcpListener(IPAddress.Parse(ip), port)
        _server.Start()

        Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
    Console.ReadLine()

End Sub

Private Sub NewClient(state As Object)
    Dim client As TcpClient = _server.AcceptTcpClient
    Try
        Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)
        _listOfClients.Add(client)

        Dim ns As NetworkStream = client.GetStream
        While True
            'Creates a buffer
            Dim toReceive(100000) As Byte

            Dim length As Integer = ns.Read(toReceive, 0, toReceive.Length)

            Dim text As String = Encoding.ASCII.GetString(toReceive, 0, length)

            For Each c As TcpClient In _listOfClients
                If c IsNot client Then 'Sends a message to every other client besides this one.
                    Dim nns As NetworkStream = c.GetStream 'New Network Stream
                    nns.Write(Encoding.ASCII.GetBytes(text), 0, text.Length)
                End If
            Next

            Console.WriteLine(text)
            Console.WriteLine()

            'Sends a received message receipt.
            Dim toSend() As Byte = Encoding.ASCII.GetBytes("Message Received...")

            ns.Write(toSend, 0, toSend.Length)


        End While

    Catch ex As Exception
        If _listOfClients.Contains(client) Then
            _listOfClients.Remove(client)
        End If
        Console.WriteLine(ex.Message)
    End Try
End Sub
End Module

Client code:

Module MainModule

Dim _client As TcpClient

Dim hostName As String = System.Net.Dns.GetHostName
Dim ip As String = System.Net.Dns.GetHostEntry(hostName).AddressList(0).ToString
Dim extip As String = "86.25.175.94"
Dim port As Integer = 5757

Sub Main()
    Console.Title = "Chat Client (Host)"
    Try
        'Gets the local ip address

        _client = New TcpClient(ip, port)

        'This thread listens for receiving messages from the server.
        Threading.ThreadPool.QueueUserWorkItem(AddressOf ReceiveMessages)

        While True
            'Starts a new stream
            Dim ns As NetworkStream = _client.GetStream()

            Dim message As String = Console.ReadLine()
            Dim toSend() As Byte = Encoding.ASCII.GetBytes(message)

            'Sends the message to the server
            ns.Write(toSend, 0, toSend.Length)


        End While
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
    Console.ReadLine()
End Sub

Private Sub ReceiveMessages(state As Object)
    Try
        While True
            'Starts a new network stream (receiving stream) to listen for any receiving messages.
            Dim rs As NetworkStream = _client.GetStream

            'Creates a buffer to receive text
            Dim toReceive(100000) As Byte

            'Reads anything coming in from the server.
            Dim length As Integer = rs.Read(toReceive, 0, toReceive.Length)

            'Converts the byte to text
            Dim text As String = Encoding.ASCII.GetString(toReceive, 0, length)

            Console.ForegroundColor = ConsoleColor.Green
            Console.WriteLine(text)
            Console.ResetColor()
            Console.WriteLine()
        End While

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
End Sub
End Module

The client and friend client are only different by one line of code:

_client = New TcpClient(extip, port) 'Friend client connects to external IP
_client = New TcpClient(ip, port) 'My client connects to local IP

Upvotes: 1

Views: 2714

Answers (1)

Visual Vincent
Visual Vincent

Reputation: 18310

Your issue is here:

_server = New TcpListener(IPAddress.Parse(ip), port)

The TcpListener(IPAddress, Int32) overload specifies which IP address to accept connections from, meaning it will accept connections from ONLY that IP (in this case your local address).

To fix this you've got to listen for connections at IPAddress.Any (equivalent to 0.0.0.0), which specifies that it should accept connections from any IP address.

_server = New TcpListener(IPAddress.Any, port)

Upvotes: 2

Related Questions