我有以下函数发送和接收数据,它适用于150字节之类的短字符串,但是字符串为2500字节会被卡住。 我试图用HTWin发送一些数据,没有任何东西得到网络的另一端。 所以另一端的接收器永远不会得到一个角色。 我也尝试使用旧的Hyperterminal同样的结果。
Private Function SendReport(ByVal Msg As String, ByVal Host As String, ByVal Port As String) As String
Try
Dim Tx As New TcpClient()
Dim stream As NetworkStream = Nothing
Dim CloseStream As Boolean = False
Dim LingerMode As New LingerOption(True, 5)
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
Tx.NoDelay = True
Tx.LingerState = LingerMode
Tx.SendTimeout = 10000
Tx.ReceiveTimeout = 10000
Tx.Connect(Host, Port)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Msg)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
stream = Tx.GetStream()
CloseStream = True
'stream.WriteTimeout = 100
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
'Tx.Connected
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
If CloseStream = True Then
stream.Close()
End If
Tx.Close()
Return responseData
Catch ex As Exception
WriteRTBLog(ex.Message, Color.Red)
WriteRTBLog(ex.StackTrace, Color.DarkRed)
Return "Error"
End Try
End Function
如果我将大字符串分成较小的字符串并反复调用此函数,则所有数据都会到达另一端。
另外,我需要获得一个很大的ACK数据包,代码必须等待并指出结束的特殊字符。 我正在考虑使用TcpListener创建一个Thread但是,是否可以在TcpClient的同一个端口中有一个TcpListener?
整个想法是发送一个由特殊字符分隔的大字符串,并用另一个特殊字符结束。
另一端接收数据,使用第一个特殊字符拆分字符串,并按拆分功能中的每个项发送一个ACK。 并且在最后一个ACK中发送第二个特殊字符以指示通信结束,因此我的应用程序可以正确处理接收到的数据。
我想我淹死在一杯水中。