如果 客户端 Socket 不主动关闭该怎么释放服务器资源

allmai_net 2014-08-12 06:15:15
我是服务器主动关闭,客户端收到数据后不做任何关闭和释放操作,请问服务器如何释放资源,我用netstat -a 发现了很多 FIN_WAIT_2,请问该情况如何解决。
...全文
554 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
碼上道 2014-08-14
  • 打赏
  • 举报
回复
没有收到客户端的fin,这时候处于半关闭状态,协议栈一会儿应该也会关闭的。
赵4老师 2014-08-13
  • 打赏
  • 举报
回复
调用shutdown函数。 shutdown The Windows Sockets shutdown function disables sends or receives on a socket. int shutdown ( SOCKET s, int how ); Parameters s [in] A descriptor identifying a socket. how [in] A flag that describes what types of operation will no longer be allowed. Remarks The shutdown function is used on all types of sockets to disable reception, transmission, or both. If the how parameter is SD_RECEIVE, subsequent calls to the recv function on the socket will be disallowed. This has no effect on the lower protocol layers. For TCP sockets, if there is still data queued on the socket waiting to be received, or data arrives subsequently, the connection is reset, since the data cannot be delivered to the user. For UDP sockets, incoming datagrams are accepted and queued. In no case will an ICMP error packet be generated. If the how parameter is SD_SEND, subsequent calls to the send function are disallowed. For TCP sockets, a FIN will be sent after all data is sent and acknowledged by the receiver. Setting how to SD_BOTH disables both sends and receives as described above. The shutdown function does not close the socket. Any resources attached to the socket will not be freed until closesocket is invoked. 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. For example, to initiate a graceful disconnect: Call WSAAsyncSelect to register for FD_CLOSE notification. Call shutdown with how=SD_SEND. When FD_CLOSE received, call recv until zero returned, or SOCKET_ERROR. Call closesocket. Note The shutdown function does not block regardless of the SO_LINGER setting on the socket. An application should not rely on being able to re-use a socket after it has been shut down. In particular, a Windows Sockets provider is not required to support the use of connect on a socket that has been shutdown. Return Values If no error occurs, shutdown returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError. Error Codes WSANOTINITIALISED A successful WSAStartup must occur before using this function. WSAENETDOWN The network subsystem has failed. WSAEINVAL The how parameter is not valid, or is not consistent with the socket type. For example, SD_SEND is used with a UNI_RECV socket type. WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. WSAENOTCONN The socket is not connected (connection-oriented sockets only). WSAENOTSOCK The descriptor is not a socket. QuickInfo Windows NT: Yes Windows: Yes Windows CE: Use version 1.0 and later. Header: Declared in winsock2.h. Import Library: Link with ws2_32.lib. See Also connect, socket
我看你有戏 2014-08-13
  • 打赏
  • 举报
回复
服务器检测,当检测到某个客户端在n秒内,没有发送数据了,就给该客户端发个心跳包,等待客户端回复 客户端回复超时,那服务器就可以判断这个客户端断了,服务器关闭这个socket 网狐棋牌就这么搞的
allmai_net 2014-08-12
  • 打赏
  • 举报
回复
引用 3 楼 jerry_dqh 的回复:
[quote=引用 楼主 coyerhc 的回复:] 我是服务器主动关闭,客户端收到数据后不做任何关闭和释放操作,请问服务器如何释放资源,我用netstat -a 发现了很多 FIN_WAIT_2,请问该情况如何解决。
不对啊,FIN_WAIT_2只有客户端才会进入,你是server端还是client端[/quote] 服务端啊,我测试过的,我客户端没有主动关闭,但也不退出程序,我服务端异步收发,发完了服务端主动关闭,但是还是很多FIN_WAIT_2,但如果客户端主动关闭,则服务器会出现 TIME_WAIT ,很快 TIME_WAIT就会消失。
碼上道 2014-08-12
  • 打赏
  • 举报
回复
引用 楼主 coyerhc 的回复:
我是服务器主动关闭,客户端收到数据后不做任何关闭和释放操作,请问服务器如何释放资源,我用netstat -a 发现了很多 FIN_WAIT_2,请问该情况如何解决。
不对啊,FIN_WAIT_2只有客户端才会进入,你是server端还是client端
勤奋的小游侠 2014-08-12
  • 打赏
  • 举报
回复
设置定时器,超时即关闭
唯恐天下不乱 2014-08-12
  • 打赏
  • 举报
回复
超时 异步 都可以

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧