The transport layer uses reliable and unreliable connections to transfer data between two devices in a network. A reliable connection provides guaranteed data delivery, but it takes more time for data delivery than an unreliable connection. An unreliable connection delivers data faster than a reliable connection, but it does not provide any guarantee for data delivery.
Reliable connections
For reliable connections, it uses TCP (Transmission Control Protocol). The TCP protocol uses a three-way handshake, windowing, and sequence numbers for guaranteed data delivery.
Let us take an example to understand the meaning of each technique and how TCP uses it for guaranteed data delivery.
A user on PC-1 wants to transfer some files to a user on PC-2. Since these files require guaranteed data delivery, the transport layer on PC-1 implements the TCP protocol and delegates this responsibility.
The three-way handshake process
Before transmitting the data to PC-2, TCP ensures that PC-2 is up and ready to receive data. For this, it uses the three-way handshake process. The following steps are involved in this process.
The source (PC-1) sends a SYN (synchronization) segment to the destination (PC-2). This segment indicates PC-1 wants to establish a reliable session for data transfer.
PC-2 responds with a segment that includes acknowledgment and synchronization. The acknowledgment indicates the successful receipt of the source’s SYN segment. The SYN flag indicates the destination is ready to establish the connection.
Upon receiving the SYN/ACK segment, PC-1 responds with an ACK segment. This indicates to PC-2 that its SYN was received by PC-1 and that the session is now fully established.
After going through the three-way handshake process, both PCs can transfer data across the session. TCP goes through this process whenever it transfers data. In this process, it also negotiates parameters it will use in data transmission.
Windowing
Windowing is the number of segments the source can send before getting an acknowledgment from the destination. For example, if the window size is 4, PC-1 can send four segments before getting an acknowledgment from PC-2. After that, it must wait for an acknowledgment from PC-2. It will send the next four segments only when it will receive an acknowledgment from PC-2 for the sent segments.
Sequencing
To track each segment, TCP attaches an incremental number to each segment. The destination device uses these numbers to tell the source device which segments it received and which segments are lost in the transmission.
Let's take an example.
Suppose the window size is 4. PC-1 sends four segments whose sequence numbers are 40, 41, 42, and 43.
PC-2 receives all segments and send an acknowledgment indicating it received segment number 40, 41, 42, and 43 and is ready for the next four segments.
PC-1 sends the next four segments whose sequence numbers are 44, 45, 46, 47. Let's suppose this time segment 46 is lost in the middle.
PC-2 sends an acknowledgment indicating it received segment numbers 44, 45, and 47 and is ready for the next four segments.
Since PC-1 does not get the acknowledgment for segment number 46, it assumes that PC-2 did not receive it. It resends segment 46 in the next batch of four segments. The next time it sends the segment numbers 46, 48, 49, and 50.
This way using the sequence numbers and acknowledgment signals, TCP provides guaranteed data delivery.
Unreliable Connections
The main problem with a reliable connection is that to establish it, TCP always goes through a handshake process before data can be transferred, and then acknowledge that all data sent was received.
If you want to send only one piece of information and get a single reply back, going through a handshake process is worthless. It adds overhead and delays.
For example, a device uses a DNS query to resolve a fully qualified domain name to an IP address. The device sends the single DNS query to a DNS server and waits for the server’s response.
In this process, since only two messages are generated and exchanged (the client’s query and the server’s response), it makes no sense to establish a reliable connection first before sending the query.
Instead of it, the device can just send its query and wait for a response. If a response doesn’t come back, the device can send the query again.
Sending data without establishing a connection is called connectionless data delivery.