TCP and UDP: Network Communication Protocols

TCP and UDP: Key Differences

UDP (User Datagram Protocol)

  • Data-oriented
  • Unreliable, connectionless
  • Simple
  • Supports Unicast and Multicast
  • Used by SNMP

TCP (Transmission Control Protocol)

  • Stream-oriented
  • Reliable, connection-oriented
  • Complex
  • Unicast only
  • Common Use Cases:
    • Used by most Internet applications
    • Web (HTTP)
    • Email (SMTP)

Fragmentation

DF (Don’t Fragment):

  • 1: Don’t Fragment
  • 0: Could Fragment

MF (More Fragment):

  • 1: More fragmentation needed
  • 0: No more fragmentation needed, last fragment.

All fragments of one packet share the same ID.

Offset: Data payload / 8.

  • First fragment: Fragment Offset = 0 (from the original data packet’s starting point).
  • Second fragment: Fragment Offset = (Length of the first fragment’s data / 8).
  • Third fragment: Fragment Offset = (Sum of the lengths of the first and second fragments’ data / 8), and so on.

IPv4

  • Uses 14-bit and 4-bit addresses.
  • IPv4 header minimum size is 20 bytes.
  • UDP header size is 8 bytes.

TCP

Sequence numbers are incremented as follows, depending on the type of segment that is sent. The  Comment  one that applies to this example is marked with a check:  • SYN: sender increments its sequence number by 1.  • FIN: sender increments its sequence number by 1.  • Pure ACK (no SYN or FIN, no data payload): sender does not change its sequence number.  • Data segment: sender increments its sequence number by the size of the data payload.

TCP Three-Way Handshake:

  1. First handshake: Client → Server: SYN (seq=n, mss=z, win=w)
  2. Second handshake: Server → Client: SYN (seq=m, ack=n+1, mss=k, win=h)
  3. Third handshake: Client → Server: ACK (ack=m+1)

Sequence Number: The position of a byte within the TCP data stream, relative to the beginning of the stream.

Acknowledgment Number: Indicates the sequence number of the next byte expected by the receiver.

RTTs

HwcZTrdrTB6OAAAAAElFTkSuQmCC

RTTd =

8HhnOq0VI+gmIAAAAASUVORK5CYII=

RTO

oNAxeOU8nbnbgAS3Wr7M1IYB+knclKiodOPiNr+jAr6aagS6BWpVtsfBCoep5S3O3cDkOhW25+RwjhIP5GTEg2dfkTU9mdU0E9DlUCvSLV3efv7f42JNLGyKzjeAAAAAElFTkSuQmCC

TCP Mechanisms

  • Retransmission timer (RTO): Determines how long to wait for an ACK before assuming data loss.
  • Keepalive timer: Verifies the validity of a connection after a period of inactivity.
  • 2MSL wait timer: Ensures that delayed segments are received before a socket closes.
  • Delayed ACK: Reduces overhead by sending fewer ACKs.
  • Nagle’s algorithm: Reduces overhead by sending fewer data segments.
  • Connection establishment timer: Sets a time limit for establishing a TCP connection if no response to a SYN is received.

Nagle’s Algorithm

If a full MSS worth of data is in the send buffer, or if all previously sent data is ACKed, then the contents of the send buffer will be transmitted immediately. Otherwise, the sender will wait to try and reduce overhead.

Recent TCP Updates

  • TCP CUBIC: Allows for rapid CWND growth, especially on high-BDP (Bandwidth-Delay Product) links.
  • TCP RACK: Tolerates reordering (without misinterpreting it as loss), provided packets arrive on time.
  • TCP BBR: Sends data at the bottleneck bandwidth rate, avoiding buffer filling.

TCP Stream

  1. The received messages are guaranteed to be in order.
  2. Because it’s a stream, the TCP messages received may not have complete segment divisions.

TCP Flags:

  • [.] – ACK
  • [S] – SYN
  • [S.] – SYN+ACK
  • [P.] – PUSH+ACK. The PUSH flag instructs the sending application to immediately transmit the data in the send buffer, rather than waiting to fill the buffer completely. On the receiving end, it prompts the application to process the data immediately, without further delay.

SACK (Selective Acknowledgment):

Example: Flags [.], ack 1057970282, win 6227, options [sack 1 {1057974626:1057977522}], length 0.

It acknowledges both SACK blocks and the ACK number.

  • ACK Number: Represents the last byte received in sequence (and implicitly acknowledges all preceding bytes).
  • SACK Blocks: Used to report non-contiguous blocks of data that have been successfully received.

TCP Header Fields

  • Error control: Sequence number and ACK number.
  • Indicating MSS, whether SACK is supported: Options.
  • Multiplexing/demultiplexing to the correct socket: Source and destination port number.
  • Establishing a connection: SYN flag.
  • Closing a connection: FIN flag.
  • Flow control: Window size.

Flow Control

Uses triple duplicate ACKs.

  • Timeout: When TCP detects a timeout, TCP Reno sets the CWND (Congestion Window) to its initial value (typically 1 MSS) and enters the Slow Start phase, causing a significant reduction in network throughput.
  • Triple Duplicate ACKs: When TCP Reno receives three duplicate ACKs, it performs a Fast Retransmit and enters the Fast Recovery phase. During this time, the CWND is not reset to its initial value, but rather halved, allowing for a quicker recovery from congestion.

ssthresh = max(2 * segsize, min(cwnd, awnd) / 2), cwnd = ssthresh + 3 * MSS

Slow Start and Congestion Avoidance

  • Slow Start: Increase CWND by 1 MSS and stay in slow start.
  • Congestion Avoidance: For each ACK received, increase CWND by MSS * MSS / CWND.