I'm trying to connect an old network camera to my computer, and I'm stuck in a very fundamental problem - I find the end of the stream.
I use TcpClient to communicate with the camera, and I really see that it sends these commands, there is no problem.
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 the answer is where the problem begins. Initially, I thought something was simple, like the following bit of code: [/ p>
while (serverStream.DataAvailable) { incoming.Add(serverStream.ReadByte()); }
But this is not so, so this time I had a different version using ReadByte (). The description indicates:
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 like:
Boolean run = true; int rec; while (run) { rec = serverStream.ReadByte(); if (rec == -1) { run = false; //b = (byte)'X'; } else { incoming.Add(rec); } }
No, it still doesn't work. I really see the data coming in after a certain point (which is not always the same, otherwise I could just read that many bytes every time). I am starting to get 0 as the value for the rest of the elements, and it doesn't stop until I stop the execution manually. Here's what it looks like: 
So my question is: did I miss something fundamental here? How can I determine the end of a stream?
Many thanks,
N.
c # sockets tcpclient networkstream
Hamza
source share