0

result = ::send(s, buf, length, flag)

How much data will be eventually sent? when I get result == SOCKET_ERROR and WSAGetLastError () == WSA_IO_PENDING will length bytes of buf be all eventually sent?

or I need to try re-sending the same buf data again?

On the other hand

WSA_IO_PENDING

Overlapped operations will complete later.

The application has initiated an overlapped operation that cannot be completed immediately. A completion indication will be given later when the operation has been completed....

it sounds to me like the send operation WILL be completed later....

UPDATE: the further question is asked here Unexpected WSA_IO_PENDING from blocking (overlapped I/O) Winsock2 calls

rnd_nr_gen
  • 2,203
  • 3
  • 36
  • 55

2 Answers2

1

From the documentation page that you've linked:

If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the len parameter. Otherwise, a value of SOCKET_ERROR is returned

That is if result == SOCKET_ERROR then nothing was sent.

As for WSA_IO_PENDING, it could occur if you were using async io, for example WSASend. In this case amount of bytes actually sent needs to be retrieved later, possibly using io completion routine.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • thanks. the document of WSA_IO_PENDING just makes me a little bit confused – rnd_nr_gen Aug 09 '18 at 09:36
  • 2
    @rnd_nr_gen Note that `WSA_IO_PENDING` is not even listed among possible error codes for this function. It may occur if you were using async io rather than blocking `send` call. That is `send` does not perform overlapped operations. – user7860670 Aug 09 '18 at 09:38
  • the same goes to `::recv`. I do get WSA_IO_PENDING from ::recv even the error code is also not listed. (Win 10) – rnd_nr_gen Aug 09 '18 at 09:43
  • @rnd_nr_gen as VTT said, `send()` and `recv()` DO NOT perform overlapped I/O, so it is simply not possible for them to report `WSA_IO_PENDING`. If you put the socket into non-blocking mode, `send()`/`recv()` can report `WSAEWOULDBLOCK`, otherwise they operate in synchronous mode instead. Only overlapped I/O enabled functions, like `WSASend/Ex()`, `WSARecv()`, `RIOSend/Ex()`, etc can report `WSA_IO_PENDING`. – Remy Lebeau Aug 09 '18 at 20:24
  • then it would be really interesting why I **did** get WSA_IO_PENDING from, at least. ::recv() All I have used for the socket are #include , ws2_32.lib, ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP), ::WSAIoctl(s, _WSAIOW(IOC_VENDOR, 4), &heartbeat, sizeof(heartbeat), 0, 0, &no, 0, 0) for keep-alive. and ::send(), ::recv() on MSVS2015/Win10 – rnd_nr_gen Aug 10 '18 at 06:38
  • @rnd_nr_gen Maybe you've got It from `WSAIoctl`? – user7860670 Aug 10 '18 at 07:14
  • not from WSAIoctl. indeed from ::recv(). randomly but not unusual. from https://learn.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-socket the ::scoket has overlapped attribute as a default. – rnd_nr_gen Aug 11 '18 at 09:47
0

after some research and experiment.. here are some facts.

::socket will have implicitly overlapped attribute enabled.

A socket can be created w/o overlapped attribute WSA_FLAG_OVERLAPPED via ::WSASocket or via setsocketopt / SO_OPENTYPE, however timeout feature (e.g. SO_RCVTIMEO) requires overlapped attribute.

When using ::send ::recv a WSA_IO_PENDING error is possible and should be handled with WSAGetOverlappedResult (I observed such behavior since Windows 10)

Alternatively, to use ::WSARecv or ::WSASend instead.

There are still questions unanwsered:

UPDATE: the further question is asked here Unexpected WSA_IO_PENDING from blocking (overlapped I/O) Winsock2 calls

rnd_nr_gen
  • 2,203
  • 3
  • 36
  • 55
  • you can not use `WSAGetOverlappedResult` after `send` or `recv` by very simply reason - this api not take pointer to `WSAOVERLAPPED`. so which `WSAOVERLAPPED` you will be use in call `WSAGetOverlappedResult` ? – RbMm Sep 19 '18 at 15:46
  • That is more and less my initial question and guessed assumption. The ´send´ and ´recv´ blocking call can have overlapped I/O by default. The same as if you call ´WSASend´ and ´WSARecv´ with both NULL lpOverlapped and NULL lpCompletionRoutine as stated from MSDN. And after those calls you may get SOCKET_ERROR with WSA_IO_PENDING. It is very strange to me that a blocking call has such error. Should I just ignore the "error", OR handle it by closing socket?? ´WSAGetOverlappedResult´ can be called with WSAOVERLAPPED having NULL event handle. Can I use it to poll result? – rnd_nr_gen Sep 19 '18 at 18:35
  • I mean ´WSAGetOverlappedResult´ with an empty WSAOVERLAPPED object. I hope the Win API will handle it properly for such situation, however I did not find any confident reference... Any comment? – rnd_nr_gen Sep 19 '18 at 18:38
  • 1
    *I mean `WSAGetOverlappedResult` with an empty `WSAOVERLAPPED` object* - no. this is always error and senseless. in `´WSAGetOverlappedResult` need use exactly pointer to overlapped used in io request – RbMm Sep 19 '18 at 18:47
  • *I hope the Win API will handle it properly for such situation* - impossible. the pointer to overlapped is main parameter for `[WSA]GetOverlappedResult` - without it impossible determinate - are io is finished, if finished - with which error code, if no error - how many bytes transferred. all this information stored in `WSAOVERLAPPED` object. no valid object - nothing will be – RbMm Sep 19 '18 at 18:50
  • *It is very strange to me that a blocking call has such error.* - here agree. the `send` or `recv` must never return `WSA_IO_PENDING`. very strange if you got it (and not mistake) – RbMm Sep 19 '18 at 18:52
  • probably I should post another question about it. It is really strange... to get WSA_IS_PENDING from very basic send and recv calls. – rnd_nr_gen Sep 20 '18 at 06:51
  • dont know how this can happens - are you sure that functions fails or you get last error exactly from this api calls ? – RbMm Sep 20 '18 at 06:58
  • yes. it is also single thread.... ,however, within a software which has other tons of network related library running in other threads.. I assume they should not interfere with each other – rnd_nr_gen Sep 20 '18 at 07:09
  • UPDATE: the question is re-asked here https://stackoverflow.com/questions/52419993/unexpected-wsa-io-pending-from-blocking-overlapped-i-o-winsock2-calls – rnd_nr_gen Sep 20 '18 at 07:42