The TCP Connection Lifecycle
Server Binds and Listens
โข The server creates a listening socket and binds it to a port.
โข It calls the listen() system call to mark the socket as passive, ready to accept connections.(example : port 3000)
๐ฆ๐ฌ๐ก ๐ค๐๐ฒ๐๐ฒ
โข When a client initiates a connection, the TCP handshake begins:
- Client sends a SYN (synchronize) packet to the server.
- The server responds with a SYN-ACK (synchronize-acknowledge).
- The client completes the handshake with an ACK.
โข Before the handshake completes, the connection is placed in the SYN queue.
โข Connections in this queue are in a half-open state (SYN received but not yet acknowledged by the client).
โข If the handshake isn't completed (e.g., due to a timeout), the entry is dropped from the SYN queue.
๐๐ฐ๐ฐ๐ฒ๐ฝ๐ ๐ค๐๐ฒ๐๐ฒ
โข Once the TCP handshake completes, the connection moves from the SYN queue to the accept queue.
โข The server can then call the accept() system call to retrieve the connection.
๐๐ถ๐น๐ฒ ๐๐ฒ๐๐ฐ๐ฟ๐ถ๐ฝ๐๐ผ๐ฟ
๐๐ต'๐ด ๐ข๐ฏ ๐ช๐ฏ๐ต๐ฆ๐จ๐ฆ๐ณ ๐ต๐ฉ๐ข๐ต ๐ด๐ฆ๐ณ๐ท๐ฆ๐ด ๐ข๐ด ๐ข๐ฏ ๐ช๐ฏ๐ฅ๐ฆ๐น ๐ต๐ฐ ๐ข๐ฏ ๐ฆ๐ฏ๐ต๐ณ๐บ ๐ช๐ฏ ๐ต๐ฉ๐ฆ ๐ง๐ช๐ญ๐ฆ ๐ฅ๐ฆ๐ด๐ค๐ณ๐ช๐ฑ๐ต๐ฐ๐ณ ๐ต๐ข๐ฃ๐ญ๐ฆ ๐ฎ๐ข๐ช๐ฏ๐ต๐ข๐ช๐ฏ๐ฆ๐ฅ ๐ฃ๐บ ๐ต๐ฉ๐ฆ ๐ฐ๐ฑ๐ฆ๐ณ๐ข๐ต๐ช๐ฏ๐จ ๐ด๐บ๐ด๐ต๐ฆ๐ฎ. ๐๐ฉ๐ช๐ด ๐ช๐ฏ๐ต๐ฆ๐จ๐ฆ๐ณ ๐ณ๐ฆ๐ฑ๐ณ๐ฆ๐ด๐ฆ๐ฏ๐ต๐ด ๐ข ๐ณ๐ฆ๐ง๐ฆ๐ณ๐ฆ๐ฏ๐ค๐ฆ ๐ต๐ฐ ๐ข๐ฏ ๐ฐ๐ฑ๐ฆ๐ฏ ๐ง๐ช๐ญ๐ฆ ๐ฐ๐ณ ๐ด๐ฐ๐ค๐ฌ๐ฆ๐ต
โข The accept() call returns a new file descriptor representing the client connection.
โข The server application uses this descriptor to read from and write to the client socket.
๐๐ผ๐ป๐ป๐ฒ๐ฐ๐๐ถ๐ผ๐ป ๐ ๐ฎ๐ป๐ฎ๐ด๐ฒ๐บ๐ฒ๐ป๐:
๐๐๐ ๐๐ช๐๐ช๐ ๐๐ซ๐๐ง๐๐ก๐ค๐ฌ๐จ:
โข If the SYN queue is full, new connection attempts are dropped.
โข Mitigation: Tune kernel parameters like ๐ฏ๐ฆ๐ต.๐ช๐ฑ๐ท4.๐ต๐ค๐ฑ๐ฎ๐ข๐น๐ด๐บ๐ฏ_๐ฃ๐ข๐ค๐ฌ๐ญ๐ฐ๐จ or use SYN cookies.
๐๐ฐ๐ฐ๐ฒ๐ฝ๐ ๐ค๐๐ฒ๐๐ฒ ๐ข๐๐ฒ๐ฟ๐ณ๐น๐ผ๐๐:
โข If the accept queue is full, new connections are ignored or reset.
โข Mitigation: Increase the backlog size in the listen() call and adjust net.core.somaxconn.
๐๐ถ๐น๐ฒ ๐๐ฒ๐๐ฐ๐ฟ๐ถ๐ฝ๐๐ผ๐ฟ ๐๐
๐ต๐ฎ๐๐๐๐ถ๐ผ๐ป:
โข The system has a limit on open file descriptors (ulimit -n or /proc/sys/fs/file-max).
โข Exceeding this limit prevents new connections.
โข Mitigation: Increase the descriptor limit for the process.
Top comments (0)