DEV Community

mridul037
mridul037

Posted on

๐—›๐—ผ๐˜„ ๐—•๐—ฎ๐—ฐ๐—ธ๐—ฒ๐—ป๐—ฑ ๐—”๐—ฐ๐—ฐ๐—ฒ๐—ฝ๐˜ ๐—–๐—ผ๐—ป๐—ป๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐˜€.

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:

  1. Client sends a SYN (synchronize) packet to the server.
  2. The server responds with a SYN-ACK (synchronize-acknowledge).
  3. 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)