简体   繁体   中英

how to handle KEEPALIVE events?

I have a client-server app, and I set the keep alivce socket option on the server side. I understand to concept of the keep alive but how should I Handle the keep alive event, whether the keep alive mechanism recongnized a problem on the other end, and I want to close that socket which is connected to that client. I looked at WSAIoctl function but didn't really understood if it can solve my problem.

Iv'e got this example online :

tcp_keepalive   KeepAlive;
DWORD   dJunk;

//  Use socket level keep alive for about 5 minutes
//  Unless this is done Microsoft will not close the socket
//  in the event of a cable / VPN disconnection for 2 hours.
KeepAlive.onoff = 1;
KeepAlive.keepalivetime = 60000;
KeepAlive.keepaliveinterval = 60000;

WSAIoctl( soc, SIO_KEEPALIVE_VALS, &KeepAlive, sizeof( KeepAlive ), NULL, 0, 
&dJunk, NULL, NULL );

I know it's very theoretical question but I really don't have any related code pieces. so again, my question is how I'm supposed to handle a connection failure, like the other end is shuted down.

You don't. There is no such thing as a 'keepalive event'. TCP does it all for you. If the keepalive timer times out, the connection will be reset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM