Socket receive question

  • Thread starter Thread starter Ole
  • Start date Start date
O

Ole

Hi

I'm receiving data from another device that sends data in packages of 8000.
In my application I use this code to receive the data:
SizeReceived = MySocket.Receive(Buffer, 8000, SocketFlags.None);

But the receive-method sometimes returns after reading 4378 bytes?? Isn't it
so that the Socket.receive method should block until all the data is
received?

Thanks,
Ole
 
Hi,


Nope, you maust call Recieve while you get all data, as many times it needs.

Receive block only if no data available, till there is some data. But you
never be sure how many you will recieve, so you need to check SizeReceived.
 
Ole said:
Hi

I'm receiving data from another device that sends data in packages of
8000. In my application I use this code to receive the data:
SizeReceived = MySocket.Receive(Buffer, 8000, SocketFlags.None);

But the receive-method sometimes returns after reading 4378 bytes?? Isn't
it so that the Socket.receive method should block until all the data is
received?

No, it's just a wrapper around the Berkely sockets API. That's why the
actual number of bytes read is returned. It's trivial to write a wrapper
method that loops until the desired amount of data is obtained.
 
Thanks! - Does that mean that I could receive even a larger number than I
ask for?

Thanks,
Ole
 
Nope, because you never can't get more data than passed buffer, because
recieved data is to Buffer.

You must do something like:

byte[] buffer = new byte[8000];
int totalReaded = 0;
while(countReaded < 8000){
int sizeReaded = MySocket.Receive(buffer,totalReaded,buffer.Length -
totalReaded, SocketFlags.None);
totalReaded += totalReaded;
}

Similar code gets exactly 8000 bytes as you need.
 

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

Back
Top