0% found this document useful (0 votes)
90 views65 pages

Cnlab

The document describes a lab course on computer networks. The course objectives are to learn basic networking concepts and protocols with a focus on TCP/IP. Students will assemble networks and conduct experiments to understand the layered architecture and how protocols work. The list of experiments includes implementing various data link layer protocols, routing algorithms, network management tools, and simulating networks using NS2.

Uploaded by

Syamala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views65 pages

Cnlab

The document describes a lab course on computer networks. The course objectives are to learn basic networking concepts and protocols with a focus on TCP/IP. Students will assemble networks and conduct experiments to understand the layered architecture and how protocols work. The list of experiments includes implementing various data link layer protocols, routing algorithms, network management tools, and simulating networks using NS2.

Uploaded by

Syamala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 65

COMPUTER NETWORKS LAB

Course Objectives:
Learn basic concepts of computer networking and acquire practical notions of
protocols with the emphasis on TCP/IP. A lab provides a practical approach to
Ethernet/Internet networking: networks are assembled, and experiments are
made to understand the layered architecture and how do some important
protocols work

Course Outcomes:
By the end of the course student will be able to
 Know how reliable data communication is achieved through data link layer.
 Suggest appropriate routing algorithm for the network.
 Provide internet connection to the system and its installation.
 Work on various network management tools

List of Experiments:
1. Study of Network devices in detail and connect the computers in Local Area
Network.
2. Write a Program to implement the data link layer farming methods such as

i) Character stuffing ii) bit stuffing.


3. Write a Program to implement data link layer farming method checksum.
4. Write a program for Hamming Code generation for error detection and
correction
5. Write a Program to implement on a data set of characters the three CRC
polynomials – CRC 12, CRC 16 and CRC CCIP.
6. Write a Program to implement Sliding window protocol for Goback N.
7. Write a Program to implement Sliding window protocol for Selective repeat.
8. Write a Program to implement Stop and Wait Protocol.
9. Write a program for congestion control using leaky bucket algorithm

10. Write a Program to implement Dijkstra‘s algorithm to compute the Shortest


path through a graph.
11. Write a Program to implement Distance vector routing algorithm by
obtaining routing table at each node (Take an example subnet graph with
weights indicating delay between nodes).
12. Write a Program to implement Broadcast tree by taking subnet of hosts.
13. Wireshark

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 1


i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters.
14. How to run Nmap scan
15. Operating System Detection using Nmap
16. Do the following using NS2 Simulator
i. NS2 Simulator-Introduction
ii. Simulate to Find the Number of Packets Dropped
iii. Simulate to Find the Number of Packets Dropped by TCP/UDP
iv. Simulate to Find the Number of Packets Dropped due to Congestion
v. Simulate to Compare Data Rate& Throughput

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 2


1. Study of Network devices in detail and connect the computers in Local
Area Network.

Aim: Study of following Network Devices in Detail


 Repeater
 Hub
 Switch
 Bridge
 Router
 GateWay

Apparatus(Software):No software or hardware needed.

Procedure: Following should be done to understand this practical.

1. Repeater: Functioning at Physical Layer. A repeater is an electronic device that


receives a signal and retransmits it at a higher level and/or higher power, or onto the
other side of an obstruction, so that the signal can cover longer distances. Repeater
have two ports, so cannot be use to connect for more than two devices

2.Hub:An Ethernet hub, active hub, network hub, repeater hub, hub or concentrator is a
device for connecting multiple twisted pair or fiber optic Ethernet devices together and
making them act as a single network segment. Hubs work at the physical layer(layer1)of
the OSI model. The device is a form of multiport repeater. Repeater hubs also
participate in collision detection ,forwarding a jam signal to all ports if it detects a
collision.

3.Switch: A network switch or switching hub is a computer networking device that


connects network segments. The term commonly refers to a network bridge that
processes and routes data at the data link layer (layer 2) of the OSI model. Switches that
additionally process data at the network layer(layer3andabove) are often referred to as
Layer3 switches or multilayer switches.

4. Bridge: A network bridge connects multiple network segments at the data link
layer(Layer 2) of the OSI model. In Ethernet networks, the term bridge formally means a
device that behaves according to the IEEE 802.1D standard. A bridge and switch are
very much alike; a switch being a bridge with numerous ports. Switch or Layer 2 switch
is often used inter changeably with bridge. Bridges can analyse incoming data packets
to determine if the bridge is able to send the given packet to another segment of the
network.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 3


5. Router: A router is an electronic device that interconnects two or more computer
networks, and selectively interchanges packets of data between them. Each data packet
contains address information that a router can use to determine if the source and
destination are on the same network, or if the data packet must be transferred from
one network to another. Where multiple routers are used in a large collection of inter
connected networks, the routers exchange information about target system addresses,
so that each router can build up at able showing the preferred paths between any two
systems on the inter connected networks.

6.GateWay:In a communications network, a network node equipped for interfacing with another
network that uses different protocols.
A gateway may contain devices such as protocol translators, impedance matching devices, rate
converters, fault isolators, or signal translators as necessary to provide system interoperability.
It also requires the establishment of mutually acceptable administrative procedures between
both networks.
A protocol translation /mapping gate way inter connects networks with different network
protocol technologies by performing there required protocol conversions.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 4


II) Aim: Connect the computers in Local Area Network.

Procedure: On the host computer

On the host computer, follow these steps to share the Internet connection:

1. Log on to the host computer as Administrator or asOwner.


2. Click Start, and then click Control Panel.
3. Click Network and Internet Connections.
4. Click Network Connections.
Right-click the connection that you use to connect to the Internet. For example, if you
connect to the Internet by using a modem, right-click the connection that you want
under Dial-up/other network available.
5. Click Properties.
6. Click the Advanced tab.

7.Under Internet Connection Sharing, select the Allow other


networkuserstoconnectthroughthiscomputer'sInternetconnection
checkbox.

8.Ifyouaresharingadial-pInternetconnection,selecttheEstablishadial-
upconnectionwhenever a computer on my network attempts to access
theInternetcheckboxifyouwanttopermityourcomputertoautomaticallycon
necttotheInternet.

9. ClickOK.You receive the following message:

When Internet Connection Sharing is enabled, your LAN adapter will be


settouseIPaddress192.168.0.1.Yourcomputermayloseconnectivitywithother
computers on your network.If these other computers have static IP
addresses,it is a good idea to set them to obtain their IP addresses
automatically.Are you sure you want to enable Internet Connection
Sharing?

10.ClickYes.
The connection to the Internet is shared to other computers on the local
area network(LAN).

The network adapter that is connected to the LAN is configured with


a static IP address of192.168.0.1and a subnet mask of 255.255.255.0

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 5


On the client computer

To connect to the Internet by using the shared connection, you must


confirm the LAN adapter IP configuration, and then configure the
client computer. To confirm the LAN adapter IP configuration,
follow these steps:

1. Log on to the client computer as Administrator or as Owner.


2. Click Start, and then click Control Panel.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 6


2. Implement the data link layer framing methods

Aim:
Implement the data link layer framing methods such as Bit Stuffing.
Theory
Security and Error detection are the most prominent features that are to be provided by any
application which transfers data from one end to the other end. One of such a mechanism in
tracking errors which may add up to the original data during transfer is known as Stuffing. It is of two
types namely Bit Stuffing and the other Character Stuffing. Coming to the Bit Stuffing, 01111110 is
appended within the original data while transfer of it. The following program describes how it is
stuffed at the sender end and de-stuffed at the reciever end.

Program:

Aim: Implement the data link layer framing methods such as Bit Stuffing.
#include<stdio.h>
#include<string.h>
int main()
{
int a[20],b[30],i,j,k=0,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0; count=1; j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 7


}
}
else
{
b[j]=a[i];
}
i++;
j++;

printf("After Bit Stuffing :");


for(i=0; i<j; i++)
printf("%d",b[i]);
return 0;
}

Output:

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 8


2.Aim:
Implement the data link layer framing methods such as Character Stuffing
and also De-stuff it.

Theory

Coming to the Character Stuffing, DLESTX (Data Link EscapeStart of TeXt) and
DLEETX (Data Link EscapeEnd of TeXt)are used to denote start and end of
character data with some constraints imposed on repetition of charaters as
shown in the program below clearly.

Program

#include<stdio.h>
#include<string.h>
int main( )
{
char sdel[100]="DLESTX -",data[100] =" ",sdata[100]=" " ,ch;
int i=0,j=0,k;
printf("\n Enter the message: ");
scanf("%s",data);
for(k=0;data[k]!='\0';k++)
{
if(data[k]>='a'&& data[k]<='z')
{
data[k]=data[k]-32;
}
}
printf("\n Original message:%s",data);
if(strlen(data)<=3)
{
strcat(sdata,sdel);
strcat(sdata,data);
strcat(sdata," -DLETX");
printf("\n Message after character stuffing is : %s",sdata);
}
else

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 9


{
while(data[i]!='\0')
{
if(data[i]=='D'&& data[i+1]=='L'&& data[i+2]=='E')
{
strcat(sdata,"DLEDLE");
i=i+3;
j=j+6;
continue;
}
sdata[j]=data[i];
j++;
i++;
}
strcat(sdel,sdata);
strcat(sdel,"-DLEETX");
printf("\n \n Message after character stuffing is :%s",sdel);
}
getch();
return 0;
}

OUTPUT
(1) Enter the message: radle

Original message: RADLE

Message after character stuffing is : DLESTX-RADLEDLE-DLEETX

2) Enter the message: rad

Original message: RAD

Message after character stuffing is : DLESTX-RAD-DLEETX

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 10


3.Write a Program to implement data link layer farming method checksum

A checksum is a error detection method in Data Communication. It is used for errors


which may have been introduced during transmission or storage. It is usually applied to
an installation file after it is received from the download server.

The actual procedure which yields the checksum, given a data input is called a checksum
function or checksum algorithm.

Checksum Algorithm

1. Take 2 binary input strings.


2. Do their binary sum to find out the checksum which will be sent to the
destination or to the receiver.
3. In binary sum there are 6 cases:-
a. If both bits are 0 and carry is 0, sum=0 and carry=0
b. If both bits are 0 and carry is 1,sum=1 and carry=0
c. If both bits are 1 and carry is 0,sum=0 and carry=1
d. If both bits are 1 and carry is 1,sum=1 and carry=1
e. If either bit is 1 and carry is 0,sum=1 and carry=0
f. If either bit is 1 and carry is 1,sum=0 and carry=1
4. While doing the addition we have to add the binary strings from rightmost end
i.e LSB to MSB.
When binary sum is done 1’s complement of it is taken by reversing 1’s to 0’s and
vice versa.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 11


Program

#include<stdio.h>
#include<string.h>

int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i,length;

printf("Enter first binary string\n");


scanf("%s",&a);
printf("Enter second binary string\n");
scanf("%s",&b);

if(strlen(a)==strlen(b))
{
length = strlen(a);
char carry='0';

for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';

}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
carry='0';

}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 12


}
else if(a[i]=='1' && b[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';

}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
carry='1';

}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';

}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';

}
else
break;
}

printf("\nSum=%c%s",carry,sum);
for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}

if(carry=='1')
carry='0';
else
carry='1';
printf("\nChecksum=%c%s",carry,complement);
}
else {
printf("\nWrong input strings");

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 13


}
}

Out Put
Enter first binary string
101101
Enter second binary string
110010
Sum=1011111
Checksum=0100000

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 14


4.Write a program for Hamming Code generation for error detection
and correction

Hamming code is a popular error detection and error correction method in


data communication. Hamming code can only detect 2 bit error and
correct a single bit error which means it is unable to correct burst errors if
may occur while transmission of data.

Hamming code uses redundant bits (extra bits) which are calculated according to the
below formula:-

r
2 ≥ m+r+1

Where r is the number of redundant bits required and m is the number of data bits.

R is calculated by putting r = 1, 2, 3 … until the above equation becomes true.

R1 bit is appended at position 20

R2 bit is appended at position 21

R3 bit is appended at position 22 and so on.

These redundant bits are then added to the original data for the
calculation of error at receiver’s end.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 15


At receiver’s end with the help of even parity (generally) the erroneous bit
position is identified and since data is in binary we take complement of
the erroneous bit position to correct received data.

Respective index parity is calculated for r1, r2, r3, r4 and so on.

Advantages of Hamming Code

1. Easy to encode and decode data at both sender and receiver end.
2. Easy to implement.
Disadvantages of Hamming Code

1. Cannot correct burst errors.


2. Redundant bits are also sent with the data therefore it requires more
bandwidth to send the data.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 16


Program

#include <stdio.h>

#include <math.h>

int input[32];

int code[32];

int ham_calc(int,int);

void main()

int n,i,p_n = 0,c_l,j,k;

printf("Please enter the length of the Data Word: ");

scanf("%d",&n);

printf("Please enter the Data Word:\n");

for(i=0;i<n;i++)

scanf("%d",&input[i]);

i=0;

while(n>(int)pow(2,i)-(i+1))

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 17


{

p_n++;

i++;

c_l = p_n + n;

j=k=0;

for(i=0;i<c_l;i++)

if(i==((int)pow(2,k)-1))

code[i]=0;

k++;

else

code[i]=input[j];

j++;

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 18


}

for(i=0;i<p_n;i++)

int position = (int)pow(2,i);

int value = ham_calc(position,c_l);

code[position-1]=value;

printf("\nThe calculated Code Word is: ");

for(i=0;i<c_l;i++)

printf("%d",code[i]);

printf("\n");

printf("Please enter the received Code Word:\n");

for(i=0;i<c_l;i++)

scanf("%d",&code[i]);

int error_pos = 0;

for(i=0;i<p_n;i++)

int position = (int)pow(2,i);

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 19


int value = ham_calc(position,c_l);

if(value != 0)

error_pos+=position;

if(error_pos == 1)

printf("The received Code Word is correct.\n");

else

printf("Error at bit position: %d\n",error_pos);

int ham_calc(int position,int c_l)

int count=0,i,j;

i=position-1;

while(i<c_l)

for(j=i;j<i+position;j++)

if(code[j] == 1)

count++;

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 20


i=i+2*position;

if(count%2 == 0)

return 0;

else

return 1;

Out put:-
Please enter the length of the Data Word: 8
Please enter the Data Word:
1
1
0
1
0
0
1
1

The calculated Code Word is: 011110100011


Please enter the received Code Word:
0
1
1
1
1
1
1
0
0
0
1
1
Error at bit position: 6

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 21


------------------------------

5. Write a Program to implement on a data set of characters the three CRC


polynomials – CRC 12, CRC 16 and CRC CCIP.

#include<stdio.h>
#include<conio.h>
void main()
{
int f[150],g[150],r[150],t[150],i,j,k,m,n,c=0;

printf("\n Enter f: ");


scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&f[i]);
t[i]=f[i];
}
printf("\n Enter g: ");
scanf("%d",&m);
for(i=0;i<m;i++)

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 22


{
scanf("%d",&g[i]); }
for(i=n;i<n+m;i++)
t[i]=0;
for(i=0;i<n;i++)
{
k=i;
if(t[i]==1)
{
for(j=0;j<m;j++,k++)
{
if(t[k]==g[j])
t[k]=0;
else
t[k]=1;
}
}
}
printf("\n check sum is : ");
for(i=n;i<m+n-1;i++)
{
printf(" %d ",t[i]);
f[i]=t[i];
}
printf("\n\n BCS : ");
for(i=0;i<m+n-1;i++)
printf(" %d ",f[i]);
printf("\n\nEnter received data : ");
for(i=0;i<m+n-1;i++)
scanf("%d",&r[i]);
for(i=0;i<n;i++)
{
k=i;
if(r[i]==1)
{
for(j=0;j<m;j++,k++)
{
if(r[k]==g[j])
r[k]=0;
else
r[k]=1;
}
}
}
printf(" \n Recived data:");
for(i=n;i<m+n-1;i++)
{

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 23


printf("%d",r[i]);
if(r[i]!=0)
c=1;
}
if(c==1)
printf("\n Error ");
else
printf("\n NO error");
getch();
}

OUTPUT 1:
Enter f: 24
111100001010101010000000
Enter g: 12
1 10000001 1 1 1
check sumis:010000001 10
BCS : 111100001010101010000000 01000000110
Enter received data: 1 1 1 100001010101010000000 0100 0000 1 10
Check sum is; 00000000000 NO error

OUTPUT 2:
Enter f: 24
101010101111111 100000000
Enter g: 16
11000000000000101
check sumis:001000001000110
BCS: 101010101111111100000000 001000001000110
Enter received data : 1111111111111111 111111111111 1111111111
Checksum is: 110110011001101 Error

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 24


6.Write a Program to implement Sliding window protocol for Goback N

#include<stdio.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 25


printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);

if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}

Output
enter window size
8
Frame 0 has been transmitted.
Frame 1 has been transmitted.
Frame 2 has been transmitted.
Frame 3 has been transmitted.
Frame 4 has been transmitted.
Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.

Please enter the last Acknowledgement received.


2
Frame 2 has been transmitted.
Frame 3 has been transmitted.
Frame 4 has been transmitted.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 26


Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.

Please enter the last Acknowledgement received.


8

7. Write a Program to implement Sliding window protocol for Selective


repeat.

1. Selective Repeat: Sender transmits only that frame which is erroneous or is lost.

2. Go back n: Sender transmits all frames present in the window that occurs after the error
bit including error bit also.

#include<stdio.h>

int main()
{
int w,i,f,frames[50];

printf("Enter window size: ");


scanf("%d",&w);

printf("\nEnter number of frames to transmit: ");


scanf("%d",&f);

printf("\nEnter %d frames: ",f);

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 27


for(i=1;i<=f;i++)
scanf("%d",&frames[i]);

printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);

for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}

if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;
}

OUTPUT

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 28


8. Write a Program to implement Stop and Wait Protocol.
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int main()

int i,j,nframes,x,x1=10,x2;

printf("enter no.of frames: ");

scanf("%d",&nframes);

i=1;

j=1;

printf("no.of frames : %d",nframes);

while(nframes>0)

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 29


printf("\n sending frams: %d",i);

x=rand()%10;

if(x%10==0)

for(x2=1;x2<2;x2++)

printf("\n waiting for %d seconds\n",x2);

sleep(x2);

printf("\n sendinf frames: %d \n",i);

x=rand()%10;

}
printf("\n ack for frame %d\n",j);

nframes=nframes-1;

i++;

j++;

output:
no.of frames : 5

sending frams: 1

ack for frame 1

sending frams: 2

ack for frame 2

sending frams: 3

ack for frame 3

sending frams: 4

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 30


ack for frame 4

sending frams: 5

ack for frame 5

9. Write a program for congestion control using leaky bucket algorithm

#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,packets[10],content=0,newcontent,time,clk,bcktsize,oprate;
for(i=0;i<5;i++)
{
packets[i]=rand()%10;
if(packets[i]==0) --i;
}
printf("\n Enter output rate of the bucket: \n");
scanf("%d",&oprate);
printf("\n Enter Bucketsize\n");
scanf("%d",&bcktsize);
for(i=0;i<5;++i)
{
if((packets[i]+content)>bcktsize)
{
if(packets[i]>bcktsize)
printf("\n Incoming packet size %d greater than the size of the bucket\n",packets[i]);

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 31


else
printf("\n bucket size exceeded\n");
}
else
{
newcontent=packets[i];
content+=newcontent;
printf("\n Incoming Packet : %d\n",newcontent);
printf("\n Transmission left : %d\n",content);
time=rand()%10;
printf("\n Next packet will come at %d\n",time);
for(clk=0;clk<time && content>0;++clk)
{
printf("\n Left time %d",(time-clk));
sleep(1);
if(content)
{
printf("\n Transmitted\n");
if(content<oprate)
content=0;
else
content=content-oprate;
printf("\n Bytes remaining : %d\n",content);
}
else
printf("\n No packets to send\n");
}
}
}
}

OUTPUT

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 32


P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 33
P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 34
10. Write a Program to implement Dijkstra‘s algorithm to compute the
Shortest path through a graph.
#include<stdio.h>

#define INFINITY 9999

#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

int main()

int G[MAX][MAX],i,j,n,u;

printf("Enter no. of vertices:");

scanf("%d",&n);

printf("\nEnter the adjacency matrix:\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

scanf("%d",&G[i][j]);

printf("\nEnter the starting node:");

scanf("%d",&u);

dijkstra(G,n,u);

return 0;

void dijkstra(int G[MAX][MAX],int n,int startnode)

int cost[MAX][MAX],distance[MAX],pred[MAX];

int visited[MAX],count,mindistance,nextnode,i,j;

//pred[] stores the predecessor of each node

//count gives the number of nodes seen so far

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 35


//create the cost matrix

for(i=0;i<n;i++)

for(j=0;j<n;j++)

if(G[i][j]==0)

cost[i][j]=INFINITY;

else

cost[i][j]=G[i][j];

//initialize pred[],distance[] and visited[]

for(i=0;i<n;i++)

distance[i]=cost[startnode][i];

pred[i]=startnode;

visited[i]=0;

distance[startnode]=0;

visited[startnode]=1;

count=1;

while(count<n-1)

mindistance=INFINITY;

//nextnode gives the node at minimum distance

for(i=0;i<n;i++)

if(distance[i]<mindistance&&!visited[i])

mindistance=distance[i];

nextnode=i;

//check if a better path exists through nextnode

visited[nextnode]=1;

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 36


for(i=0;i<n;i++)

if(!visited[i])

if(mindistance+cost[nextnode][i]<distance[i])

distance[i]=mindistance+cost[nextnode][i];

pred[i]=nextnode;

count++;

//print the path and distance of each node

for(i=0;i<n;i++)

if(i!=startnode)

printf("\nDistance of node%d=%d",i,distance[i]);

printf("\nPath=%d",i);

j=i;

do

j=pred[j];

printf("<-%d",j);

}while(j!=startnode);

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 37


Output

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 38


11.Write a Program to implement Distance vector routing algorithm by obtaining
routing table at each node (Take an example subnet graph with weights indicating delay
between nodes).

#include<stdio.h>

struct node

unsigned dist[20];

unsigned from[20];

}rt[10];

int main()

int costmat[20][20];

int nodes,i,j,k,count=0;

printf("\nEnter the number of nodes : ");

scanf("%d",&nodes);//Enter the nodes

printf("\nEnter the cost matrix :\n");

for(i=0;i<nodes;i++)

for(j=0;j<nodes;j++)

scanf("%d",&costmat[i][j]);

costmat[i][i]=0;

rt[i].dist[j]=costmat[i][j];//initialise the distance equal to cost matrix

rt[i].from[j]=j;

do

count=0;

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 39


for(i=0;i<nodes;i++)//We choose arbitary vertex k and we calculate the direct distance from
the node i to k using the cost matrix

//and add the distance from k to node j

for(j=0;j<nodes;j++)

for(k=0;k<nodes;k++)

if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])

{//We calculate the minimum distance

rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];

rt[i].from[j]=k;

count++;

}while(count!=0);

for(i=0;i<nodes;i++)

printf("\n\n For router %d\n",i+1);

for(j=0;j<nodes;j++)

printf("\t\nnode %d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);

printf("\n\n");

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 40


Output

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 41


12. Write a Program to implement Broadcast tree by taking subnet of hosts.

#include <stdio.h>
int p,q,u,v,n;
int min = 99,mincost=0;
int t[50][2],i,j;
int parent[50],edge[50][50];
void sunion(int I, int m)
{
parent[I]=m;
}
int find(int I)
{
if(parent[I]>0)
I=parent[I];
return I;
}
int main()
{
printf("Enter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\t",65+i);
parent[i]=-1;
}
printf("\n");
for(i=0;i<n;i++)
{

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 42


printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min = edge[i][j];
u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{
t[i][0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
t[i][1]=-1;
}
min = 99;

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 43


}
printf("Minimum cost is %d\n Minimum spanning tree is \n",mincost);
for(i=0;i<n;i++)
if(t[i][0]!=-1&&t[i][1]!=-1)
{
printf("%c %c %d",65+t[i][0],65+t[i][1],edge[t[i][0]][t[i][1]]);
printf("\n");
}
return 0;
}

output:

Enter the number of nodes:4


A BC D
A1 3 5 6
B6 7 8 9
C2 3 5 6
D1 2 3 7
Minimum cost is 9
Minimum spanning tree is
BA6
CA2
DA1

13. Wireshark

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 44


i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters

What is Wireshark?

Wire shark is an open-source network protocol analysis software program, widely


considered the industry standard. A global organization of network specialists and
software developers supports Wireshark and continues to make updates for new
network technologies and encryption methods.

How does Wire shark work?

Wireshark is a packet sniffer and analysis tool. It captures network traffic from
ethernet, Bluetooth, wireless (IEEE.802.11), token ring, and frame relay connections,
among others, and stores that data for offline analysis.

Editor’s Note: A “packet” is a single message from any network protocol (e.g., TCP,
DNS, etc.).

How to download Wireshark


Downloading and installing Wireshark is easy. Step one is to check the
official Wireshark download page for the operating system you need. The installation
is simple, and the basic version of Wireshark is free.

Wireshark for Windows

Wireshark comes in two options for Windows: 32-bit and 64-bit. Pick the correct
version for your OS; the current release is 3.0.3 as of this writing.

Capturing data packets on Wireshark

When you open Wireshark, you see a screen showing you a list of all the network connections you can monitor. You also have a
capture filter field to only capture the network traffic you want to see.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 45


You can select the menu item Capture -> Start.

During the capture, Wireshark will show you the packets captured in real-time.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 46


Once you have captured all the packets needed, use the same buttons or menu options to stop the capture as you did to begin.

Analyzing data packets on Wireshark

Wireshark shows you three different panes for inspecting packet data. The Packet List, the top pane, lists all the packets in the capture.
When you click on a packet, the other two panes change to show you the details about the selected packet. You can also tell if the
packet is part of a conversation. Here are details about each column in the top pane:

 No.: This is the number order of the packet captured. The bracket indicates that this packet is part of a conversation.
 Time: This column shows how long after you started the capture this particular packet was captured. You can change this
value in the Settings menu to display a different option.
 Source: This is the address of the system that sent the packet.
 Destination: This is the address of the packet destination.
 Protocol: This is the type of packet. For example: TCP, DNS, DHCPv6, or ARP.
 Length: This column shows you the packet’s length, measured in bytes.
 Info: This column shows you more information about the packet contents, which will vary depending on the type of packet.

Packet Details, the middle pane, shows you as much readable information about the packet as possible, depending on the packet
type. You can right-click and create filters based on the highlighted text in this field.

The bottom pane, Packet Bytes, displays the packet exactly as it was captured in hexadecimal.

When looking at a packet that is part of a conversation, you can right-click the packet and select Follow to see only the packets that are
part of that conversation.

Wireshark filters

 Some of the best features of Wireshark are the capture filters and display filters. Filters allow you to view the capture the way
you need to see it to troubleshoot the issues at hand. Below are several filters to get you started.

Wireshark capture filters


Capture filters limit the captured packets by the chosen filter. If the packets don’t match the filter, Wireshark won’t save them. Examples
of capture filters include:

host IP-address: This filter limits the captured traffic to and from the IP address

net 192.168.0.0/24: This filter captures all traffic on the subnet

dst host IP-address: Capture packets sent to the specified host

port 53: Capture traffic on port 53 only

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 47


port not 53 and not arp: Capture all traffic except DNS and ARP traffic

Wireshark display filters


Wireshark display filters change the view of the capture during analysis. After you’ve
stopped the packet capture, use display filters to narrow down the packets in the
Packet List to troubleshoot your issue.

One of the most useful display filters is:

ip.src==IP-address and ip.dst==IP-address

This filter shows packets sent from one computer (ip.src) to another (ip.dst). You can
also use ip.addr to show packets to and from that IP. Other filters include:

tcp.port eq 25: This filter will show you all traffic on port 25, which is usually SMTP
traffic

icmp: This filter will show you only ICMP traffic in the capture, most likely they are
pings

ip.addr != IP_address: This filter shows you all traffic except the traffic to or from the
specified computer

Analysts even build filters to detect specific attacks, like this filter used to detect
the Sasser worm:

ls_ads.opnum==0x09

14. How to run Nmap scan

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 48


What is Nmap?
 Nmap is a network mapper that has emerged as one of the most popular, free network
discovery tools on the market.
 Nmap is now one of the core tools used by network administrators to map their networks.
 The program can be used to find live hosts on a network, perform port scanning, ping
sweeps, OS detection, and version detection.
How to Run Nmap

 Nmap is Linux command-line tool for network exploration and security auditing.
 This tool is generally used by hackers and cyber security enthusiasts and even by network
and system administrators.
 It is used for the following purposes:
a. Real time information of a network
b. Detailed information of all the IPs activated on your network
c. Number of ports open in a network
d. Provide the list of live hosts
e. Port, OS and Host scanning
Installing Nmap Command

In case of Debian/Ubuntu sudo apt-get install nmap

In case of CentOS/RedHat yum install nmap

Working with Nmap Command

1. To scan a System with Hostname and IP address. First, Scan using Hostname
nmap www.geeksforgeeks.org

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 49


Now let’s Scan using IP Address nmap 172.217.27.174

 The nmap command allows scanning a system in various ways.


 In this we are performing a scan using the hostname as “geeksforgeeks” and IP address
“172.217.27.174”, to find all open ports, services, and MAC addresses on the system.

2. To scan using “-v” option. nmap -v www.geeksforgeeks.org

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 50


It is used to get more detailed information about the remote machines.

3. To scan multiple hosts nmap 103.76.228.244 157.240.198.35 172.217.27.174

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 51


We can scan multiple hosts by writing IP addresses or hostnames with nmap

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 52


4. To scan whole subnet nmap 103.76.228.*
 We can scan a whole subnet or IP range with nmap by providing “*” with it.
 It will scan a whole subnet and give the information about those hosts which are Up in the
Network.
5. To scan to detect firewall settings. sudo nmap -sA 103.76.228.244

 Detecting firewall settings can be useful during penetration testing and vulnerability scans.
 To detect it we use “-sA” option.
 This will provide you with information about firewall being active on the host.
 It uses an ACK scan to receive the information.
6. To identify Hostnames sudo nmap -sL 103.76.228.244

 We use “sL” option to find hostnames for the given host by completing a DNS query for each
one.
 In addition to this “-n” command can be used to skip DNS resolution,
 while the “-R” command can be used to always resolve DNS.

7. To scan from a file nmap -iL input.txt

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 53


 If we have a long list of addresses that we need to scan, we can directly import a file through
the command line. It will produce a scan for the given IP addresses.
8. To get some help nmap -h

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 54


 We use the “-h” option if we have any questions about nmap or any of the given
commands.
 It shows the help section for nmap command, including giving information regarding the
available flags.

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 55


9. Here -A Indicates Aggressive it will let Us Know The Extra Information’s like OS Detection (-
O), version detection, script scanning (-sC), and traceroute (–traceroute) even it provides a lot of
valuable information About The Host. nmap -A <Domain Name>

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 56


10. Using This Command we can even Discover our Target Hosting Service or Identify
Additional Targets According to our Needs For Quickly Tracing the Path.
nmap --trace out <Domain Name>

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 57


15. Operating System Detection using Nmap
11. Here It Will Display The Operating System Where The Domain or Ip Address is Running
But Will Not Display Exact Operating System Available On Computer. It Will Only Display The
Chance of Operating System Available in The Computer. This Will Just Guess the Running
Operating System (OS) in the Host.
nmap -O <Domain Name>

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 58


16. Do the following using NS2 Simulator
i. NS2 Simulator-Introduction
ii. Simulate to Find the Number of Packets Dropped
iii. Simulate to Find the Number of Packets Dropped by TCP/UDP
iv. Simulate to Find the Number of Packets Dropped due to Congestion
v. Simulate to Compare Data Rate& Throughput

NS2 Simulator-Introduction
•ns-2 stands for NewworkSimulator version 2.
•ns-2: •Is a discrete event simulator for networking research
•Work at packet level.
•Provide substantial support to simulate bunch of protocols like TCP, UDP, FTP,
HTTP and DSR.
•Simulate wired and wireless network.
•Is primarily Unix based.
•Use TCL as its scripting language.
•ns-2 is a standard in research community

ii. Simulate to Find the Number of Packets Dropped

Aim: Simulate the transmission of ping messages over a network topology consisting of 6 nodes and
find the number of packets dropped due to congestion.

#Create six nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 59


set n4 [$ns node]

set n5 [$ns node]


# Connect the nodes with two links

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n2 $n1 1Mb 10ms DropTail

$ns duplex-link $n3 $n1 1Mb 10ms DropTail

$ns duplex-link $n4 $n1 1Mb 10ms DropTail

$ns duplex-link $n5 $n1 1Mb 10ms DropTail


#Set queue length

$ns queue-limit $n0 $n1 5

$ns queue-limit $n2 $n1 2

$ns queue-limit $n3 $n1 5

$ns queue-limit $n4 $n1 2

$ns queue-limit $n5 $n1 2


#Label the nodes

$n0 label "ping0"

$n1 label "Router"

$n2 label "ping2"

$n3 label "ping3"

$n4 label "ping4"

$n5 label "ping5"


#Color the flow

$ns color 2 Blue

$ns color 3 Red

$ns color 4 Yellow

$ns color 5 Green

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 60


iii.Simulate to Find the Number of Packets Dropped by TCP/UDP

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 61


iv. Simulate to Find the Number of Packets Dropped due to Congestion
set ns [new Simulator]

set tf [open lab2.tr w]

$ns trace-all $tf


set nf [open lab2.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
$n0 label "Ping0"
$n4 label "Ping4"
$n5 label "Ping5"
$n6 label "Ping6"
$n2 label "Router"

$ns color 1 "red"


$ns color 2 "green"
$ns duplex-link $n0 $n2 100Mb 300ms DropTail
$ns duplex-link $n1 $n2 1Mb 300ms DropTail
$ns duplex-link $n3 $n2 1Mb 300ms DropTail
$ns duplex-link $n5 $n2 100Mb 300ms DropTail
$ns duplex-link $n2 $n4 1Mb 300ms DropTail
$ns duplex-link $n2 $n6 1Mb 300ms DropTail
$ns queue-limit $n0 $n2 5
$ns queue-limit $n2 $n4 3
$ns queue-limit $n2 $n6 2
$ns queue-limit $n5 $n2 5

#The below code is used to connect between the ping agents to the node n0,
#n4 , n5 and n6. set ping0 [new Agent/Ping]
$ns attach-agent $n0 $ping0
set ping4 [new Agent/Ping]
$ns attach-agent $n4 $ping4
set ping5 [new Agent/Ping]
$ns attach-agent $n5 $ping5
set ping6 [new Agent/Ping]
$ns attach-agent $n6 $ping6

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 62


$ping0 set packetSize_ 50000
$ping0 set interval_ 0.0001
$ping5 set packetSize_ 60000
$ping5 set interval_ 0.00001
$ping0 set class_ 1
$ping5 set class_ 2
$ns connect $ping0 $ping4
$ns connect $ping5 $ping6

#Define a 'recv' function for the class 'Agent/Ping'


#The below function is executed when the ping agent receives a reply from the destination

Agent/Ping instproc recv {from rtt}


{

$self instvar node_


puts " The node [$node_ id] received an reply from $from with round trip time of $rtt"

}
proc finish {}
{
global ns nf tf
exec nam lab2.nam &
$ns flush-trace
close $tf
close $nf
exit 0
}
#Schedule events
$ns at 0.1 "$ping0 send"
$ns at 0.2 "$ping0 send"
$ns at 0.3 "$ping0 send"
$ns at 0.4 "$ping0 send"

$ns at 0.5 "$ping0 send"


$ns at 0.6 "$ping0 send"
$ns at 0.7 "$ping0 send"
$ns at 0.8 "$ping0 send"
$ns at 0.9 "$ping0 send"
$ns at 1.0 "$ping0 send"
$ns at 1.1 "$ping0 send"
$ns at 1.2 "$ping0 send"
$ns at 1.3 "$ping0 send"
$ns at 1.4 "$ping0 send"

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 63


$ns at 1.5 "$ping0 send"
$ns at 1.6 "$ping0 send"
$ns at 1.7 "$ping0 send"
$ns at 1.8 "$ping0 send"
$ns at 0.1 "$ping5 send"
$ns at 0.2 "$ping5 send"
$ns at 0.3 "$ping5 send"
$ns at 0.4 "$ping5 send"
$ns at 0.5 "$ping5 send"
$ns at 0.6 "$ping5 send"
$ns at 0.7 "$ping5 send"
$ns at 0.8 "$ping5 send"
$ns at 0.9 "$ping5 send"
$ns at 1.0 "$ping5 send"
$ns at 1.1 "$ping5 send"
$ns at 1.2 "$ping5 send"
$ns at 1.3 "$ping5 send"
$ns at 1.4 "$ping5 send"
$ns at 1.5 "$ping5 send"
$ns at 1.6 "$ping5 send"
$ns at 1.7 "$ping5 send"
$ns at 1.8 "$ping5 send"
$ns at 5.0 "finish"
$ns run

AWK:
BEGIN
{
count=0;
}
{
if($1=="d")
count++;
}
END
{
printf("The Total no of Packets Drop is :%d\n\n", count);
}

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 64


Topology:

v. Simulate to Compare Data Rate& Throughput

P.SUDHAKAR Assistant Professor Computer Networks Lab(R20) Page 65

You might also like