【发布时间】:2019-02-14 09:00:18
【问题描述】:
我正在尝试使用 TCP/IP 和 VB.NET 中的端口转发创建一个基本的聊天服务器-客户端程序。该代码几乎完全来自 Carlo De Silva 的YouTube tutorials。我一直在连接两个客户端时遇到问题。当我在我的计算机上打开客户端和另一台计算机上的另一个客户端时,我收到错误“连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应[ip]:5757"
有三个不同的程序:服务器端、客户端和另一端的客户端(朋友客户端)。服务器端和客户端都使用我的本地IP,这是通过编程方式访问的,所以不能由于错字。朋友客户端使用我的外部 IP,我今天 (2018-09-09) 已经检查过,它是正确的。我已经在我的路由器上使用 TCP&UDP 和我的本地 IP 设置了端口转发,这在我今天检查时有所不同,但我已经更新了规则并且问题仍然存在。一切都通过端口 5757 完成。防火墙不是问题 - 我尝试在另一台计算机上将其关闭,但朋友客户端仍然无法连接。
我检查了网站 yougetsignal.com 上的端口转发测试器,它说端口 5757 在我的本地和外部 IP 上都已关闭。但是在撰写本文时,我目前已经打开了服务器和两个客户端程序(它们都使用我的本地 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
【问题讨论】:
-
所以在您的本地网络上一切正常,但不能通过互联网?所以问题可能出在路由器上的端口转发,或中间的防火墙(您不知道)? (请注意,与从外部连接到它的人相比,当您从自己的网络内连接时,您的路由器可以很好地工作)
-
是的:需要明确的是,两个通过本地 IP 连接到服务器的程序可以互相聊天,如果我在自己的计算机上打开它们中的两个,而不是在另一个计算机.. 通过外部 IP 连接的计算机在我的计算机或另一台计算机上都不起作用。
标签: vb.net connection client-server tcp-ip