Question about NetworkStream

N

Nuno Magalhaes

In a simple code I do something like this in a synchronous connection:
-*server*-----------------------------
ClientTcp=ListenerTcp.AcceptTcpClient();
NetworkStream nstream=ClientTcp.GetStream();
ImagePictureBox.Image.Save(nstream,System.Drawing.Imaging.ImageFormat.Jpeg);
nstream.Close();

If I don't close the underlying socket with nstream.Close() de image
never gets to the client. Why does this occur since nstream is
unbuffered it should get to the client. In the client I do something
like this:
-*client*----------------------------
NetworkStream nstream=ClientTcp.GetStream();
//Get image data as JPEG
ImagePictureBox.Image=Image.FromStream(nstream);
nstream.Close();

The image never gets to client if I don't close the socket in the
server but this forces me to establish connections after connections
and it gets slow. Should I move to asynchronous socket connections...
does this problem persists in asynchronous mode? If I want to get an
image passed to the client I have to make the client acknowledge the
server at the end of image... and also have to read the bytes
asynchronously using this format: |Size|Byte Data|.

Any lights on this would be great,
Nuno Magalhães.
 
W

William Stacey [MVP]

What are you doing on the server side. If you block until you get 0, then
only socket shutdown will produce that on your Receive. Prepend the len
(int or short) before the byte stream. Then read the len bytes and loop
until you read that many bytes. So break your stream up into messages (which
are length prepended stream of bytes.)

--
William Stacey [MVP]

In a simple code I do something like this in a synchronous connection:
-*server*-----------------------------
ClientTcp=ListenerTcp.AcceptTcpClient();
NetworkStream nstream=ClientTcp.GetStream();
ImagePictureBox.Image.Save(nstream,System.Drawing.Imaging.ImageFormat.Jpeg);
nstream.Close();

If I don't close the underlying socket with nstream.Close() de image
never gets to the client. Why does this occur since nstream is
unbuffered it should get to the client. In the client I do something
like this:
-*client*----------------------------
NetworkStream nstream=ClientTcp.GetStream();
//Get image data as JPEG
ImagePictureBox.Image=Image.FromStream(nstream);
nstream.Close();

The image never gets to client if I don't close the socket in the
server but this forces me to establish connections after connections
and it gets slow. Should I move to asynchronous socket connections...
does this problem persists in asynchronous mode? If I want to get an
image passed to the client I have to make the client acknowledge the
server at the end of image... and also have to read the bytes
asynchronously using this format: |Size|Byte Data|.

Any lights on this would be great,
Nuno Magalhães.
 
N

Nuno Magalhaes

Thank you your response. I guess you're right... I'll have to use the
read and write methods of the NetworkStream.

Thank you,
Nuno Magalhães.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top