user4572253
user4572253

Reputation:

How to know when the socket has received a FIN packet when using WSARecv() and IOCP?

When using blocking sockets and the recv() function, when the socket has received a FIN packet, recv() will return 0.

However, how to know when the socket has received a FIN packet when using WSARecv() and IOCP? I think that the lpNumberOfBytesRecvd will also be set to 0, but I'm not sure.

Upvotes: 1

Views: 847

Answers (1)

Len Holgate
Len Holgate

Reputation: 21616

If the WSARecv() call is in progress when the FIN arrives then you will get a successful completion with lpNumberOfBytesRecvd set to 0.

If the WSARecv() call is initiated after the FIN arrives then you may get a WSAESHUTDOWN error from the WSARecv() call.

I tend to issue a shutdown(s, SD_RECEIVE) on the socket when I get the first 'client close' (i.e. zero byte read return) as this then gives the predictable behaviour above and avoids a trip through the IOCP for subsequent attempts to receive...

Upvotes: 1

Related Questions