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

1

2

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?

Semidiurnal answered 23/2, 2015 at 14:16 Comment(4)
Not sure:( Since GetQueuedCompletionStatus(Ex), returns the number of bytes sent via a DWORD pointer, you could easily check it. I'm not sure that I bothered with that check in my code:( It seems agains the philosophy of IOCP to return a notification of a partiually-sent buffer without any other indication of a problem, or without foreknowledge of that as a likely result, (eg. you have sent a CancelIoEx).Scleroma
I think it is safe to treat an incomplete send ("bytes sent" less than the size of the request) as an error condition.Radley
@Harry Johnston Nice idea! Is this how people who create IOCP applications usually do it?Semidiurnal
No idea, sorry. That's what I usually do, but I'm generally working with callback-based asynchronous IO rather than IOCP. I don't think that makes any difference, but I can't be certain. (And in any case my code is only used in-house.)Radley
H
2

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.

Humbug answered 23/2, 2015 at 23:57 Comment(1)
See Len Holgate answer in this question, I think he talks about other reasons that makes `WSASend()' not send all of the data: #14348208Semidiurnal

© 2022 - 2024 — McMap. All rights reserved.