C# - TcpClient - Detecting end of stream?

Hamza picture Hamza · Jun 29, 2011 · Viewed 17.3k times · Source

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.

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());
        }

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

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

        }

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: data

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

Many thanks,

H.

Answer

The Evil Greebo picture The Evil Greebo · Jun 29, 2011

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)

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.