TcpClient Receives Data when Debugging Not When Compiled

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a program that waits for in incoming TCP connection and receives a chunk of data, the forwards it on to another server with some modifications. While I am debugging the program within Visual Studio everything works as intended. When I build the program and run it, it accepts the inbound connection and thinks there is data available... but when it reads in the stream there is no data. I cannot identify the problem, since it works fine while debugging. I have tried this on multiple servers (both x86 and x64) and get the same result. I have compiled in both debug and release. Im using Visual Studio 2010.Dim tmpLastMessageID As String = Nothing
tcpsvr = New TcpListener(System.Net.IPAddress.Any, My.Settings.ListenPort)
Try
tcpsvr.Start()
Catch ex As Exception
Dim li As New ListViewItem(Now.ToString)
li.SubItems.Add(ex.Message)
lstHL7.Items.Add(li)
Exit Sub
End Try

While ExitThread = False
Try
Dim tcpclient As TcpClient = tcpsvr.AcceptTcpClient
If tcpclient.Connected Then
Dim stream As NetworkStream = tcpclient.GetStream

If stream.DataAvailable Then
receive message
Dim bytes(tcpclient.ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(tcpclient.ReceiveBufferSize))
Dim data As String = Encoding.ASCII.GetString(bytes)
Dim tmpString As String() = Split(data, "|")

add date stamp
Dim tmpDate As String = Mid(tmpString(6), 1, 8)
tmpString(13) = tmpString(13) & "||" & tmpDate

If tmpString(23) <> tmpLastMessageID Then
send to new location
tmpLastMessageID = tmpString(23)
data = Join(tmpString, "|")
Dim tmpMsg As String = data
Dim tcpSend As New TcpClient(My.Settings.SendToIPAddress, My.Settings.SendToPort)
Dim sendStream As NetworkStream = tcpSend.GetStream
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
sendStream.Write(msg, 0, msg.Length)



Do Until sendStream.DataAvailable
Threading.Thread.Sleep(10)
Loop

send ack back

Dim ACKbytes(tcpSend.ReceiveBufferSize) As Byte
sendStream.Read(ACKbytes, 0, CInt(tcpSend.ReceiveBufferSize))
data = Encoding.ASCII.GetString(ACKbytes)
data = data.ToUpper()
stream.Write(ACKbytes, 0, ACKbytes.Length)
sendStream.Close()
sendStream.Dispose()
tcpSend.Close()

log it
Dim li As New ListViewItem(Now.ToString)
li.SubItems.Add(tmpMsg)
tmpListofHL7.Add(li)
tmpMsg = Nothing
End If

stream.Close()
stream.Dispose()
tcpclient.Close()
tcpclient = Nothing

End If

End If

Catch ex As Exception
Dim li As New ListViewItem(Now.ToString)
li.SubItems.Add(ex.Message)
tmpListofHL7.Add(li)
End Try

End While
End Sub

** All programmers are playwrights, and all computers are lousy actors.

View the full article
 
Back
Top