views:

351

answers:

1

i would like some info on the following:

Socket.BeginSend Method (array<Byte>[]()[], Int32, Int32, SocketFlags, AsyncCallback, Object)

client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client);

if the message.lenght is greater than the buffersize (32) do i have to invoke BeginSend multiple times to transmit the entire data packet?

or do i just do multiple reads on the other end until end of actual buffer length is recieved?

this is a discussion as im reading up the documentation before actual implementation.

thanks.

+2  A: 

The buffer size is not 32, it is exactly message.Length as defined. You might be confusing Int32 with the value 32.

In the above case, as long as the actual message size is less than 2147483648 (2^31) you can send it in one call to the method.

On the receiving end you have several choices:

  1. If the message size is not known, you may use a loop to read it chunk by chunk until one Receive call returns less than a full chunk size.
  2. If the message contains its length in the first few bytes (i.e. its header) then you can first read the header, and then allocate enough buffer space to read the entire message in one call.
  3. If the message size is guaranteed to be less than a certain number, you can allocate a big enough buffer that is guaranteed to read any message in one call.
Aviad P.
im just curious but why isnt there a similar mecahnism on recieving end? a way to get real data.length data packet in one go instead of reading buffer length (32 eg) multiple times to get the complete data? hope im clear cause im new to this. thanks.
iEisenhower
I'll edit my answer to address that...
Aviad P.
thank you. you have been very helpful.
iEisenhower