1

I have found out that WSASend() may not send all of the data, for example if I asked it to send 800 bytes, it may only send 600 bytes.

Now my question is: are the situations where this can happen are extremely rare that I should not bother handling this kind of event. Or do I have to handle it? Can't I for example just show an error message to the user that not all data have been sent and abort the connection instead of trying to recover?

Note: I am using IOCP.

2 Answers2

2

When sending using overlapped I/O and IOCP it's unlikely that you'll ever see a partial send. It's possibly more likely that you'll see a send with an error where you wanted to send a multiple of the system page size and only a smaller multiple was sent, but even that's fairly unlikely (and I don't have unit tests that force that condition as I view it as theoretical).

When sending via overlapped I/O, over a TCP connection, when your peer is receiving and processing slower than you are sending then this is the more likely situation that you'll encounter, that is, TCP flow control kicking in and your WSASend() calls taking longer and longer to complete.

It's really unlikely that you'll actually see an error either from a WSASend() call or a subsequent GetQueuedCompletionStatus() call. Things will just keep working until they don't...

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
  • So is it okay if I do not try to recover from a partial send, and maybe as I have said just display an error message and abort the connection? –  Feb 27 '15 at 23:01
  • 1
    If you have more than one send pending and you get a partial send failure it may not be possible to do anything else as you may have corrupted your TCP stream if send #6 partially fails but #7 and #8 complete OK. – Len Holgate Feb 28 '15 at 08:51
0

It can happen any time the receiver is slower than the sender. You must handle it by rescheduling the remaining data to be written. Don't treat it as an error.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Actually that's highly unlikely to occur if you're using IOCP and the send is overlapped. – Len Holgate Feb 27 '15 at 22:39
  • And if you disagree, I'd REALLY like to have a unit test which deals with this so a reproducible scenario would be great. – Len Holgate Feb 27 '15 at 22:52
  • @LenHolgate What is needed here is a normative reference that states what your link states, that IOCP will not complete until all the data is sent. Absent that I would stand by my answer. For example, the Posix standard provides that for blocking mode. – user207421 Feb 27 '15 at 22:59
  • 1
    Whilst I agree that would be nice, have you ever experienced the scenario that you describe? I've not seen it in 15 years working with IOCP. Also, I completely disagree with the "any time the receiver is slower" part of your answer because that implies that it is a common situation and it is not. What actually happens when the receiver is slower is that the sender simply uses resources. The sender can, on modern OS's at least, continue to send until it exhausts non paged pool. Also, you are assuming that you CAN reschedule sending - with multiple pending sends that may not be possible. – Len Holgate Feb 28 '15 at 08:50