Search code examples
vb.netsocketstcptcpclientnetworkstream

Don't get any data from socket when data should be there, and no exceptions raised, connection is open. Using DataAvailable to wait for data


I have a problem reading data from an RFID-reader. I connect to the reader by tcp and wait for DataAvailable to be true, then reading the data until I got a end of data character. Then I just go back and waiting for a new DataAvailable. This is done in a own thread for the function.

There seems to be some kind of timeout, If I don't got any data in a couple of minutes, then it just sits there in the do/loop waiting for DataAvailable. I hold the card to the RFID reader, it beeps, but there is no data available. I don't get any exceptions, and the information says that the clientsocket is still connected. Are there anything more I can check?

If I put the card to the reader in a minute-interval it seems as this will never occur. So 2-3 minutes idle:ing seems to do this.

Here are my code to read data from the socket, I have taken away some irrelevant code:

Sub test(ByVal ip As String, ByVal port As Integer)
    ' this sub is meant to run forever
    Try
        Dim clientSocket As New System.Net.Sockets.TcpClient()
        clientSocket.Connect(ip, port)
        Using serverStream As NetworkStream = clientSocket.GetStream()
            Do 'loop forever or until error occur

                'Every new dataentry starts here
                Dim inStream(0) As Byte
                Dim returndata As String = ""

                Do 'loop forever 
                    Do Until serverStream.DataAvailable 'loop until data exists to read
                        If clientSocket.Connected = False Then

                            'this will never happen.
                            'but if there are more than 5 minutes between data then
                            'it never got data again as if no data was sent.

                            Exit Sub
                        End If
                        Application.DoEvents()
                    Loop

                    'there is data to read, read first byte and 
                    serverStream.Read(inStream, 0, 1)
                    If inStream(0) = 13 Then

                        'got end of data
                        'exit loop if reading chr 13.
                        returndata &= System.Text.Encoding.ASCII.GetString(inStream)
                        Exit Do

                    End If
                Loop

                GotData(returndata)

            Loop
        End Using
    Catch ex As Exception
        ' handle error
    Finally
        'close connection if open
    End Try
End Sub

Solution

  • I found out that socket.Connected only reports the status for when the last command was runned on the connection. So in the Do Until serverStream.DataAvailable loop I used this trick to check if the connection was closed instead:

    Dim test As Boolean = clientSocket.Client.Poll(10, System.Net.Sockets.SelectMode.SelectRead)
    If test = True And serverStream.DataAvailable = False Then
       'restart connection
    End If
    

    So now finally got control over what is happening and know that its because of the client connection is closed that I dont got any data.

    So, then I figured, now that I know that the connection is closed, how do I prevent it? That was easier, just send data to the tcpip-server every 10 second and it will hold it open.

    The result that works for me (this is not the production code, just an example of the solution):

        Dim s As Date = Now
        Do Until serverStream.DataAvailable
            Dim r As Boolean = clientSocket.Client.Poll(10, System.Net.Sockets.SelectMode.SelectRead)
            If DateDiff(DateInterval.Second, s, Now) > 10 Then
               Dim dta(0) As Byte
               dta(0) = 0
               clientSocket.Client.Send(dta)
               s = Now
            End If
            If r = True And serverStream.DataAvailable = False Then
                 'restart sub
                 Exit Sub
            End If
       loop
    

    So now it doesnt even close and need to restart every x minutes.

    My problems are solved and Im a happy coder. ;)