The TCP Segment
The TCP protocol units are called segments. This protocol has a handful of features that make it the ideal choice for when a reliable transmission is needed. It features a number of mechanisms, such as the three-way handshake, acknowledgement, error checking, and the sliding window.
Looking at the layout of a TCP header at the byte level, one will observe that it is broken down into the following:
Source Port: Identifies the sending port.
Destination Port: Identifies the receiving port.
Sequence Number: Used for order and reliability, it shows the first byte in data sent.
Acknowledgement Number: If ACK is set, indicates next byte expected by receiver.
Data Offset: Specifies the header length.
Reserved: Can be ignored, never used.
Flags: Indicators of data and connection state.
Window Size: Specifies bytes the sender can receive.
Checksum: Used for checking for header and data errors.
Data: This is the payload itself.
Options: Optional parameters.
TCP Three-Way Handshake
A TCP connection is established via the three-way handshake, also known as "SYN SYN-ACK ACK". This is a series of communications that occur before any payload is even sent. The sender will first initiate by sending a segment with the SYN bit set. Then, the receiver will send one in return with the SYN and ACK bits set to acknowledge the synchronization. Lastly, the sender will send an ACK as a confirmation to the receiver. At this point, the actual payload transfer can begin.
Acknowledgement Mechanism
The acknowledgement mechanism allows for checking if data sent has been received on the other end. Each time the receiver gets a segment of the data, it will send an ACK which confirms to the sender that the data sent has been received successfully on the other end. That is how TCP makes sure data has not been lost.
Sliding Window Mechanism
TCP is able to control flow through the sliding window mechanism. Once communication is established and payload is being sent, instead of waiting for each ACK, TCP is able to send multiple parts at the same time. This is defined by the window size which can scale up and down based on the bandwidth available. The sliding window mechanism allows TCP to be efficient in this manner.
The UDP Datagram
The UDP protocol units are called datagrams. This protocol is known as connectionless because it never establishes a connection with the other end. The packets are sent and the sender never knows if the data is received, or not. The upside of this is that UDP has very little overhead, which makes it great for speed.
A UDP header looks like this:
Source Port: Identifies the sending port.
Destination Port: Identifies the receiving port.
Length: Specifies the side of the datagram.
Checksum: Contains the data checksum for error detection at the receiving end.
Application Data: This is the payload.