Transport Layer Protocols
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the most prominent transport layer protocols. The transport layer (layer 4 of the OSI model) is responsible for creating and maintaining connections between two endpoints. Data payloads are encapsulated by the transport layer into segments with the necessary TCP or UDP header information. Once the payload is encapsulated by layer 4 it is then progressed to the network (layer 3) for further IP header addition.
TCP and UDP are used by various applications where data needs to be moved across the internet or internal network. TCP is primarily used for applications that require reliable transmission of data, whereas UDP is used where some data loss is much less impactful (such as streaming a video or music).
Ports
A key component of the layer 4 transport protocols is the use of source and destination port numbers. These port numbers can identify an application or service, or be used dynamically by the client/server to identify TCP flows or communications between devices. The purpose of the port numbers are to ensure that the data reaches the destined application. For example, FTP (File Transfer Protocol) uses TCP/UDP port number 21. You can view a list of registered port numbers here: IANA Port Numbers. In total, there are 65535 ports that can be used. These port numbers are split into three groups, as below. Note that dynamic ports are also referred to as ephemeral ports.
A user wants to access a website hosted on a webserver using HTTPS. In order for the webserver to know which application is required, destination port number 443 is used. The user's device will generate a random ephemeral source of say 59823. Any return traffic will have a destination port of 59823, and a source port of 443 from the webserver. Let's take a basic view of the layer 4 and 3 headers within a PDU (Protocol Data Unit) per the below for this HTTPS communication.
TCP Header
Below is a simplified view of a TCP header, found within an extract of a PDU (Protocol Data Unit). Each portion of the TCP header serves a purpose, read more below.
TCP Handshakes (Start and Finish)
Because TCP is a connection oriented protocol, handshakes are required to bring the connection stream up before data can be transmitted. To begin a connection, a 3-way handshake is used while a 4-way handshake is used to end the connection. See below:
UDP Header
The UDP header is much smaller and a smaller feature set as compared to TCP, due to it being a connectionless transport protocol. The total size of a UDP header is 8 bytes.
TCP vs. UDP
There are many differences between TCP and UDP of which the key ones are outlined below.