TCP 客户端程序无法连接到我的服务器

TCP Client program won't connect to my server

我正在尝试使用 TCP/IP 和 VB.NET 中的端口转发创建一个基本的聊天服务器-客户端程序。该代码几乎完全来自 Carlo De Silva 的 YouTube tutorials。我一直在连接两个客户端时遇到问题。当我在我的计算机上打开客户端并在另一台计算机上打开另一个客户端时,出现错误 "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"

有三个不同的程序:服务端,客户端,另一端的客户端(朋友客户端。)服务端和客户端都使用我的本地IP,通过编程方式访问,所以不能'不是因为打字错误。朋友客户端使用我的外部IP,我今天(2018-09-09)检查过它是正确的。我已经使用 TCP&UDP 和我的本地 IP 在我的路由器上设置了端口转发,这在我今天检查时有所不同,但我已经更新了规则并且问题仍然存在。一切都通过端口 5757 完成。防火墙不是问题 - 我尝试在另一台计算机上将其关闭,但朋友客户端仍然无法连接。

我检查了网站 yougetsignal.com 上的端口转发测试器,它说我的本地和外部 IP 上的端口 5757 都关闭了。但在撰写本文时,我目前已经打开了服务器和两个客户端程序(两者都使用我的本地 IP),并且我能够在这两个客户端程序之间成功发送消息。因此,如果他们能够在服务器和服务器之间发送消息,我不明白为什么该网站说我本地 IP 上的端口已关闭。

谁能帮我解决一下好友客户端连接失败的原因?

服务器代码:

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

客户代码:

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

client和friend client只差一行代码:

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

您的问题在这里:

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

TcpListener(IPAddress, Int32) overload指定哪个IP地址接受来自的连接,意思是它将接受来自[=23=的连接]只有那个IP(在本例中是你的本地地址)。

要解决此问题,您必须在 IPAddress.Any(相当于 0.0.0.0)监听连接,它指定它应该接受来自 的连接任何 IP地址。

_server = New TcpListener(IPAddress.Any, port)