Network loosing data? I'm loosing hair: TcpClient & NetworkStream

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

Neil Saunders via .NET 247

Hi all,

I've scoured all the forums I could find, but to no avail. I'm writing a very simple client/server application whereby the server can connet to the client,

and can then request a thumbnail image of it's desktop be sent back to it. The server can request the thumbnail in multiple sizees, from 16x16, 32x32 etc,

all the way up to 256x256. Everything works fine until I attempt to request a thumbnail of size 256x256, upon which only around half the data is recieved.

The recieving loop on the server (shown below) consists only of three lines, and the infuriating thing is if I breakpoint each of the lines and step through,

all data is recieved with no problems. For development purposes both the client and the server are the same machine, connected as localhost.

The basic workings of the system is as follows:

1. Server connects to client:

client = new TcpClient("localhost",3129);
NetworkStream networkStream = client.GetStream();

2. Client accepts

TcpListener respond = new TcpListener(localHostEntry.AddressList[0], 3129);
respond.Start();
Socket tutor = respond.AcceptSocket();

3. Server sends request, client receives & decodes (Works fine), Client sends screengrab:

//Save the thumbnail to the stream in JPEG format
MemoryStream img = new MemoryStream();
thumbnail.Save(img, GetJPEGImageCodecInfo(), ThumbParams);

NetworkStream ns = new NetworkStream(tutor);
ns.Write(img.ToArray(),0,img.ToArray().Length);

4. Server receives:

//Read back the answer
MemoryStream ms = new MemoryStream();

//Read the bytes from the network stream in to the memory stream
byte[] buffer = new byte[BUFFER_SIZE]; //Buffersize = 1024
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);

//... convert memoryStream to image and display.

If I write to the console the size of img.ToArray().Length (on the client), I get something around 8000 bytes, however at the end of the recieving loop, I'm

always left with something around 4000, unless I breakpoint and step through. I've tried every combination of methods for sending and receiving, and nothing

seems to work.

Oh and one more thing, If I write the partial stream to an image and display it, I receive the *start* of the data fine, it just cuts off half way through

(i.e. I get half a screen shot). If anyone could sort this problem for me, I would be eternally, eternally grateful.

All the best,

Neil Saunders.
 
C# Learner said:
Multi-posted. Doh!

I know - I saw your response too.
Wouldn't this be presuming that the host on the other end disconnected
after sending the packet?

I don't think "packet" is really the word here - I think you mean
"response" really, as the response is likely to be made of several
packets. However, yes, I've been assuming that the server will close
the connection afterwards. If that's not the case, it should send the
length beforehand.
 
Jon Skeet [C# MVP] wrote:

[...]
I don't think "packet" is really the word here - I think you mean
"response" really, as the response is likely to be made of several
packets.

Well, by "packet", I mean an application-level protocol packet (as
opposed to, say, a TCP packet). I've always used this word to describe
data units at different layers, but, now that you mention it, I'm not
sure whether or not this is a valid use of the word. Hmm...
 
Back
Top