protocol tcp
protocol tcp
TCP (or Transmission Control Protocol for short) is another one of these rules used in
networking.
This protocol is very similar to the OSI model that we have previously discussed in room
three of this module so far. The TCP/IP protocol consists of four layers and is arguably
just a summarised version of the OSI model. These layers are:
• Application
• Transport
• Internet
• Network Interface
Very similar to how the OSI model works, information is added to each layer of
the TCP model as the piece of data (or packet) traverses it. As you may recall, this
process is known as encapsulation - where the reverse of this process is decapsulation.
One defining feature of TCP is that it is connection-based, which means that TCP must
establish a connection between both a client and a device acting as a
server before data is sent.
Because of this, TCP guarantees that any data sent will be received on the other end.
This process is named the Three-way handshake, which is something we'll come on to
discuss shortly. A table comparing the advantages and disadvantages of TCP is located
below:
TCP packets contain various sections of information known as headers that are added
from encapsulation. Let's explain some of the crucial headers in the table below:
Header Description
Source Port This value is the port opened by the sender to send the TCP packet
from. This value is chosen randomly (out of the ports from 0-65535
that aren't already in use at the time).
Destination Port This value is the port number that an application or service is
running on the remote host (the one receiving data); for example, a
webserver running on port 80. Unlike the source port, this value is
not chosen at random.
Source IP This is the IP address of the device that is sending the packet.
Destination IP This is the IP address of the device that the packet is destined for.
Sequence Number When a connection occurs, the first piece of data transmitted is
given a random number. We'll explain this more in-depth further
on.
Acknowledgement After a piece of data has been given a sequence number, the
Number number for the next piece of data will have the sequence number +
1. We'll also explain this more in-depth further on.
Data This header is where the data, i.e. bytes of a file that is being
transmitted, is stored.
Next, we'll come on to discuss the Three-way handshake - the term given for the process
used to establish a connection between two devices. The Three-way handshake
communicates using a few special messages - the table below highlights the main ones:
1 SYN A SYN message is the initial packet sent by a client during the
handshake. This packet is used to initiate a connection and synchronise
the two devices together (we'll explain this further later on).
2 SYN/ACK This packet is sent by the receiving device (server) to acknowledge the
synchronisation attempt from the client.
3 ACK The acknowledgement packet can be used by either the client or server
to acknowledge that a series of messages/packets have been
successfully received.
4 DATA Once a connection has been established, data (such as bytes of a file) is
sent via the "DATA" message.
5 FIN This packet is used to cleanly (properly) close the connection after it has
been complete.
# RST This packet abruptly ends all communication. This is the last resort and
indicates there was some problem during the process. For example, if
the service or application is not working correctly, or the system has
faults such as low resources.
The diagram below shows a normal Three-way handshake process between Alice and
Bob. In real life, this would be between two devices.
Any sent data is given a random number sequence and is reconstructed using this
number sequence and incrementing by 1. Both computers must agree on the same
number sequence for data to be sent in the correct order. This order is agreed upon
during three steps:
1. SYN - Client: Here's my Initial Sequence Number(ISN) to SYNchronise with (0)
2. SYN/ACK - Server: Here's my Initial Sequence Number (ISN) to SYNchronise with
(5,000), and I ACKnowledge your initial number sequence (0)
3. ACK - Client: I ACKnowledge your Initial Sequence Number (ISN) of (5,000), here
is some data that is my ISN+1 (0 + 1)
TCP Closing a Connection:
Let's quickly explain the process behind TCP closing a connection. First, TCP will close a
connection once a device has determined that the other device has successfully
received all of the data.
Because TCP reserves system resources on a device, it is best practice to
close TCP connections as soon as possible.
To initiate the closure of a TCP connection, the device will send a "FIN" packet to the
other device. Of course, with TCP, the other device will also have to acknowledge this
packet.
Let's show this process using Alice and Bob as we have previously.
In the illustration, we can see that Alice has sent Bob a "FIN" packet. Because Bob
received this, he will let Alice know that he received it and that he also wants to close the
connection (using FIN). Alice has heard Bob loud and clear and will let Bob know that
she acknowledges this.