Search code examples
socketstcptimeout

Linux TCP Connection timeout


I observe the TCP timeout from server to client. When TCP Three-way Handshake finished, and then the client does not anything for a long time. How many times will timeout TCP sessions?

I consult the RFC 793 document, 3.8 Interfaces:

The timeout, if present, permits the caller to set up a timeout for all data submitted to TCP. If data is not successfully delivered to the destination within the timeout period, the TCP will abort the connection.
The present global default is five minutes

The following is the captured packet connection, More than 10 minutes have passed and there is no TCP disconnection.

Am I misunderstanding somewhere? enter image description here

OS: Ubuntu 20

The following is my test code.

Client Code:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[]){
    int socket_desc;
    struct sockaddr_in server;
    
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if(socket_desc == -1){
      printf("Socket failed\n");
    }

    server.sin_addr.s_addr = inet_addr("192.168.88.88");
    server.sin_family = AF_INET;
    server.sin_port = htons(8888);

    if(connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) <0){
      printf("Connect failed\n");
    } else{
      printf("Connected\n");
      while(0); // When connected, do not anything.
return 0;
}

Server Code:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[]){
    int socket_desc, new_socket, c;
    struct sockaddr_in server;
    
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if(socket_desc == -1){
      printf("Socket failed\n");
    }

    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_family = AF_INET;
    server.sin_port = htons(8888);

    if(bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) <0){
      printf("bind failed\n");
    }

    listen(socket_desc, 5);
    c = sizeof(struct sockaddr_in);
    new_socket = accept(socket_desc, (struct sockaddr *) &client, (socklen_t *) &c);
    if (new_socket < 0){
      printf("Accept failed\n");
    } else{
      printf("Accept\n");
      while(1); // When accept , do not anything.
    }
return 0;
}

Solution

  • There is no standardized idle timeout for TCP connections. Connections could in theory exist for days without any traffic. The idle timeout is instead specific for the application and is enforced there.

    Often applications also employ TCP keep-alive to make sure that the connection state will be kept alive by intermediate devices like stateful firewalls and NAT routers and that a silent disconnect (like system switched off, connection broken) will be detected early. This way, TCP keep-alive helps to detect a broken connection even if the connection is idle.

    ... I consult the RFC 793 document, 3.8 Interfaces:

    What you refer to has nothing to do with an idle timeout. It is the timeout when data are send but not ACKed by the peer.