When I call WSASend(), will all the data be sent?

2019-04-14 14:25发布

问题:

When using IOCP, if I call WSASend() with let's say 2 KB of data. When I receive a completion notification, will this completion notification indicate that the entire 2 KB were sent, or there could be some bytes that were not sent?

回答1:

Under normal circumstances, yes, your completion will receive notification that the entire 2 KB was sent, as long as the socket is using overlapped I/O.

The only scenario where it may return less than the size of the data sent is when the connection is terminated in the middle of a send; in this case, it returns the partial number of bytes sent.

If it's not using overlapped I/O, it can return a partial write if the transport buffers don't have enough space to receive your entire write.

In my experience, this case is rarely handled, as the connection being terminated will usually invoke some other error handling, and there is rarely anything useful you can do with the number of bytes sent.