2

My main goal is to detect if my client connected to my server is here and responding. Hence I have developed this server socket (without error handling it is purely experimental):

int opt = 1;

memset(&myServerInfo, 0, sizeof(myServerInfo));

// AF_INER = IPV4
myServerInfo.ai_family = AF_INET;

// TCP
myServerInfo.ai_socktype = SOCK_STREAM;

// Make the socket suitable for binding 
myServerInfo.ai_flags = AI_PASSIVE;

getaddrinfo(NULL, SERVER_PORT, &myServerInfo, &currentInfo);

mySocket = socket(&currentInfo.ai_family, &currentInfo.ai_socktype, &currentInfo.ai_protocol);

setsockopt(mySocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

bind(mySocket, &currentInfo.ai_addr, &currentInfo.ai_addrlen);

listen(mySocket, MAX_PENDING_CONNECTIONS);

myDataSocket = accept(mySocket, (struct sockaddr *)&their_addr, &addr_size);

setsockopt(myDataSocket, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));

With the option byte SO_KEEPALIVE (in the setsockopt()) I want my server to ask my client continuously if he's here.

By setting this option I was wondering what is the poll() event that can trigger my poll when my client doesn't respond to my keepalives ?

Clément
  • 53
  • 6
  • 1
    The socket options are *not* bit-fields. They can't be combined, you must set them one by one. – Some programmer dude May 08 '22 at 15:09
  • 1
    Also, `SO_KEEPALIVE` makes no sense on a passive listening socket, since they don't have connections that needs to be kept alive. You should set it on the socket returned by `accept`. Furthermore, the default timeout for `SO_KEEPALIVE` checks tends to be a couple of hours. E.g. Linux have system parameters that allow you to change these timeouts, but generally speaking it's almost always better to add a keep-alive system to your application protocol. – Some programmer dude May 08 '22 at 15:11
  • This seems relevant: [**How to use SO_KEEPALIVE option properly to detect that the client at the other end is down?**](https://stackoverflow.com/questions/5435098/how-to-use-so-keepalive-option-properly-to-detect-that-the-client-at-the-other-e) – Andrew Henle May 08 '22 at 16:03
  • 1
    @AndrewHenle Yes i'have previously found this post but it wasn't answering my question – Clément May 08 '22 at 16:10

0 Answers0