服务器如何在SOCKET通讯中知道客户端断开连接或掉线了???

pfans 2002-06-14 09:52:34
我用Socket做了一个服务器和客户端,服务器在客户端连接时能知道,但客户端断开或掉线服务器就不知道了,请问大虾们有好办法吗?
...全文
1356 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
jfzsl 2002-07-12
  • 打赏
  • 举报
回复
up
sans 2002-07-12
  • 打赏
  • 举报
回复
我原来一直也在为这个问题烦恼,最近解决了,方法如下:
在Server端使用Select进行检查,同时设定TimeOut,如10分钟;
在Client端开一个线程,定一个时间,如1分钟就向Server发送一个信号,当Server端如果10分钟还收不到Client端发来的信号,就关闭该Client的服务。
vtable9999 2002-07-10
  • 打赏
  • 举报
回复
winsock也没有
vtable9999 2002-07-10
  • 打赏
  • 举报
回复
自己实现检测机制。tcp/ip不提供这种功能
johnsonrao 2002-07-08
  • 打赏
  • 举报
回复
帮你UP!
dy_paradise 2002-07-08
  • 打赏
  • 举报
回复
gz
lxiumin 2002-07-08
  • 打赏
  • 举报
回复
不知道用GetlastEorror()可不可以?反正我每交断了GetlastEorror就为10054
hzyem 2002-07-08
  • 打赏
  • 举报
回复
使用定时器定时向客户端发送数据包,接收方收到后返回应答数据包,如果超过一定时间没有应答就可以认为连接已断开。
top_hipster 2002-07-08
  • 打赏
  • 举报
回复
TCP/IP在设计时,并没有考虑到侦测网络连线断开这种情形,因为设计目标是适应恶劣的环境的,应用程序必须自己检测这种错误情况。一般是在recv函数检查有没有错误,具体情况可参照一些书籍的详细介绍。
推荐的方法就是如前面所说使用在线检查机制,定时传送数据报,确认对方是否还在线。路由器和路由器之间的检测就是这种形式
mfkzj 2002-07-08
  • 打赏
  • 举报
回复
你使用WSAAsyncSelect()函数,自定义消息WM_SOCKET,然后铺捉FD_ACCEPT等等,在处理消息的开始的时候先判断是否出现网络
可以设置中断时间 如果在相隔时间内客户端没有进行任何操作 就可以设定对方超时 中断连接
kingfire 2002-07-08
  • 打赏
  • 举报
回复
select FD_CLOSE|FD_READ|FD_CLOSE
  • 打赏
  • 举报
回复
我认为应该设置单独的检查在线机制,关于 FD_CLOSE 或者 OnClose 仅仅只能判断socket的正常的close ,对于非正常的吊线是无法检测的。
panyf_2k 2002-06-22
  • 打赏
  • 举报
回复
你是用的sdk,

你使用WSAAsyncSelect()函数,自定义消息WM_SOCKET,然后铺捉FD_ACCEPT等等,在处理消息的开始的时候先判断是否出现网络错误
如下:

case WM_SOCKET:
if(WSAGETSELECTERROR(lParam))
{
if(WSAGETSELECTERROR(lParam) == WSAECONNABORTED)
{
// 如果客户端关闭socket,就产生这个错误
}
else
{
// 其他socket错误
}
}

OK?
danscort2000 2002-06-22
  • 打赏
  • 举报
回复
设置超时设置或者单独开1个线程检查各个线程是否ACTIVE,是否超时,
如果超时的话就根据你的系统KILL掉或者挂起。
这里应该设置有同步变量来控制。
anjy 2002-06-22
  • 打赏
  • 举报
回复
Onclose()
lj_csdn 2002-06-22
  • 打赏
  • 举报
回复
select FD_CLOSE|FD_READ|...
masterz 2002-06-22
  • 打赏
  • 举报
回复
How do I detect an abnormal network disconnect?
The previous question deals with detecting when a protocol connection is dropped normally, but what if you want to detect other problems, like unplugged network cables or crashed workstations? In these cases, the failure prevents notifying the remote peer that something is wrong. My feeling is that this is usually a feature, because the broken component might get fixed before anyone notices, so why force everyone to restart?

If you have a situation where you must be able to detect all network failures, you have two options:

The first option is to give the protocol a command/response structure: one host sends a command and expects a prompt response from the other host when the command is received or acted upon. If the response does not arrive, the connection is assumed to be dead, or at least faulty.

The second option is to add an "echo" function to your protocol, where one host (usually the client) is expected to periodically send out an "are you still there?" packet to the other host, which it must promptly acknowledge. If the echo-sending host doesn't receive its response or the receiving host fails to see an echo request for a certain period of time, the program can assume that the connection is bad or the remote host has gone down.

If you choose the "echo" alternative, avoid the temptation to use the ICMP "ping" facility for this. If you did it this way, you would have to send pings from both sides, because Microsoft stacks won't let you see the other side's echo requests, only responses to your own echo requests. Another problem with ping is that it's outside your protocol, so it won't detect a failed TCP connection if the hardware connection remains viable. A final problem with the ping technique is that ICMP is an unreliable protocol: does it make a whole lot of sense to use an unreliable protocol to add an assurance of reliability to another protocol?

Another option you should not bother with is the TCP keepalive mechanism. This is a way to tell the stack to send a packet out over the connection at specific intervals whether there's real data to send or not. If the remote host is up, it will send back a similar reply packet. If the TCP connection is no longer valid (e.g. the remote host has rebooted since the last keepalive), the remote host will send back a reset packet, killing the local host's connection. If the remote host is down, the local host's TCP stack will time out waiting for the reply and kill the connection.

There are two problems with keepalives:

Only Windows 2000 allows you to change the keepalive time on a per-process basis. On older versions of Windows, changing the keepalive time changes it for all applications on the machine that use keepalives. (Changing the keepalive time is almost a necessity since the default is 2 hours.)

Each keepalive packet is 40 bytes of more-or-less useless data, and there's one sent each direction as long as the connection remains valid. Contrast this with a command/response type of protocol, where there is effectively no useless data: all packets are meaningful. In fairness, however, TCP keepalives are less wasteful on Windows 2000 than the "are you still there" strategy above.

Note that different types of networks handle physical disconnection differently. Ethernet, for example, establishes no link-level connection, so if you unplug the network cable, a remote host can't tell that its peer is physically unable to communicate. By contrast, a dropped PPP link causes a detectable failure at the link layer, which propagates up to the Winsock layer for your program to detect.

masterz 2002-06-22
  • 打赏
  • 举报
回复
How do I detect when my TCP connection is closed?
All of the I/O strategies discussed in the I/O strategies article have some way of indicating that the connection is closed.

First, keep in mind that TCP is a full-duplex network protocol. That means that you can close the connection half-way and still send data on the other half. An example is a web browser: it sends a short request to the web server, then closes its half of the connection. The web server then sends back the requested data on the other half of the connection, and closes its sending side, which terminates the TCP session.

Normal TCP programs only close the sending half, which the remote peer perceives as the receiving half. So, what you normally want to detect is whether the remote peer closed its sending half, meaning you won't be receiving data from them any more.

With asynchronous sockets, Winsock sends you an FD_CLOSE message when the connection drops. Event objects are similar: the system signals the event object with an FD_CLOSE notification.

With blocking and non-blocking sockets, you probably have a loop that calls recv() on that socket. recv() returns 0 when the remote peer closes the connection. As you would expect, if you are using select(), the SOCKET descriptor in the read_fds parameter gets set when the connection drops. As normal, you'll call recv() and see the 0 return value.

As you might have guessed from the discussion above, it is also possible to close the receiving half of the connection. If the remote peer then tries to send you data, the stack will drop that data on the floor and send a TCP RST to the remote peer.

pfans 2002-06-22
  • 打赏
  • 举报
回复
我没有用CSocket类,用的是SDK.
lakelive 2002-06-14
  • 打赏
  • 举报
回复
用select啊。
加载更多回复(4)
1、本课程是一个干货课程,主要讲解如何封装服务器底层,使用Tcp/ip长连接,IDE使用vs2019 c++开发以及使用c++11的一些标准,跨平台windows和linux,服务器性能高效,单服务器压力测试上万无压力,服务器框架是经历过上线产品的验证,框架简单明了,不熟悉底层封装的人,半个小时就能完全掌握服务器框架上手写业务逻辑。2、本课程是一个底层服务器框架教程,主要是教会学员在windows或linux下如何封装一个高效的,避免踩坑的商业级框架,服务器底层使用初始化即开辟内存的技术,使用内存池,服务器运行期间内存不会溢出,非常稳定,同时服务器使用自定义哈希hashContainer,在处理新的连接,新的数据,新的封包,以及解包,发包,粘包的过程,哈希容器性能非常高效,增、删、查、改永远不会随着连接人数的上升而降低性能,增、删、查、改的复杂度永远都是恒定的O(1)。3、服务器底层封装没有使用任何第三方网络库以及任何第三方插件,自由度非常的高,出了任何BUG,你都有办法去修改,查找问题也非常方便,在windows下使用iocp,linux下使用epoll.4、讲解c++纯客户,主要用于服务器之间通信,也就是说你想搭建多层结构的服务器服务器服务器之间使用socket通信。还可以使用c++客户做压力测试,开辟多线连接服务器,教程提供了压力测试,学员可以自己做压力测试服务器性能。5、赠送ue4和unity3d通信底层框架以及多人交互demo,登录,注册,玩家离开,同步主要是教会学员服务器客户如何交互。6、赠送c++连接mysql数据库框架demo,登录,注册,玩家离开数据持久化.7、服务器教程使用自定义通信协议,同时也支持protobuf,选择权在开发者自己手里,想用什么协议都可以,自由度高。8、服务器教程使用手动敲代码逐句讲解的方式开展教学课程。非喜勿喷,谢谢大家。9、服务器教程提供源码,大家可以在平台提供的地址下载或者联系我,服务器使用c++11部分标准,std::thread,条件变量,线程锁,智能指针等,需要学员具备一定c++知识,购买前请慎重考虑。

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

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