The OSI Model and the Transport Layer
The Transport Layer (Layer 4) is the crucial link between the application and the network. Its job is to ensure that data is delivered reliably (TCP) or quickly (UDP) between specific applications (identified by port numbers).
Protocol 1: Connectionless (UDP Concepts)
A connectionless protocol is fast but unreliable. Data is sent as individual datagrams without confirmation of receipt. This is ideal for time-sensitive data like video frames or game state updates.
Simple UDP Header (Conceptual):
Field | Size (Bytes) | Purpose |
---|---|---|
Source Port | 2 | The application sending the data. |
Dest Port | 2 | The application receiving the data. |
Length | 2 | The size of the payload. |
Checksum | 2 | Error check for the datagram data. |
The Port Concept: The port number (e.g., 80 for HTTP, 23 for Telnet) allows the kernel to direct the incoming data packet to the correct running application (process) rather than just the machine.
Protocol 2: Connection-Oriented (TCP Concepts)
A connection-oriented protocol is reliable but slower. It ensures that every packet arrives and arrives in the correct order. This is vital for transferring files or managing terminal sessions.
Key Mechanisms for Reliability:
- Sequence Numbers: Every byte sent is numbered. This allows the receiver to reorder packets that arrive out of sequence.
- Acknowledgement (ACK): The receiver sends an ACK back to the sender, confirming which sequence number was successfully received. If the sender doesn’t receive an ACK within a timeout period, it retransmits the data.
TCP State Machine (Simplified): TCP requires a state machine (Part 17) to manage the connection life cycle, including SYN (synchronize) for starting the connection, ACK, and FIN (finish) for closing.
The Transport Checksum
Both TCP and UDP rely on a checksum to ensure data integrity.
Checksum Logic: A complex assembly routine calculates the checksum (usually the one’s complement sum of all 16-bit words in the packet header and payload). The sender includes the checksum, and the receiver recalculates it. If the receiver’s result is not zero, the packet is corrupted and must be discarded (UDP) or retransmitted (TCP).
Optimization: The Z80 is slow at 16-bit addition, so the checksum routine is one of the most heavily optimized parts of the network stack, often relying on `ADC HL, …′ routines (Part 8) to perform fast multi-byte summation.