急:CSOCKET中TCP连接,服务端如何马上知道客户端已断开,为什么onclose()不行?

wjl_2001 2002-05-06 08:47:29
急:CSOCKET中TCP连接,服务端如何马上知道客户端已断开,为什么onclose()不行?
...全文
74 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
formoney 2002-05-08
very good
回复
acptvc 2002-05-06
If the client disconnects the connection gracefully by calling closesocket or shutdown API, the onclose event will be activated. However, if the network disconnectes because of physical problem, for example, network line and OS shuts down, the onclose will not be notified.

Therefore, there is no method to detect the physical disconnection of a network. There is also no method to detect the program down of the opposite side.
However there are 2 ways.
The first way: you need to set SO_KEEPALIVE by setsockopt API.
Please refer to the following article:
Q140325 How to Determine Loss of Client/Server Connection
http://support.microsoft.com/support/kb/articles/q140/3/25.asp

The other way: you can sets a socket as a non-block in the program, and implement TIMEOUT by select(). If the opposite side does not respond for a certain time, we should assume the opposite side is down and close by close().

CSDN
======================
- 微软全球技术中心 <display name>

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================

回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2002-05-06 08:47
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……