vb.net - TCP 客户端未正确接收数据 VB.NET
问题描述
我正在尝试制作一个简单的客户端应用程序来接收小文本数据,对其进行比较,然后根据服务器发送的内容在客户端计算机上执行某些操作。
服务器逻辑:服务器端是用java制作的,所以不能在那里改变任何东西。服务器在连接到客户端时发送字符串“abc001”。
客户端逻辑:客户端从服务器接收字符串“abc001”并检查它收到的字符串是否与“abc001”相同,然后做相应的事情。
问题:当客户端收到数据时,我将其显示在 msgbox 中。但不仅仅是“abc001”,还会弹出一个额外的空白 msgbox(包括图像)。
客户代码 - 开始时:
Try
' declare vals
Dim ip As String = "127.0.0.1"
Dim port As Integer = 5000
' set client
_client = New TcpClient(ip, port)
' disable cross thread calls checking
CheckForIllegalCrossThreadCalls = False
' recieve msg
Threading.ThreadPool.QueueUserWorkItem(AddressOf RecieveMessages)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
客户代码 - 接收数据
Private Sub RecieveMessages(state As Object)
Try
While True
Dim ns As NetworkStream = _client.GetStream()
Dim toRecieve(_client.ReceiveBufferSize) As Byte
ns.Read(toRecieve, 0, CInt(_client.ReceiveBufferSize))
Dim txt As String = Encoding.ASCII.GetString(toRecieve)
MsgBox(txt)
End While
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
如何不获取空白消息框。即使进行比较,接收到的数据也不匹配参数。尝试使用延迟,尝试将缓冲区大小固定为 6 个字节但没有用。任何帮助表示赞赏。谢谢。
编辑1:尽我所能,但无法解决。尝试清理返回的字符串数据,甚至尝试将每个返回数据存储在数组中。看到堆栈,它说 msgbox 里面没有“任何东西”。它是空的..我什至不知道该怎么做..这是字符串清理的代码:
Private Sub RecieveMessages(state As Object)
Dim message(0) As String
Dim command_raw, command_clean, command As String
Dim counter As Integer = 0
Try
While True
Dim ns As NetworkStream = _client.GetStream()
Dim toRecieve(_client.ReceiveBufferSize) As Byte
ns.Read(toRecieve, 0, CInt(_client.ReceiveBufferSize))
Dim txt As String = Encoding.ASCII.GetString(toRecieve)
message(0) = txt
command_raw = message(0)
command_clean = command_raw.Replace(vbCrLf, Nothing)
command = command_clean.Substring(0, 6)
MsgBox(command)
End While
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
解决方案
推荐阅读
- r - 在R中对矩阵的行进行排序
- actions-on-google - 应用操作测试工具 v3.0.0:找不到带有操作文件参考的 Android 模块
- java - 单击另一个按钮后如何启用/禁用按钮?
- python - 向量化 numpy 多维数组。操作数组明智而不是元素明智
- c# - 使用在云中使用水平扩展的选项来实施 dotnet core web api 的最佳实践
- mysql - 选择表格中的最低日期
- python - 条件循环的问题
- django - 为什么我的动态表单集不显示任何字段
- ios - 在没有情节提要的 Objective-c 代码上创建一个表
- python - 为什么即使安装了 FBX 模块也找不到?