Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to shutdown() a socket on Linux, differences between Linux and Windows?

I am losing data on my sockets because I am doing a close().

The Linux-specific shutdown() manpage is not helpful:

The shutdown() call causes all or part of a full-duplex connection on the socket associated with sockfd to be shut down. If how is SHUT_RD, further receptions will be disallowed. If how is SHUT_WR, further transmissions will be disallowed. If how is SHUT_RDWR, further receptions and transmissions will be disallowed.

Microsoft's MSDN is MUCH better, but it being Windows specific, there are differences between it and Linux:

To assure that all data is sent and received on a connected socket before it is closed, an application should use shutdown to close connection before calling closesocket. One method to wait for notification that the remote end has sent all its data and initiated a graceful disconnect uses the WSAEventSelect function as follows :

1. Call WSAEventSelect to register for FD_CLOSE notification.
2. Call shutdown with how=SD_SEND.
3. When FD_CLOSE received, call the recv or WSARecv until the function completes with success and indicates that zero bytes were received. If SOCKET_ERROR is returned, then the graceful disconnect is not possible.
4. Call closesocket.

My Question

  • Under Linux, what is the equivalent of waiting for FD_CLOSE (step 1)?

I am getting answers and comments that think I am asking about the behavior on Windows. I am asking about the behavior on Linux, I am merely referencing the Windows documentation because it is much more clear and complete than Linux manpages.

like image 210
dongle26 Avatar asked Feb 19 '23 21:02

dongle26


1 Answers

The MSDN suggestions are followed in my very detailed answer to close() is not closing socket properly. This is essentially the same as Remy Lebeau's answer here.

like image 66
Joseph Quinsey Avatar answered Feb 27 '23 11:02

Joseph Quinsey