【发布时间】:2011-03-28 22:20:38
【问题描述】:
您能否解释一下为什么“输出”窗口不打印字符串的“xxxxx”部分? 看起来我对某事缺少一些基本的了解...?
我正在通过 TcpClient 发送字符串消息,并且在构建字符串时,我不会在发送方添加任何特殊字符,也不会在接收方添加任何特殊字符。这是问题的一部分吗?
http://i56.tinypic.com/9lg7pi.png
编辑:
我正在像这样在发送方构建我的字符串:
Private Sub SendData(ByVal stringArray As String())
SendData(GetMessageString(stringArray))
End Sub
Public Function GetMessageString(ByVal array As String()) As String
Dim str As String = ""
For i = 0 To array.Length - 1
str = str & array(i)
If i < array.Length - 1 Then
str = str & "|"
End If
Next
Return str
End Function
在接收端,变量被构建:
client.GetStream.BeginRead(readBodyBuffer, 0, MESSAGE_BODY_LENGTH, New AsyncCallback(AddressOf ReadBody), Nothing)
...
Private Sub ReadBody(ByVal aread As IAsyncResult)
BytesRead = client.GetStream.EndRead(aread)
...
' Read (add) into buffer
messagePart = Encoding.ASCII.GetString(readBodyBuffer)
messagePart = messagePart & "xxxxx"
编辑 3
我的简单错误是错误地使用了字节数组的 Redim: (参数 10 给出 11 个元素)
错误:
ReDim readBodyBuffer(MESSAGE_BODY_LENGTH)
正确:
ReDim readBodyBuffer(MESSAGE_BODY_LENGTH - 1)
【问题讨论】:
标签: .net vb.net string tcpclient