Skip to content

Instantly share code, notes, and snippets.

@haithemsekri
Forked from fffaraz/tcpKeepAlive.c
Created February 12, 2023 11:43
Show Gist options
  • Select an option

  • Save haithemsekri/89ad630179c6d5208043edfc46141718 to your computer and use it in GitHub Desktop.

Select an option

Save haithemsekri/89ad630179c6d5208043edfc46141718 to your computer and use it in GitHub Desktop.
SO_KEEPALIVE
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
int enableKeepAlive = 1;
int fd = tcpSocket->socketDescriptor();
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enableKeepAlive, sizeof(enableKeepAlive));
int maxIdle = 300;
setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &maxIdle, sizeof(maxIdle));
int count = 4;
setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &count, sizeof(count));
int interval = 4;
setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment