正常关闭TCP套接字 [英] Gracefully shut down a TCP socket
问题描述
我在C ++中编写一个IRC客户端,目前我遇到一个问题,在退出时,我会:
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();
但是,问题是没有发送QUIT消息。我已经嗅闻了来自客户端的数据包,并且此消息从不发送。我相信这是一个问题,插座没有刷新,但我不知道如何这样做,谷歌建议禁用Nagle的算法,但我怀疑这是一个好的做法。
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.
推荐答案
首先,您应该检查 send
:是您尝试发送的数据实际上被网络堆栈接受? (一般来说,这应该在每个发送
调用后完成,而不仅仅是在这种情况下)。
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).
数据被接受,则AFAIK应实际上作为调用 shutdown
的结果而被发送。您可以尝试使用 SO_LINGER
来查看它是否有所作为,请参阅 MSDN上的正常关闭,Linger选项和套接字闭包。
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.
这篇关于正常关闭TCP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!