我正在尝试从静态TCP端口号触发给定功能。当端口号接收到值= 1的数据包时,它将触发内部功能。
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
Dim IP As System.Net.IPAddress = System.Net.IPAddress.Parse(strIPAddress)
Dim PortNO As Integer = 5040
Dim MyData As String = ""
Dim LabView_TriggerValue As Integer = 1
Try
Dim client As TcpClient = New TcpClient(IP.ToString, PortNO)
Dim data As Byte() = New Byte(9998) {}
Dim responseData As String = String.Empty
Dim stream As NetworkStream = client.GetStream()
stream.ReadTimeout = 1000
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
MyData = responseData.Replace(vbCrLf, "")
MessageBox.Show(responseData)
'Return MyData
Do
Loop Until responseData = LabView_TriggerValue
stream.Close()
client.Close()
Catch ex As Exception
Label1.Text = "" & ex.Message
End Try
我遇到了一个问题,我的Do ... Loop,直到调出不循环。不管端口5040上的值如何,代码都将运行。理想情况下,它应保持循环。刷新端口5040,直到接收到整数值为1的数据包为止。