Journey of Packets: Layers of the Protocol Stack




Overview

This article is going to talk about the hardware or underlying aspects of Web Development. In the previous article, we discussed about the Servers and the way they connect with each other to form the internet. A beginner Web developer tends to ignore these basic underlying principles of web, however these are crucial buliding blocks of the internet.

The Protocol Stack


Image
You have heard many times, and I have mentioned a lot of times too in the previous articles, things like: "Sending an HTTP request", "Receiving a response from the Server" etc. Ever wondered how these requests and responses actually transfer over the network. Like, seriously there isn't a teleport mechanism which would teleport your requests from the Client to the Server. Then, how do these information travel. Firstly, information over the network travels in the form of quantized units of data called "Data Packets". Information is broken down into a large number of data packets, which are further processed over the network. Now, we'll consider an example: Suppose you want to transfer a message - "HELLO" from your computer to another computer on the same network. We'll see how this is accomplished:

  • Firstly you need to obviously have an internet connection which connects you to the Internet. Let's say you are connected through a phone line connection.
  • Now, your message has to travel a few steps down in order to reach the phone line of your computer. What's more? These steps are collectively known as the "Protocol Stack".
  • First Step is the First Layer of the Protocol Stack, the "Application Layer". "Application Layer" sits on the top of the protocol stack. This layer consists of protocols which are utilized by the Client, like- HTTP Protocol, E-Mail, File Transfer Protocol (FTP) etc. So, basically we were able to create our message "HELLO" at the Application Layer, using the HTTP protocol. In other words, you may say that, Client is in direct touch with the Application Layer. After initiating our message, our message is broken into chunks or data packets, as mentioned earlier. These packets further move down to the next Step,
  • Second Step or Second Layer is the "TCP Layer" TCP stands for Transmission Control Protocol. Before TCP, we'll talk a bit about Ports. Basically, Ports link to a service on the computer. For e.g. there is a port for an e-mail service (PORT 25). So, if I wish to send an e-mail, the data packets should reach the PORT 25 of the target computer in order for my e-mail to reach the target computer. Similarly, Default port for HTTP is 80. So, our message "HELLO" should reach PORT 80 of the target computer as we had initiated an HTTP request. How would the data packets know where they need to go? Well, here comes TCP to rescue. As the data packets reach the TCP Layer, they are assigned a PORT Number according to their Request types. TCP assigns a lot of useful information to the data packets like- Source PORT number( the port from which the request was initiated), Destination PORT number( the port where the data packets need to reach), and other useful information like- header length, checksum, acknowledgement etc. So, you can say that TCP disguises the Packets in a way that such that they are not lonely on the destination computer XD, packets are made aware of who they need to contact at the destination computer. After all this, packets further move down to the next step.
  • IP Layer: Third destination is the IP layer. As we saw previously, TCP assigned port numbers to all the packets, but one thing is still left out for the complete address of the destination where the packets have to reach. Just like, your home address has a House Number, a Street Number, and a landmark, similarly packets also have a destination address which is made up by the TCP and IP layer. We know that IP address is allocated to any computer on a network, hence the packets must know which computer they need to reach, this job is done by the IP layer as it assigns the IP address of the destination computer along with other useful information like the Source IP, and other options. Now, our packets are ready with full addresses of their destination.
  • Hardware Layer: The data packets are ready with all the necessary information. Now, they need a medium to travel to the destination computer. This is what the hardware layer does. Hardware Layer consists of the Network adapter, modem, Router, telephone line that your ISP provided you to connect to the internet. The hardware layer converts the data packets into machine level code format i.e. the binary format. Binary format is the heart of any digital device. Information is carried in the form of 0s and 1s. Hence, now the data packets are converted to binary format and carried along the various connections to the destination computer. On the Client computer i.e. the computer which initiates the request, the modem encodes the code into binary, while at the Receiver's end, the modem decodes the binary to provide useful information.
After all the above steps, the data packets have finally reached the destination computer, and guess what? The protocol stack of the destination computer is waiting to process the request. The difference is instead of Processing from top to bottom as in the previous case, the receiver computer processes the request from bottom to top. Following steps take place:
  • Hardware Layer: This was the last layer in the previous case (from where the request was initiated). But, at the receiver's end, this is going to be the first layer to process the incoming data packets. Remember the packets were encoded to machine language at the sender's side. Now, these packets are decoded at the here at the receiver's side, so that the packets are once again human readable.
  • IP Layer: This layer simply identifies the IP address of the computer which sent the data packets. As you have read above, the data packets were assigned an IP address at the sender's end.
  • TCP Layer: This layer identifies the port to which the request has to transferred to. For e.g. an HTTP request would to be directed to PORT 80 and so on. Again, Port numbers were assigned in the TCP layer at the sender's end.
  • Application Layer: Finally, the data packets once again combine together to form a continuous stream of data, which is sent to the Receiver's Browser or the required Application.
So, you saw how the protocol stack works in a reversed order at the Receiver's end, and how it works at the sender's end. This was a basic implementation of the journey of requests/response objects in the form of data packets from on computer to another. Keep in mind that the Internet is a vast collection of computer connected with each other, so a typical request has to go through a number of network devices like a router, switch etc. in order to reach its final destination. If you enjoyed reading this and gained something from this article, a share of the same would make my day, Thanks.

Let's Connect

We can always have a word over call or e-mail to discuss whatever you are looking for.