Hi, I am receiving a stream of data (a text message) through a TCP listener. It then sends back a stream. Then the first computer (the client) sends another stream of data. Then I send back a response message.

The first message is successfully received.
The first response is successfully sent.

Then it stops working. If I close the connection and re-open the connection, it works. Any ideas? The connection is staying open...

Dim BYTES(4096) As Byte
        Dim DATA_RECEIVED As String = Nothing
        Dim HL7_STRING_RECEIVED As String
        Dim SERVER_TCP_LISTENER As New TcpListener(6300)
        SERVER_TCP_LISTENER.Start()
        Dim HOSPITAL_HL7_CLIENT As TcpClient = SERVER_TCP_LISTENER.AcceptTcpClient()
        DATA_RECEIVED = Nothing
        Dim NETWORK_DATA_SREAM As NetworkStream = HOSPITAL_HL7_CLIENT.GetStream()

            Do While True

                If NETWORK_DATA_SREAM.CanRead Then
                    Dim TCP_READ_BUFFER(HOSPITAL_HL7_CLIENT.ReceiveBufferSize) As Byte
                    Dim HL7_MESSAGE_BUILDER As StringBuilder = New StringBuilder()
                    Dim NETWORK_BYTES_READ As Integer = 0
                    Do
                        NETWORK_BYTES_READ = NETWORK_DATA_SREAM.Read(TCP_READ_BUFFER, 0, TCP_READ_BUFFER.Length)
                        HL7_MESSAGE_BUILDER.AppendFormat("{0}", Encoding.ASCII.GetString(TCP_READ_BUFFER, 0, NETWORK_BYTES_READ))
                    Loop While NETWORK_DATA_SREAM.DataAvailable
                    HL7_STRING_RECEIVED = HL7_MESSAGE_BUILDER.ToString

                    If Len(HL7_STRING_RECEIVED) > 10 Then

                        Dim HL7 As New HL7_IBOUND_MESSAGE
                        Dim RESPONSE_MESSAGE As String = ""
                        RESPONSE_MESSAGE = HL7.PROCESS_INBOUND_MESSAGE(HL7_STRING_RECEIVED)
                        ' HL7_STRING_RECEIVED = HL7.ADT_MESSAGE.ACCEPT_ACKNOWLEDGEMENT.GENERATE_MESSAGE
                        Dim ACK_MESSAGE_BYTES As Byte() = System.Text.Encoding.ASCII.GetBytes(RESPONSE_MESSAGE)
                        NETWORK_DATA_SREAM.Write(ACK_MESSAGE_BYTES, 0, ACK_MESSAGE_BYTES.Length)
                        ACK_MESSAGE_BYTES = New Byte(256) {}
                        HL7_STRING_RECEIVED = ""


                    End If

                    If NETWORK_BYTES_READ = 0 Then
                        HOSPITAL_HL7_CLIENT.Close()
                        SERVER_TCP_LISTENER.Stop()
                        HL7_NETWORK_INTERFACE()
                    End If




                End If
            
            Loop

Also, meant to add, when tested against a simulator, it works...

Use an BeginAcceptTcpClient() method,

Class MyServer
    Public Sub Start()
        Dim listener As New TcpListener(IPAddress.Any, 4554)
        listener.Start()
        
        While True
            Dim result As IAsyncResult = listener.BeginAcceptTcpClient(callBack, listener)
        End While
    End Sub
    Private Sub callBack(ByVal result As IAsyncResult)
        Dim listener As TcpListener = DirectCast(result.AsyncState, TcpListener)
        Dim client As TcpClient = listener.EndAcceptTcpClient(result)
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.