优雅地关闭 TCP 套接字
我正在用 C++ 编写一个 IRC 客户端,目前我遇到一个问题,在退出时,我这样做:
Send("QUIT :Quit\r\n"); // just an inline, variadic send() wrapper shutdown(m_hSocket, SD_BOTH); closesocket(m_hSocket); WSAShutdown();
但是,问题是没有发送 QUIT 消息。我嗅探了来自客户端的数据包,事实上该消息从未发送过。我相信这是套接字未刷新的问题,但我不知道如何执行此操作,Google 建议禁用 Nagle 算法,但我怀疑这是一个好的做法。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该检查
send
的返回值:您尝试发送的数据是否确实被网络堆栈接受? (一般来说,这应该在每次send
调用之后完成,而不仅仅是在这种情况下)。假设数据被接受,那么据我所知,它应该作为调用
shutdown
的结果而实际传输。您可以尝试使用SO_LINGER
来查看是否有影响,请参阅 MSDN 上的正常关闭、延迟选项和套接字关闭。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 everysend
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 usingSO_LINGER
to see if it makes a difference, see Graceful Shutdown, Linger Options, and Socket Closure on MSDN.