出于某种原因,我无法将文本附加到我从tcpclient流中获取的数据。
Dim Client As New TcpClient("server", port)
Dim stream As NetworkStream = Client.GetStream()
Dim receiveBytes As [Byte]() = New [Byte](Client.ReceiveBufferSize) {}
Dim returnData As [String] = [String].Empty
stream.Read(receiveBytes, 0, CInt(Client.ReceiveBufferSize))
returnData = Encoding.ASCII.GetString(receiveBytes)
MsgBox(returnData + "hello")
消息框将只包含没有hello的returnData字符串。
答案 0 :(得分:1)
这是因为你忽略了Stream.Read
的返回值。您不会总是一次收到完整的缓冲区 - 并且其他任何内容都将为0.这意味着您将在{{1}的末尾使用Unicode“null”字符(0)并且Windows将这些视为字符串终止符。
将结尾前的两行更改为:
returnData
(那已经删除了之前的Dim bytesRead = stream.Read(receiveBytes, 0, CInt(Client.ReceiveBufferSize))
Dim returnData = Encoding.ASCII.GetString(receiveBytes, 0, bytesRead)
声明 - 当你要替换那个值时,声明一个变量并给它一个虚拟值没有意义。)
(请注意,这是假设数据 在ASCII中......是吗,肯定是吗?)