# Transport Layer
## What is the Transport Layer
The Transport layer is the fourth layer of the OSI model. It provides end-to-end communication services between applications running on different hosts. While the lower layers handle getting data from one physical device to another, the Transport layer ensures that data is delivered reliably from the correct application on the source host to the correct application on the destination host. The two main protocols at the Transport layer are TCP and UDP.
## Functions of the Transport Layer
Segmentation is the process of dividing the data received from the upper layers into smaller units called segments. This is necessary because the lower layers have limits on how large a single transmission unit can be. The Transport layer adds header information to each segment including source and destination port numbers and sequence numbers.
Port numbers identify which application on a host is sending or receiving data. Just as an IP address identifies a specific device on a network, a port number identifies a specific application or service on that device. Port numbers range from 0 to 65535. Well-known ports from 0 to 1023 are assigned to standard services such as HTTP on port 80 and HTTPS on port 443.
Multiplexing allows multiple applications on the same host to use the network simultaneously. The Transport layer combines data from multiple applications for transmission and directs received data to the correct application based on port numbers.
Connection establishment and termination, in the case of TCP, involves a handshaking process before data transfer begins and a graceful termination process when communication is complete.
Reliability in TCP involves acknowledging received segments, retransmitting lost segments, and detecting and discarding duplicate segments. Sequence numbers allow segments to be reordered at the destination if they arrive out of order.
Flow control prevents the sender from overwhelming the receiver. TCP uses a sliding window mechanism where the receiver tells the sender how much buffer space it has available.
## TCP vs UDP
TCP is a connection-oriented protocol that provides reliable, ordered delivery of data. Before data transfer begins, TCP establishes a connection through the three-way handshake. TCP guarantees that all data arrives at the destination in the correct order and without errors. The overhead of connection establishment, acknowledgment, and retransmission makes TCP slower than UDP but ensures data integrity.
UDP is a connectionless protocol that does not guarantee delivery, order, or error checking. UDP simply sends datagrams without establishing a connection first. Because UDP has much less overhead than TCP, it is faster. UDP is used for applications where some data loss is acceptable and speed is more important, such as video streaming, online gaming, and DNS queries.Back to Subject