【问题标题】:Reading TCP data from camera stops after a specific amount of pictures在特定数量的图片后停止从相机读取 TCP 数据
【发布时间】:2016-12-03 04:03:37
【问题描述】:

我正在通过 TCP 从相机读取数据,我首先通过 TCP 发送触发器来拍照,然后我读取相机图像。一切正常,直到 296 次触发完成,从那一刻起,程序在第一次 TCP 读取时被阻塞。

我可以通过再次发送触发器(来自另一个程序)来临时解决此问题。我相信在那一刻相机被触发了 2 次(可能是被阻止的一次和新的触发器(修复))。

有人知道会出什么问题吗?谢谢!

    'Code in Timer
    'Trigger
    Dim tcpClient As New System.Net.Sockets.TcpClient()
    tcpClient.Connect("ip", port)
    Dim networkStream As NetworkStream = tcpClient.GetStream()
    If networkStream.CanWrite Then
        networkStream.Write(dataTrigger, 0, n)
        tcpClient.Close()
    Else

        If Not networkStream.CanWrite Then
            Console.WriteLine("cannot read data from this stream")
            tcpClient.Close()
        End If
    End If
    networkStream.Close()

    'Read images
    Dim tcpClient As New System.Net.Sockets.TcpClient()
    tcpClient.Connect("ip", port)
    Dim networkStream As NetworkStream = tcpClient.GetStream()
    If networkStream.CanRead Then
        ' Reading data length
        Dim dataLength(15) As Byte
        networkStream.Read(dataLength, 0, 16)

        ' Reading all data
        Dim bytesReaded, bytesLeft, bytesCounter As Integer
        bytesCounter = 0
        bytesLeft = dataLength
        Do While bytesLeft > 0
            bytesReaded = networkStream.Read(dataImages, bytesCounter, bytesLeft)
            bytesCounter = bytesCounter + bytesReaded
            bytesLeft = dataLengthInt - bytesCounter
        Loop
        tcpClient.Close()
    Else
        If Not networkStream.CanRead Then
            Console.WriteLine("cannot not write data to this stream")
            tcpClient.Close()
        End If
    End If
    networkStream.Close()

【问题讨论】:

    标签: .net vb.net networking tcp


    【解决方案1】:

    networkStream.Read(dataLength, 0, 16) 不能保证在一次读取中读取所有数据。你需要循环直到你有 16 个字节,就像你在主循环中所做的那样。

    你也做了bytesLeft = dataLengthInt - bytesCounter,但你没有在你的代码中为dataLengthInt赋值。

    我不知道您是否还有其他错误,(我不太了解 VB.NET)但其中最大的两个是跳出来的。

    【讨论】:

    • 谢谢,这可能是一个原因,尽管我认为这不会阻止该功能。省略了 dataLengthInt 转换。
    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多