C#-TcpClient-正在检测流的结尾? [英] C# - TcpClient - Detecting end of stream?

查看:52
本文介绍了C#-TcpClient-正在检测流的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一台古老的网络摄像机连接到我的计算机上,但我陷入了一个非常基本的问题-检测流的结束.

I am trying to interface an ancient network camera to my computer and I am stuck at a very fundamental problem -- detecting the end of stream.

我正在使用 TcpClient 与摄像机通信,实际上可以看到它传输了命令数据,在这里没有问题.

I am using TcpClient to communicate with the camera and I can actually see it transmitting the command data, no problems here.

        List<int> incoming = new List<int>();            
        TcpClient clientSocket = new TcpClient();
        clientSocket.Connect(txtHost.Text, Int32.Parse(txtPort.Text));
        NetworkStream serverStream = clientSocket.GetStream();
        serverStream.Flush();

        byte[] command = System.Text.Encoding.ASCII.GetBytes("i640*480M");
        serverStream.Write(command, 0, command.Length);

回读响应是问题开始的地方.最初,我认为像下面的代码这样简单的事情会起作用:

Reading back the response is where the problem begins though. I initially thought something simple like the following bit of code would have worked:

        while (serverStream.DataAvailable)
        {
            incoming.Add(serverStream.ReadByte());
        }

但是没有,所以这次我使用ReadByte()获得了另一个版本.说明如下:

But it didn't, so I had a go another version this time utilising ReadByte(). The description states:

从流中读取一个字节,然后在流一个字节,如果返回则返回-1在流的结尾.

Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

所以我想我可以按照以下方式实现一些东西:

so I thought I could implement something along the lines of:

        Boolean run = true;
        int rec;
        while (run)
        {
            rec = serverStream.ReadByte();

            if (rec == -1)
            {
                run = false;
                //b = (byte)'X';
            }
            else
            {
                incoming.Add(rec);
            }

        }

不,仍然无法正常工作.实际上,我可以看到数据在某个点之后和之后进入(这并不总是相同的,否则我每次可以读取那么多字节)我开始得到 0 作为其余部分的值元素,直到我手动停止执行才停止.看起来是这样的:

Nope, still doesn't work. I can actually see data coming in and after a certain point (which is not always the same, otherwise I could have simply read that many bytes every time) I start getting 0 as the value for the rest of the elements and it doesn't halt until I manually stop the execution. Here's what it looks like:

所以我的问题是,我是否在这里缺少一些基本的东西?如何检测流结束?

So my question is, am I missing something fundamental here? How can I detect the end of the stream?

非常感谢,

H.

推荐答案

您所缺少的是如何考虑TCP数据流.这是一个开放的连接,就像一条开放的电话线一样-另一端的某人可能会说话,也可能不会说话( DataAvailable ),并且仅仅是因为他们停下来喘口气( DataAvailable ==false ),这并不意味着它们实际上已经完成了当前语句.过了一会儿,他们可以再次讲话( DataAvailable == true )

What you're missing is how you're thinking of a TCP data stream. It is an open connection, like an open phone line - someone on the other end may or may not be talking (DataAvailable), and just because they paused to take a breath (DataAvailable==false) it doesn't mean they're actually DONE with their current statement. A moment later they could start talking again (DataAvailable==true)

您需要为ABOVE TCP(实际上只是一个传输层)通信协议定义某种规则.因此,例如,在当前图像传输完成后,摄像机可能会向您发送一个特殊的字符序列,因此您需要检查发送的每个字符,确定该序列是否已发送给您,然后采取适当的措施.

You need to have some kind of defined rules for the communication protocol ABOVE TCP, which is really just a transport layer. So for instance perhaps the camera will send you a special character sequence when it's current image transmission is complete, and so you need to examine every character sent and determine if that sequence has been sent to you, and then act appropriately.

这篇关于C#-TcpClient-正在检测流的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆