added KEEPALIVE option to the TCP socket so that at least we are notified if the peer turns out to be non-connected

This commit is contained in:
Emiel Bruijntjes 2021-09-20 12:48:30 +02:00
parent e1e83dfba6
commit 0f596d7a6e
1 changed files with 6 additions and 0 deletions

View File

@ -120,6 +120,12 @@ private:
// turn socket into a non-blocking socket and set the close-on-exec bit
fcntl(_socket, F_SETFL, O_NONBLOCK | O_CLOEXEC);
// we set the 'keepalive' option so that we automatically detect if the peer is dead
int keepalive = 1;
// set the keepalive option
setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive));
// try to connect non-blocking
if (connect(_socket, addresses[i]->ai_addr, addresses[i]->ai_addrlen) == 0) break;