473,473 Members | 4,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question about NetworkStream

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.

Nov 17 '05 #1
2 2581
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]

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.
Nov 17 '05 #2
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.

William Stacey [MVP] wrote:
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]

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cmjman | last post by:
I have an issue where networkStream.Write doesn't perform its write downstream from my client program until the network stream is closed. I then see the data that was sent appear on the other side....
0
by: Swami | last post by:
I wrote a server (code pasted below) that is supposed to receive ASCII text files. For each file I receive, I change some header info and save it as another file on my end. For some reason, the...
4
by: 0to60 | last post by:
I have a class that wraps a TcpClient object and manages all the async reading of the socket. It works really nice, and I use it all over the place. But there's this ONE INSTANCE where I create...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
0
by: Abubakar | last post by:
Hi, try { int x = ns.Read(readbuffer, 0, readbuffer.Length); } catch (System.IO.IOException ioexception) { UINotifications.ServerMessageDisplay(ioexception.ToString( )); }
12
by: Brian Henry | last post by:
I wrote a simple client / server app (nothing major, just sends text) it works fine locally on the same system, but once i move the client to a remote system it errors and gives me "No connection...
1
by: Rob T | last post by:
Hi, I have a simple little program that I'm going to use to access our SMTP server. The below code works perfectly fine, but my question is if I were to try to execute the last line...
7
by: littleIO | last post by:
Hi, I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated. I have a server which listens, receives calls, processes them and sends back the...
6
by: Ryan Liu | last post by:
Hi, I have some basic question about NetworkStream, can someone explain to me? Thanks a lot in advance! TcpClient has a GetStream() method return a NetworkStream for read and write. I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.