Skip to content

Instantly share code, notes, and snippets.

@fffaraz
Created June 12, 2015 06:55
Show Gist options
  • Select an option

  • Save fffaraz/fbcf45308ee57e898657 to your computer and use it in GitHub Desktop.

Select an option

Save fffaraz/fbcf45308ee57e898657 to your computer and use it in GitHub Desktop.

Revisions

  1. fffaraz created this gist Jun 12, 2015.
    16 changes: 16 additions & 0 deletions tcpKeepAlive.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    #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));