Socket dropping data? .NET newbie reduced to tears

  • Thread starter Neil Saunders via .NET 247
  • Start date
N

Neil Saunders via .NET 247

Hello everybody, I'm hoping that somebody will be able to help mebefore I cross the bridge of absolute insanity.

I've recently started to teach myself C#, and to begin with Ihave aimed to write a simple client/server system whereby theserver can request a thumbnail of the clients screen to betransmitted back. After brushing up on my windows GDI knowledge,the system quite happily snaps the screen, usesImage.GetThumbnail to shrink it, and then saves this to aMemoryStream, which is then transmitted back to the Server.Everything works fine until I request a thumbnail of 256x256, atwhich point only half of the data seems to be recieved. I'mdoing all of this over loopback for testing.

For instance, the client reports that it is sending 8000 bytes,and the server says it only receives 4000ish.

I've spend countless hours scouring google, but have come up withnothing. The curious thing is that when I set breakpoints on thereceiving loop (See below), all the data is received.

//Read the bytes from the network stream in to the memory stream
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = BUFFER_SIZE;
long totalBytes = 0;

do
{
bytesRead = networkStream.Read(buffer, 0, BUFFER_SIZE);
totalBytes += bytesRead;
ms.Write(buffer, 0, bytesRead);
}
while(networkStream.DataAvailable);

I then just do:

Image thumb = Image.FromStream(ms);
pictureBox1.Image = thumb;

I've tried every combination of Sockets, NetworkStreams,StreamWriters/Readers, but nothing seems to work. It would makemy week if anyone could shed light on this behaviour.

All the best,

Neil Saunders.
 
C

C# Learner

Neil said:
[...]
//Read the bytes from the network stream in to the memory stream
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = BUFFER_SIZE;
long totalBytes = 0;

do
{
bytesRead = networkStream.Read(buffer, 0, BUFFER_SIZE);
totalBytes += bytesRead;
ms.Write(buffer, 0, bytesRead);
}
while(networkStream.DataAvailable);

In place of testing NetworkStream.DataAvailable (which I don't believe
to be suitable in this situation), you should test whether the total
number of bytes read so far is equal to the size of the full packet, or
whether the last call to NetworkStream.Read returned 0 (indicating that
the remote host has disconnected).

Now, you need some way of determining the size of the full packet. In
the request packet, is there a _specified length_ or some kind of
_marker_ which enables the reader of the request to detect that the
whole packet has been received?

In the following code, 'fullPacketLength' is the determined length of
the incoming packet.

<untested code>

byte[] buffer = new byte[32768];
int totalBytesRead = 0;

do
{
int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break; // remote host disconnected
}
ms.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
while (totalBytesRead < fullPacketLength);

 
V

Vijaye Raji

A strem read operation will not always read all the data that was sent from
one side. You need to repeatedly call Read to get all the data.

Usually, such socket communication are always accompanied by a header that
will describe the length of the data that is to follow.

-vJ
Hello everybody, I'm hoping that somebody will be able to help me before I
cross the bridge of absolute insanity.

I've recently started to teach myself C#, and to begin with I have aimed to
write a simple client/server system whereby the server can request a
thumbnail of the clients screen to be transmitted back. After brushing up on
my windows GDI knowledge, the system quite happily snaps the screen, uses
Image.GetThumbnail to shrink it, and then saves this to a MemoryStream,
which is then transmitted back to the Server. Everything works fine until I
request a thumbnail of 256x256, at which point only half of the data seems
to be recieved. I'm doing all of this over loopback for testing.

For instance, the client reports that it is sending 8000 bytes, and the
server says it only receives 4000ish.

I've spend countless hours scouring google, but have come up with nothing.
The curious thing is that when I set breakpoints on the receiving loop (See
below), all the data is received.

//Read the bytes from the network stream in to the memory stream
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = BUFFER_SIZE;
long totalBytes = 0;

do
{
bytesRead = networkStream.Read(buffer, 0, BUFFER_SIZE);
totalBytes += bytesRead;
ms.Write(buffer, 0, bytesRead);
}
while(networkStream.DataAvailable);

I then just do:

Image thumb = Image.FromStream(ms);
pictureBox1.Image = thumb;

I've tried every combination of Sockets, NetworkStreams,
StreamWriters/Readers, but nothing seems to work. It would make my week if
anyone could shed light on this behaviour.

All the best,

Neil Saunders.
 

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