Yes, there is a timeout mechanism in place for TCP socket connections called TCP Keep-Alive. It prevents socket connections from remaining open indefinitely, addressing scenarios where one side terminates the connection without notifying the other.
TCP Keep-Alive is configurable on Linux through the following properties:
The process involves sending empty ACK packets at regular intervals until a response is received. If no response is received after a specified number of probes, the connection is terminated.
Keep-Alive packets are optional and may not be reliably transmitted. However, in practice, most operating systems and cloud providers support this mechanism.
Per Socket Configuration (Java 11 and above):
Java 11 introduced the ability to configure TCP timeouts on a per-socket level using native code.
System-Wide Configuration:
For older versions of Java and on other operating systems, you may have to apply timeout configuration at the system level.
Linux:
# Echo commands to modify the values in /proc echo 180 > /proc/sys/net/ipv4/tcp_keepalive_time echo 3 > /proc/sys/net/ipv4/tcp_keepalive_probes echo 10 > /proc/sys/net/ipv4/tcp_keepalive_intvl
Mac OS X:
# Use sysctl to modify values sysctl -w net.inet.tcp.keepidle=180000 net.inet.tcp.keepintvl=10000 net.inet.tcp.keepcnt=3
Windows:
Registry key: HKEY_LOCAL_MACHINESystemCurrentControlSetServicesTCPIPParameters
The above is the detailed content of How Does TCP Keep-Alive Prevent Stale Socket Connections?. For more information, please follow other related articles on the PHP Chinese website!