我在vb.net类中有一个TCP侦听器,用于侦听另一个程序发出的数据。问题在于它以比我的处理例程可以处理的速度更快的速度发送数据。该例程将在处理最后一条消息的过程中进行大约一半,然后在侦听端口上出现另一条消息,并且它开始处理而没有完成另一条消息。 我想添加一个消息缓冲区,以便将所有传入的文本字符串放入堆栈中,当我的处理例程完成后,它将把下一个字符串从堆栈中拉出并使用它,然后拉出下一个字符串,直到堆栈为空 我不确定执行此操作的最佳方法。我希望看到有人认为这是处理此问题的最佳方法。
TIA 里克
Private Sub TCPListening()
Dim CommandString As String = ""
Dim ParamLength As Short
Dim ADIFStr As String = ""
Dim tmpStr As String = ""
Dim tmpBool As Boolean
Dim paramStr As String = ""
Dim xcvrfreq As String = ""
Dim xcvrmode As String = ""
Dim prsvsplit As String = ""
Dim Arr() As String
Dim decimalstr As String = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
Dim thousandsstr As String = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator
If ExternalTcpListener Is Nothing Then
ExternalTcpListener = New TcpListener(IPAddress.Parse(ExternalTCPIP), ExternalTCPPort)
ExternalTcpListener.Start()
End If
Try
Dim responseString As String = ""
Dim sendBytes As [Byte]()
Dim ExternaltcpClient As TcpClient = ExternalTcpListener.AcceptTcpClient()
While True
Debug.Print("Start While")
Dim networkStream As NetworkStream = ExternaltcpClient.GetStream()
Dim bytes(ExternaltcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(ExternaltcpClient.ReceiveBufferSize))
Dim Sentdata As String = System.Text.Encoding.ASCII.GetString(bytes)
ttimer.start()
Sentdata = Replace(Sentdata, vbNullChar, "")
If Sentdata <> "" Then Debug.Print("SendData= " & Sentdata)
If InStrRev(Sentdata, "<command:") > 8 Then
'<command:10>CmdSetFreq<parameters:23><xcvrfreq:10> 7,185.000<command:10>CmdGetFreq<parameters:0>
'If multiple commands are recieved this section of the routine splits them up and processes each one as seperate commands.
Sentdata = Strings.Replace(Sentdata, "<command", "|<command")
Arr = Split(Sentdata, "|")
Debug.Print(" Arr=" & UBound(Arr))
For x = 1 To UBound(Arr)
Sentdata = Arr(x)
Debug.Print(" New " & Sentdata)
If InStr(Sentdata, "<command") Then
tmpStr = Sentdata
tmpStr = Right(Sentdata, Len(Sentdata) - 1)
CommandString = Extract(tmpStr, ">", "<")
ParamLength = Extract(Sentdata, "<parameters:", ">")
Select Case CommandString
Case "CmdSetFreqMode", "CmdQSXSplit", "CmdSetFreq", "CmdSetTXFreq", "CmdSetMode", "CmdSplit" ', "CmdTX", "CmdRX"
Station.ActiveWindow.SetExternalTCPCommandsInvoke(Sentdata)
'The above commands are processed and handled in EntryWindow.SetExternalTCPCommands
Case "CmdGetFreq"
'<command:10>CmdGetFreq<parameters:0>
Debug.Print("NI1Inside: " & CommandString & " " & Sentdata)
If ParamLength = 0 Then
tmpStr = Format(Val(Station.ActiveRadio.Frequency), "##,###.000")
responseString = "<CmdFreq:" & Len(tmpStr) & ">" & tmpStr
Debug.Print("[R1] " & responseString)
sendBytes = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'LogError("CmdGetFreq1Response - ElapsedTime: " & ttimer.ElapsedMilliseconds)
End If
Case "CmdGetTXFreq"
'<command:12>CmdGetTXFreq<parameters:0>
Debug.Print("NIInside: " & CommandString & " " & Sentdata)
If ParamLength = 0 Then
tmpStr = Format(Val(Station.ActiveRadio.TXFreq), "##,###.000")
If Len(tmpStr) = 0 Then
responseString = "<CmdTXFreq:4>.000"
Else
responseString = "<CmdTXFreq:" & Len(tmpStr) & ">" & tmpStr
End If
Debug.Print("[R1] " & responseString)
sendBytes = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'LogError("CmdGetTXFreq1Response - ElapsedTime: " & ttimer.ElapsedMilliseconds)
End If
end select
End If
Next
'End If
ElseIf InStr(Sentdata, "command") And InStrRev(Sentdata, "<command:") < 8 Then
'This section of the routine processes single commands that are sent via the Listener
tmpStr = Sentdata
tmpStr= Right(Sentdata, Len(Sentdata) - 1)
CommandString = Extract(tmpStr, ">", "<")
ParamLength = Extract(Sentdata, "parameters:", ">")
Select Case CommandString
Case "CmdSetFreqMode", "CmdQSXSplit", "CmdSetFreq", "CmdSetTXFreq", "CmdSetMode", "CmdSplit" ', "CmdTX", "CmdRX"
Station.ActiveWindow.SetExternalTCPCommandsInvoke(Sentdata)
'The above commands are processed and handled in EntryWindow.SetExternalTCPCommands
Case "CmdGetFreq"
'<command:10>CmdGetFreq<parameters:0>
Debug.Print("NI2Inside: " & CommandString & " " & Sentdata)
If ParamLength = 0 Then
tmpStr = Format(Val(Station.ActiveRadio.Frequency), "##,###.000")
responseString = "<CmdFreq:" & Len(tmpStr) & ">" & tmpStr
Debug.Print("[R] " & responseString)
sendBytes = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'LogError("CmdGetFreqResponse - ElapsedTime: " & ttimer.ElapsedMilliseconds)
End If
Case "CmdGetTXFreq"
'<command:12>CmdGetTXFreq<parameters:0>
Debug.Print("NI2Inside: " & CommandString & " " & Sentdata)
If ParamLength = 0 Then
tmpStr = Format(Val(Station.ActiveRadio.TXFreq), "##,###.000")
If Len(tmpStr) = 0 Then
responseString = "<CmdTXFreq:4>.000"
Else
responseString = "<CmdTXFreq:" & Len(tmpStr) & ">" & tmpStr
End If
Debug.Print("[R] " & responseString)
sendBytes = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'LogError("CmdGetTXFreqResponse - ElapsedTime: " & ttimer.ElapsedMilliseconds)
End If
缺少许多代码,但是它使您大致了解正在发生的事情。