百思不得其解,恳求大家帮忙
我用如下代码接收服务器端发过来的数据Private rsvstr As String
Public Sub ConneceAndRead()
ServerIPAddress = My.Settings.svrAddr
PORTNUM = My.Settings.port
Try
Client = New TcpClient(ServerIPAddress, PORTNUM)
' MsgBox(Client.GetStream.Length)
Client.GetStream.BeginRead(readbuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
Catch ex As Exception
MsgBox(ex.ToString & "," & "可能服务器已经关闭,或者网络故障,请检查后重新登录!")
Exit Sub
End Try
End Sub
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim message As String
Dim bytesread As Integer
bytesread = Client.GetStream.EndRead(ar)
message = System.Text.Encoding.UTF8.GetString(readbuffer)
If rsvstr = Nothing Then
rsvstr = message
Else
rsvstr = rsvstr & message ’问题所在
End If
If rsvstr.Contains(",,end") = True Then
ProcessCommand(rsvstr) '这里是自己写的处理过程
rsvstr = ""
End If
Array.Clear(readbuffer, 0, READ_BUFFER_SIZE)
Client.GetStream.BeginRead(readbuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
End Sub
当现在接收到的字符串和前面接收的连接的新串中,如果能存在“,,end”则说明一组数据已经接收完,可以处理。当没有遇到“,,end”,则现在接收的字符和前面接收到的合并,问题就出在这里。rsvstr = rsvstr & message执行后,rsvstr从编译器变量指示以及msgbox rsvstr可以看出message并没有并到rsvstr中,但rsvstr.contains(",,end")却返回true(此时message中是含",,end"的)。实在不懂,怎么会这样!
我没多少分给大家,不好意思,希望大侠们帮帮忙,很急! --------------------编程问答-------------------- 沙发,帮你顶 --------------------编程问答-------------------- message = System.Text.Encoding.UTF8.GetString(readbuffer)
改成
message = System.Text.Encoding.UTF8.GetString(readbuffer,0,bytesread) --------------------编程问答-------------------- 2楼的修改,应该是正解
补充:.NET技术 , VB.NET