🧠Concept of Socket and Listening Mechanism
- In Linux, a socket is a special type of file
file descriptor
used for inter-process communication (IPC) or network communication. It acts as an endpoint for sending or receiving data between processes, either on the same system or across different systems over a network. Sockets provide a standardized way for programs to exchange data, making them a fundamental concept in networking and system programming. - When a Process listens on an IP/Port it produces a socket.
🧵 Connection Lifecycle and Queues
- A socket transitions to a connection through the TCP three-way handshake:
SYN, SYN-ACK, and ACK
. Two key kernel-level queues are introduced:- Socket SYN Queue:
- What it is:
- Also called the
incomplete connection queue
. - Holds incoming TCP connection requests that are in the initial stage of the TCP three-way handshake.
- Holds half-open connections awaiting handshake completion.
- It stores connections that have received a
SYN
packet from a client but haven’t completed the handshake yet.
- Also called the
- How it works
Three Way Handshake
:- A client sends a
SYN
packet to the server to initiate a connection. - The server responds with a
SYN-ACK
packet and places the connection in the SYN Queue. - The connection remains in this queue until the client sends an
ACK
packet to complete the handshake.
- A client sends a
- What it is:
- Socket Accept Queue: Stores fully established connections ready for backend processes to accept using the
accept()
system call.- What it is:
- Also called the
completed connection queue
. - Holds fully established TCP connections that have completed the three-way handshake
SYN, SYN-ACK, ACK
. - It stores complete connections and are waiting for the application to accept them via the
accept()
system call.
- Also called the
- How it works:
- Once the client sends the final
ACK
, the connection moves from the SYN Queue to the Accept Queue. - The server’s application (e.g., a web server) calls
accept()
to dequeue a connection and begin processing it.
- Once the client sends the final
- What it is:
- Socket SYN Queue:
⛵ Connection Queue
- Once the connection is established, it's represented in the kernel by a file descriptor.
- This file descriptor holds the state and metadata of the connection and lives in the accepting process’s memory space (PCB).
- Receive Queue:
- The receive queue is part of the kernel memory space associated with an individual connection.
- It temporarily stores incoming data from the client before it's passed to the application (e.g., your server).
- If the backend does not read fast enough, the receive queue fills up, leading the kernel to trigger flow control that tells the client to slow down.
- Send Queue:
- The send queue stores outgoing data from the server that will be transmitted to the client.
- When the application writes data (e.g., API response), it's placed here first—not sent immediately.
- The kernel may delay actual transmission to optimize performance, bundling small writes into larger packets before flushing them to the network.
- Receive Queue:
Top comments (0)