Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 209
Saul Avatar asked Apr 17 '11 21:04

Saul


1 Answers

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.

like image 163
Jon Avatar answered Sep 30 '22 15:09

Jon