Understanding IP, TCP, and UDP Packet Structures: A Quick Guide
This article explains the structure and fields of IP packets, including version, header length, TOS, total length, ID, flags, fragment offset, TTL, protocol, checksum, source and destination addresses, as well as the formats and key functions of TCP and UDP headers, highlighting their differences.
IP Packet Format
Introduction
The IP protocol encapsulates transport‑layer messages in an IP header that contains information such as the originating transport protocol, destination machine, source machine, and other useful data. IP is connection‑less, meaning it does not establish a session before sending data, which reduces overhead.
IP Header Fields
Version (0‑3 bits) : Binary value; 4 for IPv4 (0100) and 6 for IPv6 (0110).
Internet Header Length (IHL) (4‑7 bits) : Length of the header in 4‑byte units; a header without options is 20 bytes, so the value is 5.
TOS (Type of Service) (8‑15 bits) : Complex field updated several times. Sub‑fields include:
0‑2 bits: Priority
3rd bit: Delay
4th bit: Throughput
5th bit: Reliability
Last two bits: Reserved
Total Length (TL) : Total size of the packet in bytes, including header and data, up to 65 535 bytes.
Identification (ID) : 16‑bit unique identifier for each IP packet, used for reassembly of fragments.
Flags (3 bits) :
Bit 0 (Reserved): Usually 0.
DF (Don’t Fragment): 0 = can fragment, 1 = cannot fragment.
MF (More Fragments): 0 = last fragment, 1 = more fragments follow.
Fragment Offset (13 bits) : Position of this fragment relative to the start of the original packet; 0 when no fragmentation occurs.
Time To Live (TTL) (8 bits) : Decremented by one at each router hop; packet is discarded when TTL reaches 0 to prevent endless circulation.
Protocol Number (8 bits) : Indicates the encapsulated transport‑layer protocol (e.g., TCP = 6, UDP = 17, ICMP = 1).
Header Checksum (16 bits) : Used for error detection of the header.
Source Address (SA) : 32‑bit IP address of the sender.
Destination Address (DA) : 32‑bit IP address of the receiver.
Options : Optional fields for security, routing, timestamps, etc.
Padding : Added to ensure the header length is a multiple of 4 bytes.
TCP Packet Format
Purpose of TCP
TCP sits on top of IP and provides a stateful, reliable transmission service. It ensures data is delivered in order, without loss, and acknowledges receipt.
TCP Header
Key Fields
The TCP header includes source/destination ports, sequence number, acknowledgment number, data offset, reserved bits, control flags (SYN, ACK, FIN, etc.), window size, checksum, urgent pointer, and optional options.
UDP Packet Format
Introduction
UDP is a simplified version of TCP, containing only source/destination ports, length, and checksum.
UDP Header
Key Fields
Fields are source port, destination port, length (header + data), and checksum.
Summary of TCP vs UDP
TCP is connection‑oriented; UDP is connection‑less.
TCP provides reliable, ordered delivery; UDP offers faster, best‑effort delivery.
TCP incurs higher latency and resource usage; UDP is lightweight.
TCP guarantees data integrity; UDP may experience packet loss.
TCP requires more system resources; UDP requires fewer.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
