Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - TcpClient - Detecting end of stream?

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.

like image 685
Hamza Avatar asked Jun 29 '11 15:06

Hamza


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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.

like image 63
The Evil Greebo Avatar answered Oct 11 '22 15:10

The Evil Greebo


Well you can't exactly says EOS on a network communication ( unless the other party drop the connection ) usually the protocol itself contains something to signal that the message is complete ( sometimes a new line, for example ). So you read the stream and feed a buffer, and you extract complete message by applying these strategies.

like image 44
Felice Pollano Avatar answered Oct 11 '22 15:10

Felice Pollano