As per the code below, this app sends a message via TCP sockets and waits for a reply. It should timeout after 3 seconds (the interval set for tmrSend) or after the reply arrives.
The problem is that tmrSend.Start does not seem to be doing anything. The executing thread is tied up in this loop, and it becomes and endless loop if an acknowledgement is not sent, because the bElapsed variable is never set to true since the tmrSend_Tick event does not fire.
Code:Private Sub tmrSend_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrSend.Tick bElapsed = True End Sub Private Sub SendData(sMsg as string) Try Dim tcp As New TcpClient TCP.Connect(curLab.IPAddress, curLab.IPPort) TCP.NoDelay = True Dim stream As NetworkStream = TCP.GetStream() Dim data As [Byte]() 'Convert sMsg to byte array Data to send to stream data = StrToByteArray(sMsg) stream.Write(data, 0, data.Length) Thread.Sleep(0) If curLab.WaitforACKnowledgement Then tmrSend.Enabled = True tmrSend.Start() bElapsed = False sReply = "" Do While bElapsed = False Or stream.DataAvailable = True If bElapsed Then Exit Do If stream.DataAvailable Then Dim lenReply As Integer, iOffset As Integer, iSize As Integer iSize = 1024 Dim strReply(iSize) As Byte lenReply = 1 Do Until lenReply = 0 lenReply = stream.Read(strReply, iOffset, iSize) sReply = sReply & ByteArrayToString(strReply) If lenReply > iSize Then iOffset = iOffset + lenReply Else Exit Do End If Loop stream.Close() Exit Do End If Thread.Sleep(0) Loop End If tmrSend.Stop() stream.Close(500) tcp.Close() bDataSent = True end sub


Reply With Quote
