Hello All,
I have a question about receiving a stream from a piece of software using tcp sockets. I am able to receive the JPEG Capture as a 4-byte stream and successfully save it into my program. But for some odd reason, I cannot get the image to recreate on my end. Is there any certain way to retrieve a streamed JPEG in 4byte chucks and recreate it on my end? It keeps saying that the image is invalid. I'll post some demo code ASAP.
Thank you!
AlkjPosted Sep 4, 2007, 12:24 PM
Here is the code that I promised plus more of a description of what happens:
using
System;using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpClient socketForServer = new TcpClient("127.0.0.1", 4200);
NetworkStream networkStream = socketForServer.GetStream();
StreamReader streamr = new StreamReader(networkStream);
StreamWriter streamw = new StreamWriter(networkStream);
FileStream file = new FileStream("test1.jpg", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter image = new StreamWriter(file);
string output;
streamw.WriteLine("password");
streamw.Flush();
output = streamr.ReadLine();
Console.WriteLine(output);
streamw.WriteLine("/capture");
streamw.Flush();
output = streamr.ReadLine();
Console.WriteLine(output);
int i = 0;
while ((output = streamr.ReadLine()) != null)
{
image.Write(output);
Console.WriteLine(i);
i++;
}
image.Close();
file.Close();
streamr.Close();
streamw.Close();
}
}
}
What happens is that it begins to read all of the lines but it seems to stop somewhere along there but never errors out. Reason I put in: int i and then incremented it is because it will always stop at 237 (assuming line 237). I'll give it a while, but it never finishes and the final image still says invalid.
Any help would be great.
Thank you!