当保持活动消息未收到响应时,我正在尝试实现 TCP SO_KEEPALIVE 以关闭并重新连接我的连接。我的问题是我对此没有任何运气,我认为(希望)这是因为我没有正确实施它。下面是我如何实现这一点的示例。
//var to re-set socket's timeout value
struct timeval timeout;
//creates a variable for KEEPALIVE's optval parm
int optval;
//creates a variable for KEEPALIVE's optlen parm
socklen_t optlen = sizeof(optval);
// sets KEEPALIVE parms
optval = 1;
optlen = sizeof(optval);
// turns on KEEPALIVE property on socket
if (setsockopt (Socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0)
{
CloseSocket(Socket, 0);
connect(Socket);
}