更新时间:2023-01-17 10:29:45
您只需创建第二个客户端,然后通过第二个客户端发送数据。 我怀疑问题可能是由于网络或服务器问题造成的。 它是否第二次成功连接到服务器?
Below is a simple connect and send message function from the MSDN sample. I'm trying to send a message to 2 different servers.
Is there something else that needs to be done to release the local port from the first server message so that the 2nd server can receive a message?
My testing shows that the 2nd server is not getting the message. Before I start troubleshooting the server I just want someone with more experience with TCPClient to concure that the code should work fine and there isn't anything special for handling 2 server message from same application.
Shared Sub Connect(server As [String], message As [String]) Try ' Create a TcpClient. ' Note, for this client to work you need to have a TcpServer ' connected to the same address as specified by the server, port ' combination. Dim port As Int32 = 13000 Dim client As New TcpClient(server, port) ' Translate the passed message into ASCII and store it as a Byte array. Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message) ' Get a client stream for reading and writing. ' Stream stream = client.GetStream(); Dim stream As NetworkStream = client.GetStream() ' Send the message to the connected TcpServer. stream.Write(data, 0, data.Length) Console.WriteLine("Sent: {0}", message) ' Close everything. stream.Close() client.Close() Catch e As ArgumentNullException Console.WriteLine("ArgumentNullException: {0}", e) Catch e As SocketException Console.WriteLine("SocketException: {0}", e) End Try Console.WriteLine(ControlChars.Cr + " Press Enter to continue...") Console.Read() End Sub 'Connect
You just need to make a second client, and send the data via a second client. I suspect the problem is likely due to a network or server issue. Is it connecting successfully to the server the second time?