COMPUTER NETWORKS
FILE
Name : Ritwik
Roll no : 2017UIT2542
1.OSI Model
#include<iostream>
#include<graphics.h>
using namespace std;
int main()
int gd,gm;
gd = DETECT;
initgraph(&gd,&gm,"");
int i,j;
outtextxy(250,450,"ISO OSI Model");
delay(2000);
outtextxy(90,30,"Sender");
delay(500);
setcolor(6);
for(i=50;i<=350;i+=50)
rectangle(200,i,50,i+30);
rectangle(100,i+30,140,i+40);
if(i>=300)
rectangle(140,i+30,180,i+40);
delay(500);
setcolor(10);
outtextxy(70,60,"Application");
delay(500);
outtextxy(70,110,"Presentation");
delay(500);
outtextxy(70,160,"Session");
delay(500);
outtextxy(70,210,"Transport");
delay(500);
outtextxy(70,260,"Network");
delay(500);
outtextxy(70,310,"Data Link");
delay(500);
outtextxy(70,360,"Physical");
delay(500);
setcolor(15);
outtextxy(390,30,"Receiver");
delay(500);
setcolor(8);
outtextxy(105,82,"h1");
delay(500);
outtextxy(105,132,"h2");
delay(500);
outtextxy(105,182,"h3");
delay(500);
outtextxy(105,232,"h4");
delay(500);
outtextxy(105,282,"h5");
delay(500);
outtextxy(105,332,"h6");
outtextxy(145,332,"t6");
delay(500);
outtextxy(105,382,"h7");
outtextxy(145,382,"t7");
delay(500);
setcolor(15);
outtextxy(390,30,"Receiver");
delay(500);
setcolor(6);
for(i=50;i<=350;i+=50)
rectangle(350,i,500,i+30);
rectangle(400,i+30,440,i+40);
if(i>=300)
rectangle(440,i+30,480,i+40);
delay(500);
setcolor(10);
outtextxy(370,60,"Application");
delay(500);
outtextxy(370,110,"Presentation");
delay(500);
outtextxy(370,160,"Session");
delay(500);
outtextxy(370,210,"Transport");
delay(500);
outtextxy(370,260,"Network");
delay(500);
outtextxy(370,310,"Data Link");
delay(500);
outtextxy(370,360,"Physical");
delay(500);
setcolor(8);
outtextxy(405,82,"h1");
delay(500);
outtextxy(405,132,"h2");
delay(500);
outtextxy(405,182,"h3");
delay(500);
outtextxy(405,232,"h4");
delay(500);
outtextxy(405,282,"h5");
delay(500);
outtextxy(405,332,"h6");
outtextxy(445,332,"t6");
delay(500);
outtextxy(405,382,"h7");
outtextxy(445,382,"t7");
delay(500);
// sender Lines
line(120,80,120,100);
delay(500);
line(120,130,120,150);
delay(500);
line(120,180,120,200);
delay(500);
line(120,230,120,250);
delay(500);
line(120,280,120,300);
delay(500);
line(120,330,120,350);
delay(500);
line(120,380,120,400);
delay(500);
// Physical Connection
line(120,400,420,400);
// Receiver Lines
line(420,380,420,400);
delay(500);
line(420,330,420,350);
delay(500);
line(420,280,420,300);
delay(500);
line(420,230,420,250);
delay(500);
line(420,180,420,200);
delay(500);
line(420,130,420,150);
delay(500);
line(420,80,420,100);
delay(500);
return 0;
2. Networking commands
Ifconfig
ifconfig utility is used to configure network interface parameters. Mostly we use this command
to check the IP address assigned to the system.
traceroute
traceroute print the route packets take to network host. Destination host or IP is mandatory
parameter to use this utility .
ping
PING (Packet INternet Groper) command is the best way to test connectivity between two
nodes. Whether it is Local Area Network (LAN) or Wide Area Network (WAN). Ping use ICMP
(Internet Control Message Protocol) to communicate to other devices.
netstat
Netstat (Network Statistic) command display connection info, routing table information etc. To
displays routing table information use option as -r.
dig
Dig (domain information groper) query DNS related information like A Record, CNAME, MX
Record etc. This command mainly use to troubleshoot DNS related query.
nslookup
nslookup command also use to find out DNS related query.
route
route command also shows and manipulate ip routing table.
host
host command to find name to IP or IP to name in IPv4 or IPv6 and also query DNS records.
arp
ARP (Address Resolution Protocol) is useful to view / add the contents of the kernel’s ARP
tables
3.Parity check
# include<bits/stdc++.h>
using namespace std;
// Function to get parity of number n. It returns 1
// if n has odd parity, and returns 0 if n has even
// parity
int getParity(string s)
int p=0;
for(int i=0;i<s.length();i++)
if(s[i]=='1')
p++;
return p;
/* Driver program to test getParity() */
int main()
{ string s1,s2;
cout<<"Enter sent data string:";
cin>>s1;
cout<<"Enter recieved data string:";
cin>>s2;
if(getParity(s1)==getParity(s2))
cout<<"There is no error";
else
cout<<"One bit error";
return 0;
}
4.CRC
#include <iostream>
using namespace std;
int main()
int i,j,k,l;
//Get Frame
int fs;
cout<<"\n Enter Frame size: ";
cin>>fs;
int f[20];
cout<<"\n Enter Frame:";
for(i=0;i<fs;i++)
cin>>f[i];
//Get Generator
int gs;
cout<<"\n Enter Generator size: ";
cin>>gs;
int g[20];
cout<<"\n Enter Generator:";
for(i=0;i<gs;i++)
cin>>g[i];
cout<<"\n Sender Side:";
cout<<"\n Frame: ";
for(i=0;i<fs;i++)
cout<<f[i];
cout<<"\n Generator :";
for(i=0;i<gs;i++)
{
cout<<g[i];
//Append 0's
int rs=gs-1;
cout<<"\n Number of 0's to be appended: "<<rs;
for (i=fs;i<fs+rs;i++)
f[i]=0;
int temp[20];
for(i=0;i<20;i++)
temp[i]=f[i];
cout<<"\n Message after appending 0's :";
for(i=0; i<fs+rs;i++)
cout<<temp[i];
//Division
for(i=0;i<fs;i++)
j=0;
k=i;
//check whether it is divisible or not
if (temp[k]>=g[j])
for(j=0,k=i;j<gs;j++,k++)
if((temp[k]==1 && g[j]==1) || (temp[k]==0 && g[j]==0))
temp[k]=0;
else
temp[k]=1;
//CRC
int crc[15];
for(i=0,j=fs;i<rs;i++,j++)
crc[i]=temp[j];
cout<<"\n CRC bits: ";
for(i=0;i<rs;i++)
cout<<crc[i];
cout<<"\n Transmitted Frame: ";
int tf[15];
for(i=0;i<fs;i++)
tf[i]=f[i];
for(i=fs,j=0;i<fs+rs;i++,j++)
tf[i]=crc[j];
for(i=0;i<fs+rs;i++)
cout<<tf[i];
cout<<"\n Receiver side : ";
cout<<"\n Received Frame: ";
for(i=0;i<fs+rs;i++)
cout<<tf[i];
for(i=0;i<fs+rs;i++)
temp[i]=tf[i];
//Division
for(i=0;i<fs+rs;i++)
j=0;
k=i;
if (temp[k]>=g[j])
for(j=0,k=i;j<gs;j++,k++)
if((temp[k]==1 && g[j]==1) || (temp[k]==0 && g[j]==0))
temp[k]=0;
else
temp[k]=1;
cout<<"\n Reaminder: ";
int rrem[15];
for (i=fs,j=0;i<fs+rs;i++,j++)
rrem[j]= temp[i];
for(i=0;i<rs;i++)
cout<<rrem[i];
int flag=0;
for(i=0;i<rs;i++)
{
if(rrem[i]!=0)
flag=1;
if(flag==0)
cout<<"\n Since Remainder Is 0 Hence Message Transmitted From
Sender To Receriver Is Correct";
else
cout<<"\n Since Remainder Is Not 0 Hence Message Transmitted
From Sender To Receriver Contains Error";
return 0;
}
5.Hamming code
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
int a, b, c[30], d, r = 0, d1,x=0; //Max bits here is kept to 30
cout << " Enter the No of Data Bits you want to Enter : ";
cin>> a;
while (a + r + 1 > pow (2, r))
r++;
cout <<"Total Bits in hamming code :" << a + r << endl;
cout <<" Enter the Data Bits One by One :" << endl;
for (int i = 1; i <= a; ++i)
cin >> c[i];
cout << endl << " Data bits entered : ";
for (int i = 1; i <= a; ++i)
cout << c[i] << " ";
cout << endl;
int data[a + r],data1[a + r];
d = 0;
d1 = 1;
for (int i = 1; i <= a + r; ++i)
if ((i) == pow (2, d))
data[i] = 0;
++d;
else
data[i] = c[d1];
++d1;
cout << " Data Bits are Encoded with Parity bits(0): ";
for (int i = 1; i <= a + r; ++i)
cout << data[i] << " ";
d1 = 0;
int min, max = 0, parity, s, j;
/*Parity Bit Calculation */
for (int i = 1; i <= a + r; i = pow (2, d1))
++d1;
parity = 0;
j = i;
s = i;
min = 1;
max = i;
for (j; j <= a + r;)
{
for (s = j; max >= min && s <= a + r; ++min, ++s)
if (data[s] == 1)
parity++;
j = s + i;
min = 1;
if (parity % 2 == 0) // Even Parity
data[i] = 0;
else
data[i] = 1;
cout << endl << " Hamming codeword for even parity : ";
for (int i = 1; i <= a + r; ++i)
cout << data[i] << " ";
cout << endl << endl;
cout<<"Enter recieved hamming code :";
for(int i=1;i<=a+r;i++)
cin>>data1[i];
min=max=d1=0;
for (int i = 1; i <= a + r; i = pow (2, d1))
{
++d1;
parity = 0;
j = i;
s = i;
min = 1;
max = i;
for (j; j <= a + r;)
for (s = j; max >= min && s <= a + r; ++min, ++s)
if (data1[s] == 1)
parity++;
j = s + i;
min = 1;
if (parity % 2 == 0) // Even Parity
data1[i] = 0;
else
data1[i] = 1;
for (int i = 1; i <= a + r; ++i)
if(data[i]!=data1[i])
{
x=1;
if(x==0)
cout<<"No error in transmission of data";
else if(x==1)
cout<<"There is error in data recieved";
cout << endl << endl;
6.Checksum
#include<stdio.h>
#include<conio.h>
#include<math.h>
int sender(int b[10],int k)
int checksum,sum=0,i;
printf("\n****SENDER****\n");
for(i=0;i<k;i++)
sum+=b[i];
printf("SUM IS: %d",sum);
checksum=~sum;
printf("\nSENDER's CHECKSUM IS:%d",checksum);
return checksum;
int receiver(int c[10],int k,int scheck)
int checksum,sum=0,i;
printf("\n\n****RECEIVER****\n");
for(i=0;i<k;i++)
sum+=c[i];
printf(" RECEIVER SUM IS:%d",sum);
sum=sum+scheck;
checksum=~sum;
printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
return checksum;
main()
{
int a[10],i,m,scheck,rcheck;
printf("\nENTER SIZE OF THE STRING:");
scanf("%d",&m);
printf("\nENTER THE ELEMENTS OF THE ARRAY:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
scheck=sender(a,m);
rcheck=receiver(a,m,scheck);
if(rcheck==0)
printf("\n\nNO ERROR IN TRANSMISSION\n\n");
else
printf("\n\nERROR DETECTED");
getch();