当数据应该存在时,不要从套接字获取任何数据,并且没有引发异常,连接已打开。使用DataAvailable等待数据

发布于 2024-10-26 15:12:11 字数 2017 浏览 3 评论 0原文

我在从 RFID 读取器读取数据时遇到问题。我通过 tcp 连接到读取器并等待 DataAvailable 为 true,然后读取数据,直到获得数据字符结束。然后我就回去等待新的 DataAvailable。这是在该函数自己的线程中完成的。

似乎存在某种超时,如果我在几分钟内没有收到任何数据,那么它只是坐在 do/loop 中等待 DataAvailable。我将卡靠近 RFID 读取器,它发出蜂鸣声,但没有可用数据。我没有收到任何异常,并且信息表明客户端套接字仍然处于连接状态。还有什么我可以检查的吗?

如果我以一分钟的间隔将卡放入读卡器,似乎这种情况永远不会发生。所以 2-3 分钟空闲:ing 似乎可以做到这一点。

这是我从套接字读取数据的代码,我去掉了一些不相关的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2024-11-02 15:12:11

我发现 socket.Connected 仅报告在连接上运行最后一个命令时的状态
因此,在 Do Until serverStream.DataAvailable 循环中,我使用此技巧来检查连接是否已关闭:

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

现在终于可以控制正在发生的事情,并知道这是因为客户端连接已关闭我没有任何数据。

所以,然后我想,现在我知道连接已关闭,我该如何阻止它呢?这更容易,只需每 10 秒向 tcpip 服务器发送一次数据,它就会保持打开状态。

对我有用的结果(这不是生产代码,只是解决方案的示例):

    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

所以现在它甚至不会关闭,需要每 x 分钟重新启动一次。

我的问题得到了解决,我是一个快乐的程序员。 ;)

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. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文