From 0f596d7a6e5766ab60e05e437a01f58a724950b8 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Mon, 20 Sep 2021 12:48:30 +0200 Subject: [PATCH] added KEEPALIVE option to the TCP socket so that at least we are notified if the peer turns out to be non-connected --- src/linux_tcp/tcpresolver.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/linux_tcp/tcpresolver.h b/src/linux_tcp/tcpresolver.h index d4af8de..0b0801c 100644 --- a/src/linux_tcp/tcpresolver.h +++ b/src/linux_tcp/tcpresolver.h @@ -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;