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

时间:2018-09-09 22:27:36

标签: vb.net connection client-server tcp-ip

我正在尝试使用VB.NET中的TCP / IP和端口转发来创建基本的聊天服务器-客户端程序。该代码几乎完全来自Carlo De Silva的YouTube tutorials。我一直在连接两个客户端时遇到问题。当我在我的计算机上打开客户端并在另一台计算机上打开另一个客户端时,出现错误“连接尝试失败,因为连接的一方在一段时间后未正确响应,或者建立的连接失败,因为连接的主机无法响应[ip]:5757“

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

我已经在网站yougetsignal.com上检查了端口转发测试器,该端口说我的本地IP和外部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 = New TcpClient(extip, port) 'Friend client connects to external IP
_client = New TcpClient(ip, port) 'My client connects to local IP

1 个答案:

答案 0 :(得分:2)

您的问题在这里:

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

Amplify documentation指定要 接受来自 的连接的IP地址,这意味着它将接受来自 的连接/ em>该IP(在本例中为您的本地地址)。

要解决此问题,您必须在TcpListener(IPAddress, Int32) overload(相当于0.0.0.0)上监听连接,该连接指定它应接受来自 any < / em> IP地址。

_server = New TcpListener(IPAddress.Any, port)