Basics of Layer 4
Layer 4 of the OSI model, also known as the Transport Layer, is responsible for the transparent transfer of data between end hosts, ensuring that communication is reliable and orderly.

Key Functions:
- Reliable Data Transfer: Ensures data is delivered accurately and in the correct order.
- Error Recovery: Detects and retransmits lost or corrupted data.
- Data Sequencing: Organizes data packets in the correct sequence.
- Flow Control: Manages the rate of data transmission to prevent overwhelming the destination.
- Layer 4 Addressing (Port Numbers): Identifies the specific application layer protocol. It does not refer to physical interfaces or ports on network devices.
- Session Multiplexing: Allows multiple sessions to coexist on the same device.
What is a Session?
A session is an exchange of data between two or more communicating devices. Each session can be identified by unique port numbers, and these ports are categorized as follows:

Port Ranges:
- Well-Known Port Numbers: 0 - 1023 (e.g., HTTP, FTP)
- Registered Port Numbers: 1024 - 49151 (e.g., applications registered with IANA)
- Ephemeral/Private/Dynamic Port Numbers: 49152 - 65535 (e.g., temporary ports for client applications)

TCP (Transmission Control Protocol)
TCP is a connection-oriented protocol, meaning it requires a connection to be established between two hosts before data can be sent. This ensures a reliable and ordered data exchange.

Connection Establishment:
- Three-Way Handshake:
- SYN: Host A sends a SYN (synchronize) packet to Host B.
- SYN-ACK: Host B responds with a SYN-ACK (synchronize-acknowledge).
- ACK: Host A sends an ACK (acknowledge), and the connection is established.

Connection Termination:
- Four-Step Process:
- FIN: Host A sends a FIN (finish) to Host B.
- ACK: Host B acknowledges with an ACK.
- FIN: Host B sends a FIN.
- ACK: Host A sends an ACK, closing the connection.

Key Features:
- Reliable Communication: TCP ensures the destination host acknowledges receipt of each TCP segment (Layer 4 PDU). If a segment isn’t acknowledged, it is retransmitted.

- Data Sequencing: Sequence numbers in the TCP header allow the destination host to reorder segments that arrive out of sequence.

- Flow Control: The destination host can instruct the source host to adjust the rate of data transmission to prevent buffer overflow.

UDP (User Datagram Protocol)
UDP is a simpler, connectionless protocol, where data is sent without establishing a connection. This means UDP does not guarantee reliable communication.

Key Characteristics:
- No Connection Establishment: Data is sent without prior communication between the sending and receiving hosts.
- No Reliability: There are no acknowledgments for received segments, and lost segments are not retransmitted.
- No Sequencing: UDP has no mechanism for ordering segments, so out-of-order segments cannot be corrected.
- No Flow Control: UDP does not regulate the rate of data transmission.
- Error Checking: UDP includes a checksum for basic error detection, but it does not provide the robust error recovery features of TCP.
Comparing TCP and UDP
Header Fields:

- TCP includes more features than UDP, providing reliable, ordered, and error-checked delivery at the cost of additional overhead.
- UDP is simpler and faster, making it ideal for applications that can tolerate some data loss, such as streaming audio or video.
Application Use:
- TCP: Preferred for applications that require reliable communication, such as file transfers.
- UDP: Preferred for real-time applications where speed is critical, and occasional data loss is acceptable.
- Mixed Use: Some applications use both TCP and UDP depending on the context, offering flexibility.

Important Port Numbers
Some common and critical port numbers used by TCP and UDP include:
