What is IP address
As we as a whole surf the web for different reasons and uses, the actual web is excessively immense. There are millions of people around the globe making requests to thousands of servers every second, thus there should be an identifier to separate the gadgets associated, and there comes the Ip address.
IP stands for "Internet Protocol," which is the set of rules governing the format of data sent via the internet or local network.
versions of IP address
There are two widely used versions of IP address
IPv4 was the first version of IP. It was deployed for production in the ARPANET in 1983. Today it is the most widely used IP version. It is used to identify devices on a network using an addressing system.
The IPv4 uses a 32-bit address scheme allowing to store 2^32 addresses, which is more than 4 billion addresses. To date, it is considered the primary Internet Protocol and carries 94% of Internet traffic. an
Each address is a string of numbers separated by periods. There are four numbers in total and each number can range between 0 and 255. An example of an IPv4 address would be: 506.457.14.512
It is the most recent version of the Internet Protocol. Internet Engineer Taskforce initiated it in early 1994. The design and development of that suite is now called IPv6.
This new IP address version is being deployed to fulfil the need for more Internet addresses. It was aimed to resolve issues that are associated with IPv4. With 128-bit address space, it allows 340 undecillion unique address space
What are port numbers
Now we know that IP address differentiate our device on the internet but there is another underlying problem that is how does the server know which application to send the data, And here the concept of port number kicks in.
Port numbers are part of the addressing information that helps identify senders and receivers of information and a particular application on the devices.
It is a 16-bit number ( i.e there are 2^16 or 65k port numbers available )
Port numbers from ( 0 -1023) are Restricted port numbers or well-known
Port numbers from \\( 1024 -49151) \\ are given to the registered users and companies
Port numbers from \\( 49152 -65535) \\are \\ available for anyone to use \\.
Here is a list of protocols and on which port they use
Transport layer
The transport layer acts as a bridge between the application process and the network layer
Imagine you live in India and want to send a courier to your friend who lives in USA.
Your first step would be to contact any courier service and had over the items you want to send by mentioning the destination address
Then the courier service in India would contact some service in USA and would send to them
Now your item has reached USA and from there the courier service there will transport to your friend home
In this scenario, the transport layer is the one between you and your regional courier service
And the layer between your regional courier service and your friends is the Network layer
This layer breaks the encrypted data from the session layer into junks called segments and tags a port number to the segment
Process to process delivery
End-to-end Connection between hosts
Multiplexing and Demultiplexing
Congestion Control
Data integrity and Error correction
Flow control
What are the protocols used in the transport layer
These are some widely used protocols used in the transport layer
TCP(Transmission Control Protocol)
UDP( User Datagram Protocol)
DCCP( Datagram Congestion Control Protocol)
Difference between TCP and UDP
TCP
It provides a reliable end-to- end byte stream over an unreliable internetwork.
TCP was designed to dynamically adapt to properties of the and many other failures of the Internet.
It is a connection-oriented protocol. Connection-orientation means that the communicating devices should create a connection before transmitting data and should close the connection after transmitting the data. The connection is created between client and server using sockets, and each socket has a socket number (address) consisting of the IP address of the host and a port number.
To set up the connection between client and server it uses (3-way handshake)
All TCP connections are full-duplex and point-to-point. Full duplex means that traffic can go in both directions at the same time.
The length of the segment Headers is of a variable length of (20-60) bytes
Demo
Lets now create a TCP server using Nodejs
execute the command npm init -y to create a package.json
To create a TCP server we need to take the help of the net module, the module provides an asynchronous network API for creating a stream-based TCP server
Import the module, it gives us the function createServer
As we discussed above that to communicate with the TCP server we need to create a socket
The createServer function creates a new Socket and immediately initiates a connection with the socket, then returns the net.Socket that starts the connection.
Now you would wonder about the 3-way handshake, which will be done internally before the socket is created
Look at the code below to see how the flow works
const net = require('net');
const server = net.createServer(socket => {
socket.write('Hurray you are connected to the server');
socket.on('data', data => {
console.log(data.toString());
})
})
server.listen(8080)
Run npm start or you can run in debug mode in vs code
Now we created a server that will listen on port 8080, but how to talk to the server?
For that we use the Telnet client which can be installed using simple commands, you can refer to google for installing
Make sure your server is up and running and waiting for a client to connect.
Now open a new terminal and execute the following command
❯ telnet 127.0.0.1 8081
After executing the command you will see the following output.
❯ telnet 127.0.0.1 8080
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Hurray you are connected
We can see that it trying to connect to the server and after that, it is getting connected. Here happens the 3-way handshake.
Now you can write anything on the terminal and you can see the console.log of the server
Overview
UDP
Now let's what is UDP protocol and how it works
UDP acts quite opposite to the TCP protocol
It is a connection-less protocol which implies that it doesn't care about the who the client connected and all the above 3-way handshake and said to stateless
It doesn't Guarantees the data shared
It is way more faster than TCP
Both the protocol have their pros and cons and they are used accordingly to the requirements
The header size of UDP is 8-bytes and it is fixed
Demo
Now let's write a UDP server and watch it in action.
we will be using dgram module in node js
The dgram module provides an implementation of UDP datagram sockets.
const dgram = require('dgram')
const socket = dgram.createSocket('udp4');
socket.on('message', (msg, rinfo) => {
console.log("Rinfo",rinfo);
console.log(`Server got : ${msg} from ${rinfo.address } and ${rinfo.port}`);
})
socket.bind(8081);
For the client, we can use Netcat
Now start the server
Open a new terminal and execute the command ❯ echo "hi" | nc -w1 -u 127.0.0.1 8081
Now you can see the hi on the console
Overview
The Steps of a Three-Way Handshake
First, a connection between server and client is established, so the server must have open ports that can accept and initiate new connections.
The client sends an SYN (Synchronize Sequence Number) data packet over an IP network to a server on the same or an external network.
When the server receives the SYN packet from the client, it responds and returns a confirmation – the ACK (Acknowledgement Sequence Number) packet or SYN/ACK packet. The packet includes two things SYN and ACK
The client receives the SYN/ACK from the server and sends back an ACK packet to the server
On completion of this process, the connection is created and the client and server now can communicate.