Computer Networks simplified!
Tags: computerscience, programming, beginners
Published: 2022-01-22 06:38:54

## Have you ever wondered how data is transferred from one device to another?

Well, to answer this question we need to understand computer networks. In this blog, we will briefly discuss the entire process in one go.

Before we get into any lower-level details, let's first get an overview of the process, and we'll dwell deep later.

## The OSI reference model
This is a proposed conceptual model that characterizes communication functions. 

### The 7 proposed layers:

1. Application Layer
2. Presentation Layer
3. Session Layer
4. Transport Layer
5. Network Layer
6. Data-link Layer
7. Physical Layer

Again, this is a proposed layer. Real communication models(like TCP/IP model) may not have all the layers, or more interestingly some layers combined to form one.

### 1. Application Layer
Let's first understand what protocol means before talking about the application layer.

**Protocol: **
The protocol is a set of rules agreed upon by all parties for some process. Once a protocol is proposed, parties use the same defined steps to perform a task.

In the application layer, there are a bunch of protocols that are used for various tasks.

For example, the Gmail application may be using SMTP(Simple Mail Transfer Protocol) to send and receive emails, and all this happens at the application level(here the application is Gmail or it can be your browser). So, this protocol must be supported by the application itself.
The same concept applies to HTTP and HTTPS(which are already baked into browsers).

Some more examples of protocols in the layer: SMTP, HTTP, HTTPS, Telnet, RDP, etc.

### 2. Presentation Layer
Some very important services like translation, data compression, encryption, etc are provided by this layer. 

Data after going through specified steps in protocols from the application layer undergo the below steps in the presentation layer now.

**Translation**
Data that is in text format(Unicode or utf-8) is converted to binary form.

**Compression** 
Binary data from the above steps are compressed algorithms like Huffman encoding.

**Encryption** Encryption of secret data(like passwords, SSL certificates) is part of this process. Symmetric and asymmetric encryption protocols act here.

... and many more, there are tons of other things which happen here but these were the important ones.

A lot of fancy buzz happens at the presentation layer. Perfect naming for the layer, lol!

### 3. Session Layer
Establish, Manage, and terminate session connections between end-user applications.

More importantly, things like token management, synchronization, authentication, authorization are the responsibilities of this layer.

We can say, this layer is important for security and synchronization.

Note: In a TCP/IP model, the browser bake in all the above three layers functionalities into just one layer called the application layer.

### 4. Transport Layer
**Segmentation**: 
Data is broken down into segments(small pieces) and sent over the network.

- **Flow Control**:
Devices may have different networking capabilities and maybe one device can operate faster(like send data at 100mbps) than the other(10mbps). Well, the transport layer takes care of this problem. We will explore how later.

- **Error Control**: 
While transferring segments, some segments may get lost! Again, the transport layer got our back. It uses checksums to find such errors in transmission.

- **Port numbers**: 
The segments may get confused when they reach the destination device as they don't know their destination application. To avoid this confusion, we assign port numbers to segments before sending them from the source. So when a segment reaches a destination device it knows which application it should go to.

Finally, a segment looks like this 
Segment = (source port + destination port + sequence number + acknowledgment + length) 
Wireshark example: Src Port: 6648, Dst Port: 443, Seq: 24, Ack: 251, Len: 23

### 5. Network Layer
After segments are made we attach some more information to the segment, this includes the source IP and destination IP.

Just like the port number which helps segments to reach a particular application on the destination device, IP addresses help reach a particular device on the network.

(segment + source and destination IP) together is called a packet. An IP address is just 32 bits which can uniquely determine about 4294967296 devices, but there are more devices than that number on the internet and that's where IPV6 pitches in.

**IP(Internet Protocol)** is again just unique over a local network but not on the whole internet. Just remember this, we will know more about it in further blogs.

Routing, DNS, Masking are also functionalities provided by this layer.

**Routing**: helps in the path determination of packet. i.e. how the packet should reach from source to destination and what routes/hops it should take over the network.

**DNS**: it is a map of key-value pairs. example: www.google.com(key):192.168.32.3(value). Instead of us remembering all the IP addresses of different websites, we have DNS to rescue. For example, www.google.com is resolved to an IP from the DNS server when we make a request.

### 6. Data Link Layer
As we already discussed IP addresses are just unique over the network but not over the internet, we need one more level of uniqueness(remember there are millions of devices!). We use the MAC address which is 48 bits! and every device which has networking capabilities is manufactured with its unique mac address on the entire internet. phew!!

Now finally, we have something we call a frame, a combination of packet + source and destination mac addresses + some extra tail data(like some more checksum to avoid data loss at this level).

some more functionalities provided by this layer are:

- **Access to media**: Determination of media for transfers (wired, wireless, optical, etc)

- **Media-Access Control**: avoiding collision over the media.

- **Error detection**: tail data is used to perform checksum here.


### 7. Physical Layer:
Bit-level data transfer happens at this layer, i.e bits of frames are converted into some form of signals. Ex: light flashes or radio signals and then transmitted. 

We won't discuss much of the physical layer as I don't think it is that important for Software Engineering. If you are interested in exploring, be my guest.

**Transmission Media**: What media to use to transport data?
  - _Guide_: There is a physical medium like wires(co-axial, twisted pair cables) on which transmission takes place.

  - _Unguided_: Medium of transfer is air, examples are Wifi, infrared and radio signals.

**Transmission Modes**:
When two devices are connected over a medium we can have three modes of transmission
  - _Simplex_: Data can flow only in one direction.

  - _Half-duple_: Data can flow in both directions but not at the same time.

  - _Full-duplex_: Data can flow in both directions and simultaneously

I would highly recommend you to read about Manchester encoding and differential Manchester encoding to get an understanding of how signals can carry bit information.

Data is processed sequentially from the application layer to the physical layer and then sent over to another device. Now data is decodedbackwardsd. i.e data is decoded from the physical layer to the application layer. 

It is just like amazon delivery, packing the product and delivering it to the customer and when the customer receives the product they unbox just relate and you will be surprised. We will dive deep soon, hopefully! See you again!

Visit my blog at: https://rahilrehan.github.io/ 🙈
And follow me on Twitter: https://twitter.com/rahil_rehan