如何处理KEEPALIVE事件?

问题描述 投票:-1回答:1

我有一个客户端 - 服务器应用程序,我在服务器端设置了keep alivce套接字选项。我理解保持活动的概念,但我应该如何处理保持活动事件,保持活动机制是否在另一端重新确认问题,并且我想关闭连接到该客户端的套接字。我查看了WSAIoctl函数,但没有真正理解它是否可以解决我的问题。

我在网上得到了这个例子:

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 );

我知道这是一个非常理论化的问题,但我真的没有任何相关的代码片段。所以,我的问题是我应该如何处理连接失败,就像另一端被拒绝一样。

c++ sockets winsock keep-alive winsock2
1个回答
3
投票

你没有。没有“保持活动”这样的事情。 TCP为您完成所有工作。如果keepalive定时器超时,则重置连接。

© www.soinside.com 2019 - 2024. All rights reserved.