Junior
How does TCP work?
sobes.tech AI
Answer from AI
TCP provides reliable, connection-oriented delivery of data in IP networks.
Key stages and mechanisms:
-
Connection establishment (Three-way Handshake):
- The client sends a SYN packet with an initial sequence number (ISN).
- The server responds with a SYN-ACK packet, confirming receipt of SYN and sending its ISN.
- The client sends an ACK packet, confirming receipt of SYN-ACK.
-
Data transfer:
- Data is segmented.
- Each segment has a header with a sequence number and checksum.
- The receiver sends an acknowledgment (ACK) upon receiving a segment, indicating the next expected segment number.
-
Flow control:
- The receiver informs the sender of the available buffer size (window size).
- The sender does not send more data than the receiver's window size, preventing buffer overflow.
-
Error control:
- Checksums are used to verify segment integrity.
- If a segment is lost or corrupted, the receiver does not send an ACK for it. The sender retransmits the segment after a timeout.
-
Congestion control:
- Mechanisms to prevent network congestion (e.g., Slow Start, Congestion Avoidance, Fast Retransmit, Fast Recovery).
- The window size dynamically adjusts based on network conditions (packet loss, RTT).
-
Connection termination (Four-way Handshake):
- One side sends a FIN packet indicating it will not send more data.
- The other side acknowledges the FIN (ACK) and may continue sending its data.
- When the other side is ready, it sends its FIN packet.
- The first side acknowledges the FIN (ACK).
Differences from UDP:
| Characteristic | TCP | UDP |
|---|---|---|
| Orientation | Connection establishment | Connectionless |
| Reliability | High (guaranteed delivery, order) | Low (no guarantees) |
| Error control | Yes | No |
| Flow control | Yes | No |
| Congestion control | Yes | No |
| Speed | Lower | Higher |
| Header size | Larger (20-60 bytes) | Smaller (8 bytes) |