BeginSend question about sent bytes

A

appzguy

I was hoping someone could help me understand the number of bytes sent
if I am using BeginSend.

Let's assume I send 100 bytes of data using BeginSend. In my async
callback, if I call EndSend it will return me the number of bytes that
were actually sent.

My question is under what circumstances will the number of bytes
reported when calling EndSend not match the number of bytes I wanted to
send. So, EndSend says it sent 50 bytes and I wanted to send 100
bytes.

If the case is that EndSend reports only 50 bytes were sent, do I need
to resend the remaining 50 bytes again by calling BeginSend again with
those 50 bytes?
 
B

Barry Kelly

I was hoping someone could help me understand the number of bytes sent
if I am using BeginSend.

Let's assume I send 100 bytes of data using BeginSend. In my async
callback, if I call EndSend it will return me the number of bytes that
were actually sent.

My question is under what circumstances will the number of bytes
reported when calling EndSend not match the number of bytes I wanted to
send. So, EndSend says it sent 50 bytes and I wanted to send 100
bytes.

If the case is that EndSend reports only 50 bytes were sent, do I need
to resend the remaining 50 bytes again by calling BeginSend again with
those 50 bytes?

Theoretically, yes, you need to loop and keep sending slices of the
array until it's all sent. However, I note that NetworkStream, which
wraps Socket's Send and Receive, doesn't care about the return value
from Send / EndSend. If NetworkStream's implementation is correct, then
you don't need to iterate.

-- Barry
 
A

appzguy

Thanks Barry,

Is there anything I need to assume about those 50 bytes that did not
get sent? If initially I sent 100 bytes but the EndSend reports only
50 bytes -- what happened to those 50 missing bytes?
 
B

Barry Kelly

Thanks Barry,

Is there anything I need to assume about those 50 bytes that did not
get sent? If initially I sent 100 bytes but the EndSend reports only
50 bytes -- what happened to those 50 missing bytes?

Did you actually see that result? If you did, then that indicates that
NetworkStream's implementation is in error.

-- Barry
 
M

Markus Stoeger

Greg said:
I am kind of curious if you have actually seen this as well.

I have never seen this happen either. The docs for BeginSend say the
following:

[...] and will block on EndSend _until the Socket sends the number of
bytes requested_ or throws an exception.

Max
 
B

Barry Kelly

Greg Young said:
I am kind of curious if you have actually seen this as well.

I suspect the signature is the way it is in order to conform to the
original BSD send() API.

-- Barry
 
A

appzguy

Thanks to everyone who has responded.....

To answer your question, I have not actually seen this. I was just
curious on all the different cases I would need to handle since I am
writing a client/server application. However, from another forums
posting, someone kindly pointed me at the MSDN documentation (which I
should've read more carefully and I would've gotten my answer)
It basically states, that it will block until SOME of the buffer was
sent...the keyword being some which means if BeginSend only sent 50
bytes of the 100 bytes I requested I would have to requeue the
remaining 50 bytes that were unsent.
From MSDN, the EndSend method:

If you are using a connectionless protocol, EndSend will block until
the datagram is sent. If you are using a connection-oriented protocol,
EndSend will block until some of the buffer was sent. If the return
value from EndSend indicates that the buffer was not completely sent,
call the BeginSend method again, modifying the buffer to hold the
unsent data.

There is no guarantee that the data you send will appear on the network
immediately. To increase network efficiency, the underlying system may
delay transmission until a significant amount of outgoing data is
collected. A successful completion of the BeginSend method means that
the underlying system has had room to buffer your data for a network
send.
 

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