Overridable Notification FunctionsOnClose Notifies a socket that the socket connected to it has closed. OnConnect Notifies a connecting socket that the connection attempt is complete, whether successfully or in error.
TO: btbq(阿不) GetSockOptCall this member function to retrieve a socket option. GetSockOpt retrieves the current value for a socket option associated with a socket of any type, in any state, and stores the result in lpOptionValue. Options affect socket operations, such as the routing of packets, out-of-band data transfer, and so on. SELECT 能得到TCP连接的信息,但不能得到如拔网线等异常信息,若正常关闭,则SELECT可以得到CLOSE的消息。 像你的情况,客户端是不能改的了,那就好好考虑一下系统传输的特性。比如,若CLIENT端超过5秒钟没有数据过来,则认为是断线。 像硬件的客户端,一般都是联了服务器就不停发数据,若服务器几秒钟都收不到,肯定出问题了。 正常关闭的话会受到SOCKET CLOSE的消息。
OnConnect Notifies a connecting socket that the connection attempt is complete, whether successfully or in error.
2、非正常断线一般采用所谓的“心跳协议”,简单又实用。比如客户端一秒时间内发送一次在线信号。服务端设置一个变量i = 5 每隔1秒收不到信号就--i,如果i=0表示断线。如果收到信号将i=5,这样5秒为断线等待时间。
Client和Server定时握手
谁知道SELECT()怎么用,是不是那个可以做到我的要求?麻烦大家告诉我。
SELECT 能得到TCP连接的信息,但不能得到如拔网线等异常信息,若正常关闭,则SELECT可以得到CLOSE的消息。
像你的情况,客户端是不能改的了,那就好好考虑一下系统传输的特性。比如,若CLIENT端超过5秒钟没有数据过来,则认为是断线。
像硬件的客户端,一般都是联了服务器就不停发数据,若服务器几秒钟都收不到,肯定出问题了。
正常关闭的话会受到SOCKET CLOSE的消息。
就像你的OnReceive()一样,CAsyncSocket、CSocket有一虚函数OnClose(),当该函数激活时就是连接断开时。当OnClose()被激活时用一个数据成员记录即可,其他操作前可以先检测该记录值。