Saul
Saul

Reputation: 1002

Gracefully shut down a TCP socket

I'm writing an IRC client in C++ and currently I'm having an issue where, upon exit, I do:

Send("QUIT :Quit\r\n"); // just an inline, variadic send() wrapper
shutdown(m_hSocket, SD_BOTH);
closesocket(m_hSocket);

WSAShutdown();

However, the issue is that the QUIT message is not being sent. I've sniffed the packets coming from the client and infact this message is never sent. I believe this is an issue with the socket not being flushed, but I have no idea how to do this and Google suggested disabling Nagle's algorithm but I doubt this is good practice.

Thanks in advance.

Upvotes: 7

Views: 9278

Answers (1)

Jon
Jon

Reputation: 437336

First of all you should check the return value of send: are the data you attempt to send actually accepted by the network stack? (In general this should be done after each and every send call, not just in this case).

Assuming the data is accepted, then AFAIK it should be actually transmitted as a result of calling shutdown. You might try using SO_LINGER to see if it makes a difference, see Graceful Shutdown, Linger Options, and Socket Closure on MSDN.

Upvotes: 6

Related Questions