Disabling TCP with sockets connected through AcceptEx ()

The documentation for AcceptEx() says:

When this operation is completed successfully, sAcceptSocket can be but only for the following functions:

  • ReadFile
  • Writefile
  • to send
  • WSASend
  • Recv
  • WSARecv
  • TransmitFile
  • closesocket
  • setsockopt (only for SO_UPDATE_ACCEPT_CONTEXT)

Note that shutdown() not listed. Indeed, calling shutdown(sAcceptSocket, SD_SEND) returns SOCKET_ERROR and WSAGetLastError() causes a WSAENOTCONN error:

The request to send or receive data was denied because the socket was not connected and (when sending a datagram via a sendto call) the address was not sent.

Why can't you disconnect a socket connected through AcceptEx() ? Also, why is the socket not showing as connected since it is already receiving data (a completed overlap operation and a completion notification indicate that several bytes of size>0 have been received)?

+7
source share
1 answer

You need to call setsockopt(SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT) after AcceptEx() . The received socket is not completely related to the properties of the listening socket and, as such, will not be in a fully connected state until SO_UPDATE_ACCEPT_CONTEXT is set. Some winsock API functions are affected by this, including getpeername() , getsockname() and shutdown() .

If you use ConnectEx() for an outbound connection, you need to call setsockopt(SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT) after ConnectEx() is complete before you can use shutdown() . This is documented behavior on MSDN. It does not say the same for AcceptEx() and SO_UPDATE_ACCEPT_CONTEXT , but shutdown() has a similar restriction for sockets accepted by AcceptEx() .

+8
source

All Articles