0% found this document useful (0 votes)
8 views

Cn Lab Manual

The document is a lab manual for the Computer Networks Lab course at Swarna Bharathi Institute of Science & Technology for the academic year 2024-25. It outlines the course objectives, outcomes, and a list of experiments that include implementing various networking protocols and algorithms, such as data link layer framing methods, CRC computation, and Dijkstra's algorithm. Additionally, it provides programming exercises in C for practical understanding of network concepts and tools.

Uploaded by

premaja.b
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Cn Lab Manual

The document is a lab manual for the Computer Networks Lab course at Swarna Bharathi Institute of Science & Technology for the academic year 2024-25. It outlines the course objectives, outcomes, and a list of experiments that include implementing various networking protocols and algorithms, such as data link layer framing methods, CRC computation, and Dijkstra's algorithm. Additionally, it provides programming exercises in C for practical understanding of network concepts and tools.

Uploaded by

premaja.b
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

SWARNA BHARATHI INSTITUTE OF SCIENCE & TECHNOLOGY

(Autonomous)

LAB MANUAL

DEPARTMENT OF MASTER OF COMPUTER APPLICATONS

YEAR : 2024-25

REGULATION : R22

COURSE NAME: COMPUTER NETWORKS LAB

COURSE CODE:

1
COMPUTER NETWORKS LAB
MCA I Year II Sem. L T P C
0 0 3 1.5

Course Objectives
 To understand the working principle of various communication protocols.
 To understand the network simulator environment and visualize a network topology and observe its
performance
 To analyze the traffic flow and the contents of protocol frames

Course Outcomes
 Implement data link layer farming methods
 Analyze error detection and error correction codes.
 Implement and analyze routing and congestion issues in network design.
 Implement Encoding and Decoding techniques used in presentation layer
 To be able to work with different network tools

List of Experiments
1. Implement the data link layer framing methods such as character, character-stuffing and bit stuffing.
2. Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC CCIP
3. Develop a simple data link layer that performs the flow control using the sliding window protocol, and loss
recovery using the Go-Back-N mechanism.
4. Implement Dijsktra’s algorithm to compute the shortest path through a network
5. Take an example subnet of hosts and obtain a broadcast tree for the subnet.
6. Implement distance vector routing algorithm for obtaining routing tables at each node.
7. Implement data encryption and data decryption
8. Write a program for congestion control using Leaky bucket algorithm.
9. Write a program for frame sorting techniques used in buffers.
10. Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters.
11. How to run Nmap scan
12. Operating System Detection using Nmap
13. 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.
vi. Simulate to Plot Congestion for Different Source/Destination
vii. Simulate to Determine the Performance with respect to Transmission of Packets

TEXT BOOK:
1. Computer Networks, Andrew S Tanenbaum, David. j. Wetherall, 5th Edition. Pearson Education/PHI

REFERENCES:
1. An Engineering Approach to Computer Networks, S. Keshav, 2nd Edition, Pearson Education
2. Data Communications and Networking – Behrouz A. Forouzan. 3rd Edition, TM

2
1.Implemeny thedata link layer framing methods such as character, character-stuffing
and bit stuffing

Write a C program to implement the data link layer framing methods such as
bit stuffing.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
clrscr();
printf("enter frame length:");
scanf("%d",&n);
pritf("enter input frame(0's&1's only):");
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;
}
}
else
{
b[j]=a[i];
} i++;
j++;
}

DEPT OF MCA COMPUTER NETWORKS LAB


3
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
getch();
}

Output:

Enter the number of


bits: 10
1
0
1
0
1
1
1
1
1
1
1
Data after stuffing: 10101111101
Write a C program to implement the data link layer framing method such as
character stuffing.
Program Code: //Program for Character Stuffing
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
printf("Enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("Enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);}
printf("Enter the character\
n"); ch=getche();

DEPT OF MCA COMPUTER NETWORKS LAB


4
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i]; i+
+;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\nframe after stuffing:\
n"); printf("%s",b);
}
Program Output:
Enter string
SBIT

DEPT OF MCA COMPUTER NETWORKS LAB


5
Enter position
2
Enter the character
frame after stuffing:
dlestxSdldleBITMdleetx

DEPT OF MCA COMPUTER NETWORKS LAB


6
2.Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC
CCIP

// program for Cyclic Redundancy Check


#include<stdio.h>
#include<conio.h>
int main(void)
{
int data[50],div[16],rem[16];
int datalen, divlen, i,j,k;
int ch;
clrscr();
printf("Enter the data: ");
i = 0;
while((ch = fgetc(stdin)) != '\n')
{
if(ch == '1')
data[i] = 1;
else
data[i] = 0;
i++;
}
datalen = i;
printf("\nEnter the divisor: ");
i = 0;
while((ch = fgetc(stdin)) != '\n')
{
if(ch == '1')
div[i] = 1;
else
div[i] = 0;
i++;
}
divlen = i;
for(i = datalen ; i < datalen + divlen - 1 ; i++)
data[i] = 0;
datalen = datalen + divlen - 1;
for(i = 0 ; i < divlen ; i++)
rem[i] = data[i];
k = divlen-1;
while(k < datale

DEPT OF MCA COMPUTER NETWORKS LAB


7
if(rem[0] == 1)
{
for(i = 0 ; i < divlen ; i++)
rem[i] = rem[i] ^ div[i];
}
else
{
if(k == datalen-1)
break;
for(i = 0 ; i < divlen-1 ; i++)
{
rem[i] = rem[i+1];
printf("%d",rem[i]);
}
rem[i] = data[++k];
printf("%d\n",rem[i]);
}
j=1;
for(i = datalen - divlen + 1 ; i < datalen ; i++)
{
data[i] = rem[j++];
}
printf("\nThe data to be sent is\n");
for(i = 0 ; i < datalen ; i++)
printf("%d",data[i]);
getch();
return 0;
}
OUTPUT:
Enter the data: 10101111 Enter the divisor: 1011
0011
0111
1111
1001
0100
1000
0110
The data to be sent is 10101111110

DEPT OF MCA COMPUTER NETWORKS LAB


8
DEPT OF MCA COMPUTER NETWORKS LAB
9
3.Develop a simple data link layer that performs the flow control using the sliding window
protocol, and loss recovery using the Go-Back-N mechanism

SLIDING WINDOW PROTOCOL,


#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;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);

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

output:-

Output
Enter window size: 3
Enter number of frames to transmit: 5
Enter 5 frames: 12 5 89 4 6
With sliding window protocol the frames will be sent in the following manner (assuming
no corruption of frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the
receiver
12 5 89
Acknowledgement of above frames sent is received by
sender 4 6
Acknowledgement of above frames sent is received by sender

DEPT OF MCA COMPUTER NETWORKS LAB


10
GO BACK N PROTOCOL
#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;
}
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.

DEPT OF MCA COMPUTER NETWORKS LAB


11
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. 8

DEPT OF MCA COMPUTER NETWORKS LAB


12
4.Implement Dijsktra’s algorithm to compute the shortest path through a network

Write a C program to Implement Dijkstra’s Algorithm to compute the


shortest path through a given path.

#include<stdio.h>
#include<conio.h>
#define INFINITY
#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
//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;

DEPT OF MCA COMPUTER NETWORKS LAB


13
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=dista
nce[i];
nextnode=i;
}
//check if a better path exists through
nextnode visited[nextnode]=1;
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);
}
}

DEPT OF MCA COMPUTER NETWORKS LAB


14
DEPT OF MCA COMPUTER NETWORKS LAB
15
5.Take an example subnet of hosts and obtain a broadcast tree for the subnet.
#include<stdio.h>
int a[10][10],n;
main()
{
int i,j,root; clrscr();
printf("Enter no.of nodes:");
scanf("%d",&n);
printf("Enter adjacent matrix\
n"); for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("Enter connecting of %d>%d::",i,j);
scanf("%d",&a[i][j]);
}
printf("Enter root node:"); scanf("%d",&root);
adj(root);
}
adj(int k)
{
int i,j;
printf("Adjacent node of root node::\n");
printf("%d\n",k);
for(j=1;j<=n;j++)
{
if(a[k][j]==1 || a[j][k]==1) printf("%d\t",j);
}
printf("\n"); for(i=1;i<=n;i++)
{
if((a[k][j]==0) && (a[i][k]==0) && (i!=k))
printf("%d",i);
}
} OUTPUT:
Enter no.of nodes: 5 Enter adjacent matrix
Enter connecting of 1–>1::0 Enter connecting of 1–>2::1 Enter connecting of 1–>3::1
Enter connecting of 1–>4::0 Enter connecting of 1–>5::0 Enter connecting of 2–>1::1
Enter connecting of 2–>2::0 Enter connecting of 2–>3::1 Enter connecting of 2–>4::1
Enter connecting of 2–>5::0 Enter connecting of 3–>1::1 Enter connecting of 3–>2::1
Enter connecting of 3–>3::0 Enter connecting of 3–>4::0 Enter connecting of 3–>5::0
Enter connecting of 4–>1::0 Enter connecting of 4–>2::1 Enter connecting of 4–>3::0
Enter connecting of 4–>4::0 Enter connecting of 4–>5::1 Enter connecting of 5–>1::0
Enter connecting of 5–>2::0
Enter connecting of 5–>3::0 Enter connecting of 5–>4::1 Enter connecting of 5–
>5::0 Enter root node:2
Adjacent node of root node:
2
134
5

DEPT OF MCA COMPUTER NETWORKS LAB


16
6.Implement distance vector routing algorithm for obtaining routing tables at each node.

#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
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<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}

DEPT OF MCA COMPUTER NETWORKS LAB


17
OUTPUT:
[cseb332@Insat5E ~]$ cc dst.c
[cseb332@Insat5E ~]$ ./a.out

Enter the number of nodes : 4


Enter the cost matrix:
0 3 5 99
3 0 99 1
5402
99 1 2 0
State value for router 1 is
node 1 via 1 Distance0
node 2 via 2 Distance3
node 3 via 3 Distance5
node 4 via 2 Distance4
State value for router 2 is
node 1 via 1 Distance3
node 2 via 2 Distance0
node 3 via 4 Distance3
node 4 via 4 Distance1
State value for router 3 is
node 1 via 1 Distance5
node 2 via 4 Distance3
node 3 via 3 Distance0
node 4 via 4 Distance2
State value for router 4 is
node 1 via 2 Distance4
node 2 via 2 Distance1
node 3 via 3 Distance2
node 4 via 4 Distance0

DEPT OF MCA COMPUTER NETWORKS LAB


18
7.Implement data encryption and data decryption
#include<stdio.h>
#include<conio.h>
#include<string.h>
int p10[]={3,5,2,7,4,10,1,9,8,6},
p8[]={6,3,7,4,8,5,10,9},
p4[]={2,4,3,1};
int ip[]={2,6,3,1,4,8,5,7},
ipinv[]={4,1,3,5,7,2,8,6},
ep[]={4,1,2,3,2,3,4,1};
int s0[][4]={{1,0,3,2,},{3,2,1,0},{0,2,1,3,},{3,1,3,2}};
int s1[][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
void permute(char
op[],char ip[],int p[], int n){
int i;
for(i=0;i<n;i++) op[i]=ip[p[i]-1]; op[i]='\0';
}
void circularls(char pr[],int n){ int i;
char ch=pr[0]; for(i=0;i<n-1;i++) pr[i]=pr[i+1]; pr[i]=ch;
}
void keygen(char k1[],char k2[],char key[]){ char keytemp[11];
permute(keytemp,key,p10,10;
circularls(keytemp,5);
circularls(keytemp+5,5);
permute(k1,keytemp,p8,8);
circularls(keytemp,5);
circularls(keytemp,5);
circularls(keytemp+5,5);
circularls(keytemp+5,5);
permute(k2,keytemp,p8,8);
}
void xor(char op[],char ip[]){ int i;
for(i=0;i<strlen(op)&&i<str
len(ip);i++) op[i]=(op[i]-
'0')^(ip[i]-'0')+'0';
}
void sbox(char op[],char ip[],int s[][4]) { int value;
value=s[(ip[0]-'0')*2+(ip[3]-'0')][(ip[1]-'0')*2+(ip[2]-'0')];
op[0]=value/2+'0'; op[1]=value%2+'0'; op[2]='\0';
}
void fk(char op[],char ip[],char k[])
{
char l[5],r[5],tmp[9],tmp1[9],tmp2[9]; strncpy(l,ip,4);
l[4]='\0';
strncpy(r,ip+4,4); r[4]='\0';

DEPT OF MCA COMPUTER NETWORKS LAB


19
permute(tmp,r,ep,8); xor(tmp,k);
sbox(tmp1,tmp,s0); sbox(tmp2,tmp+4,s1); strcat(tmp1,tmp2); permute(tmp,tmp1,p4,4);
xor(tmp,l);
strcat(tmp,r); strcpy(op,tmp);
}
void sw(char pr[]) { char tmp[9]; strncpy(tmp,pr+4,4); strncpy(tmp+4,pr,4); tmp[8]='\0';
strcpy(pr,tmp);
}
void main()
{
char key[11],k1[9],k2[9],plain[9],cipher[9],tmp[9];
clrscr(); printf("enter 10 bit key:"); gets(key);
if(strlen(key)!=10) printf("invalid key length !!"); else
{
keygen(k1,k2,key); printf("sub key k1::"); puts(k1); printf("subkey k2::"); puts(k2);
printf("enter 8 bit plain text:"); gets(plain);
if(strlen(plain)!=8) printf("invalid length plain text !!"); permute(tmp,plain,ip,8);
fk(cipher,tmp,k1); sw(cipher); fk(tmp,cipher,k2); permute(cipher,tmp,ipinv,8);
printf("cipher teaxt is::"); puts(cipher);
/* decryption process*/ permute(tmp,cipher,ip,8); fk(plain,tmp,k2); sw(plain);
fk(tmp,plain,k1); permute(plain,tmp,ipinv,8); printf("decrypted text is::"); puts(plain);
}
getch();
}

OUTPUT:
Enter 10 bit key: 1456987203 Sub key k1::17062538
Sub keyk2::25893401
Enter 8 bit plain text: computer Cipher text is::epfnmrct
Decrypted text is::computer

DEPT OF MCA COMPUTER NETWORKS LAB


20
8.Write a program for congestion control using Leaky bucket algorithm.
#include<stdio.h>
#include<string.h>
int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}

int main()
{
int drop=0,mini,nsec,cap,count=0,i,inp[25],process;
syste("clear");
printf("Enter The Bucket Size\n");
scanf("%d",&cap);
printf("Enter The Processing Rate\n");
scanf("%d",&process);
printf("Enter The No. Of Seconds You Want To Stimulate\n");
scanf("%d",&nsec);
for(i=0;i<nsec;i++)
{
printf("Enter The Size Of The Packet Entering At %d sec\n",i+1);
scanf("%d",&inp[i]);
}
printf("\nSecond|Packet Recieved|Packet Sent|Packet Left|Packet Dropped|\n");
printf(" \n");
for(i=0;i<nsec;i++)
{
count+=inp[i];
if(count>cap)
{
drop=count-cap;
count=cap;
}
printf("%d",i+1);
printf("\t%d",inp[i]);
mini=min(count,process);
printf("\t\t%d",mini);
count=count-mini;
printf("\t\t%d",count);
printf("\t\t%d\n",drop);
drop=0;
}
for(;count!=0;i++)

DEPT OF MCA COMPUTER NETWORKS LAB


21
{
if(count>cap)
{
drop=count-cap;
count=cap;
}
printf("%d",i+1);
printf("\t0");
mini=min(count,process);
printf("\t\t%d",mini);
count=count-mini;
printf("\t\t%d",count);
printf("\t\t%d\n",drop);
}
}
$ cc –o Congestion Congestion.c
$ ./Congestion
Enter The Bucket Size
5
Enter The Processing Rate
2
Enter The No. Of Seconds You Want To Stimulate
3
Enter The Size Of The Packet Entering At 1 sec
5
Enter The Size Of The Packet Entering At 1 sec
4
Enter The Size Of The Packet Entering At 1 sec
3
Second|Packet Recieved|Packet Sent|Packet Left|Packet Dropped|
1 5 2 3 0
2 4 2 3 2
3 3 2 3 1
4 0 2 1 0
5 0

DEPT OF MCA COMPUTER NETWORKS LAB


22
9.Write a program for frame sorting technique used in buffers.

#include<stdio.h>
#include<string.h>
#define FRAM_TXT_SIZ 3
#define MAX_NOF_FRAM 127
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
struct frame // structure maintained to hold frames
{ char
text[FRAM_TXT_SIZ]; int
seq_no;
}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
int assign_seq_no() //function which splits message
{ int k=0,i,j; //into frames and assigns sequence no
for(i=0; i < strlen(str); k++)
{ fr[k].seq_no = k;
for(j=0; j < FRAM_TXT_SIZ && str[i]!='\0'; j++)
fr[k].text[j] = str[i++];
}
printf("\nAfter assigning sequence numbers:\n");
for(i=0; i < k; i++)
printf("%d:%s ",i,fr[i].text);
return k; //k gives no of frames
}
void generate(int *random_ary, const int limit) //generate array of random nos
{ int r, i=0, j;
while(i <
limit)
{ r = random() % limit;

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


if( random_ary[j] == r )
Break;
if( i==j ) random_ary[i++] = r;
}}
void shuffle( const int no_frames ) // function shuffles the frames
{
int i, k=0, random_ary[no_frames];
generate(random_ary, no_frames);
for(i=0; i < no_frames; i++)
shuf_ary[i] = fr[random_ary[i]];
DEPT OF MCA COMPUTER NETWORKS LAB
23
printf("\n\nAFTER SHUFFLING:\n");
for(i=0; i < no_frames; i++)
printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);
}
void sort(const int no_frames) // sorts the frames
{
int i,j,flag=1;
struct frame
hold;
for(i=0; i < no_frames-1 && flag==1; i++) // search for frames in sequence
{
flag=0;
for(j=0; j < no_frames-1-i; j++) //(based on seq no.) and display
if(shuf_ary[j].seq_no > shuf_ary[j+1].seq_no)
{
hold = shuf_ary[j];
shuf_ary[j] = shuf_ary[j+1];
shuf_ary[j+1] = hold;
flag=1;
}
}
}
int main()
{
int no_frames,i;
printf("Enter the message: ");
gets(str);
no_frames = assign_seq_no();
shuffle(no_frames);
sort(no_frames); printf("\n\
nAFTER SORTING\n");
for(i=0;i<no_frames;i++)
printf("%s",shuf_ary[i].text);
printf("\n\n");
}

[root@localhost nwcn]# ./a.out


Enter the message: Welcome To Acharya Institute of Technology
After assigning sequence numbers:
0:Wel 1:com 2:e T 3:o A 4:cha 5:rya 6: In 7:sti 8:tut 9:e o 10:f T 11:ech 12:nol 13:ogy
AFTER SHUFFLING:

DEPT OF MCA COMPUTER NETWORKS LAB


24
1:com 4:cha 9:e o 5:rya 3:o A 10:f T 2:e T 6: In 11:ech 13:ogy 0:Wel 8:tut 12:nol 7:sti
AFTER SORTING
Welcome To Acharya Institute of Technology

DEPT OF MCA COMPUTER NETWORKS LAB


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

Capturing Packets
After downloading and installing Wires hark, you can launch it and double-click the name of a
network interface under Capture to start capturing packets on that interface. For example, if you
want to capture traffic on your wireless network, click your wireless interface. You can configure
advanced features by clicking Capture > Options, but this isn’t necessary for now

DEPT OF MCA COMPUTER NETWORKS LAB


26
Inspecting Packets
Click a packet to select it and you can dig down to view its details.

Start Wires hark, click on Statistics.

How to do it…

1. From the Statistics menu, choose Capture File Properties:

DEPT OF MCA COMPUTER NETWORKS LAB


27
DEPT OF MCA COMPUTER NETWORKS LAB
28
11. How to run Nmap scan
Nmap is one of the most popular network mappers in the infosec world. It’s utilized by cyber
security professionals and newbies alike to audit and discover local and remote open ports, as
well as hosts and network information.
Nmap can be used to:

 Create a complete computer network map.


 Find remote IP addresses of any hosts.
 Get the OS system and software details.
 Detect open ports on local and remote systems.
 Audit server security standards.
 Find vulnerabilities on remote and local hosts.
Nmap command examples
Let’s get to know a few useful command-line based scans that can be performed using Nmap.

1. Basic Nmap Scan against IP or host

nNow, if you want to scan a hostname, simply replace the IP for the host, as you see below:

nmap cloudflare.com

These kinds of basic scans are perfect for your first steps when starting with Nmap

2. Scan specific ports or scan entire port ranges on a local or remote

server nmap -p 1-65535 localhost

In this example, we scanned all 65535 ports for our localhost computer.

Nmap is able to scan all possible ports, but you can also scan specific ports, which will report
faster results. See below:

nmap -p 80,443 8.8.8.8

DEPT OF MCA COMPUTER NETWORKS LAB


29
12.Operating System Detection using Nmap

Detect OS and services

This is the command to scan and search for the OS (and the OS version) on a host. This
command will provide valuable information for the enumeration phase of your network security
assessment (if you only want to detect the operating system, type nmap -O 192.168.0.9):

nmap -A 192.168.0.9

Standard service detection

This is the command to scan for running service. Nmap contains a database of about 2,200 well-
known services and associated ports. Examples of these services are HTTP (port 80), SMTP
(port 25), DNS (port 53), and SSH (port 22):

nmap -sV 192.168.0.9

DEPT OF MCA COMPUTER NETWORKS LAB


30
DEPT OF MCA COMPUTER NETWORKS LAB
31
13.Do the following using NS2 Simulator

i.NS-2 Simulator Introduction

NS2 stands for Network Simulator Version 2. It is an open-source event-driven simulator


designed specifically for research in computer communication networks.

Features of NS2
1. It is a discrete event simulator for networking research.
2. It provides substantial support to simulate bunch of protocols like TCP, FTP, UDP, https and
DSR.
3. It simulates wired and wireless network.
4. It is primarily Unix based.
5. Uses TCL as its scripting language.
6. Otcl: Object oriented support
7. Tclcl: C++ and otcl linkage
8. Discrete event

scheduler Basic

Architecture

NS2 consists of two key languages: C++ and Object-oriented Tool Command Language
(Outcall). While the C++ defines the internal mechanism (i.e., a backend) of the simulation
objects, the OTcl sets up simulation by assembling and configuring the objects as well as
scheduling discrete events. The C++ and the OTcl are linked together using TclCL

DEPT OF MCA COMPUTER NETWORKS LAB


32
ii Simulate to Find the Number of Packets

Create a new Simulation Instance


set ns [new Simulator]
set bandwidth 1.75Mb
#Turn on the Trace and the animation files
set f [open out.tr w]
set nf [open out.nam w]
$ns trace-all $f
$ns namtrace-all $nf
#Define the finish procedure to perform at the end of the simulation

proc finish {} {
global f nf ns
$ns flush-trace
close $f
close $nf
exec nam out.nam &
#exec awk -f 1.awk out.tr &
exit 0
}
#Create the
nodes set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node]

#Label the nodes


$n0 label "TCP Source"
$n1 label "UDP Source"
$n2 label "Sink"
#Set the color
$ns color 1 red
$ns color 2 yellow
#Create the Topology
$ns duplex-link $n0 $n1 $bandwidth 10ms DropTail
$ns duplex-link $n1 $n2 1.75Mb 20ms DropTail
#Attach a Queue of size N Packets between the nodes n1 n2
$ns queue-limit $n1 $n2 10
#Make the Link Orientation
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right

DEPT OF MCA COMPUTER NETWORKS LAB


33
#Create a UDP Agent and attach to the node n1
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
#Create a CBR Traffic source and attach to the UDP Agent
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
#Specify the Packet Size and interval
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
#Create a Null Agent and attach to the node n2
set null0 [new Agent/Null]
$ns attach-agent $n2 $null0
#Connect the CBR Traffic source to the Null agent
$ns connect $udp0 $null0
#Create a TCP agent and attach to the node n0
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
#Create a FTP source and attach to the TCP agent
set ftp0 [new Application/FTP]
#Attach the FTP source to the TCP Agent
$ftp0 attach-agent $tcp0
#Create a TCPSink agent and attach to the node n2
set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink
#Specify the Max file Size in Bytes
$ftp0 set maxPkts_ 1000
#Connect the TCP Agent with the TCPSink
$ns connect $tcp0 $sink
$udp0 set class_ 1
$tcp0 set class_ 2
#Schedule the Events
$ns at 0.1 "$cbr0 start"
$ns at 1.0 "$ftp0 start"
$ns at 4.0 "$ftp0 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run

DEPT OF MCA COMPUTER NETWORKS LAB


34
Now run the code using
ns filename.tcl
you will get something like this:

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

Create a new Simulation Instance


set ns [new Simulator]
set bandwidth 1.75Mb
#Turn on the Trace and the animation files
set f [open out.tr w]
set nf [open out.nam w]
$ns trace-all $f
$ns namtrace-all $nf
#Define the finish procedure to perform at the end of the simulation
proc finish {} {
global f nf ns
$ns flush-trace
close $f
close $nf
exec nam out.nam &
#exec awk -f 1.awk out.tr &
exit 0
}
#Create the
nodes set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node] #Label the
nodes
$n0 label "TCP Source"
$n1 label "UDP Source"
$n2 label
"Sink" #Set the
color
$ns color 1 red

DEPT OF MCA COMPUTER NETWORKS LAB


35
$ns color 2 yellow
#Create the Topology
$ns duplex-link $n0 $n1 $bandwidth 10ms DropTail
$ns duplex-link $n1 $n2 1.75Mb 20ms DropTail
#Attach a Queue of size N Packets between the nodes n1 n2
$ns queue-limit $n1 $n2 10
#Make the Link Orientation
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
#Create a UDP Agent and attach to the node
n1 set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
#Create a CBR Traffic source and attach to the UDP Agent
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
#Specify the Packet Size and interval
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
#Create a Null Agent and attach to the node n2
set null0 [new Agent/Null]
$ns attach-agent $n2 $null0
#Connect the CBR Traffic source to the Null agent
$ns connect $udp0 $null0
#Create a TCP agent and attach to the node n0
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
#Create a FTP source and attach to the TCP agent
set ftp0 [new Application/FTP]
#Attach the FTP source to the TCP Agent
$ftp0 attach-agent $tcp0
#Create a TCPSink agent and attach to the node n2
set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink
#Specify the Max file Size in Bytes
$ftp0 set maxPkts_ 1000
#Connect the TCP Agent with the TCPSink
$ns connect $tcp0 $sink
$udp0 set class_ 1

$tcp0 set class_ 2


#Schedule the
Events
$ns at 0.1 "$cbr0 start"
$ns at 1.0 "$ftp0 start"
DEPT OF MCA COMPUTER NETWORKS LAB
36
$ns at 4.0 "$ftp0 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run

iv Simulate to Find the Number of Packets Dropped due to Congestion


#Create a new Simulation Instance
set ns [new Simulator]
set bandwidth 1.75Mb
#Turn on the Trace and the animation files
set f [open out.tr w]
set nf [open out.nam w]
$ns trace-all $f
$ns namtrace-all $nf
#Define the finish procedure to perform at the end of the simulation
proc finish {}
{ global f nf ns
$ns flush-trace
close $f
close $nf
exec nam out.nam &
#exec awk -f 1.awk out.tr &
exit 0
}
#Create the
nodes set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node]

#Label the nodes


$n0 label "TCP Source"
$n1 label "UDP Source"
$n2 label "Sink"

#Set the color

37 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
$ns color 1 red
$ns color 2 yellow
#Create the Topology
$ns duplex-link $n0 $n1 $bandwidth 10ms DropTail
$ns duplex-link $n1 $n2 1.75Mb 20ms DropTail
#Attach a Queue of size N Packets between the nodes n1 n2
$ns queue-limit $n1 $n2 10
#Make the Link Orientation
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
#Create a UDP Agent and attach to the node n1
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
#Create a CBR Traffic source and attach to the UDP Agent
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
#Specify the Packet Size and interval
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
#Create a Null Agent and attach to the node n2
set null0 [new Agent/Null]
$ns attach-agent $n2 $null0
#Connect the CBR Traffic source to the Null agent
$ns connect $udp0 $null0
#Create a TCP agent and attach to the node n0
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
#Create a FTP source and attach to the TCP agent
set ftp0 [new Application/FTP]
#Attach the FTP source to the TCP Agent
$ftp0 attach-agent $tcp0
#Create a TCPSink agent and attach to the node n2
set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink
#Specify the Max file Size in Bytes
$ftp0 set maxPkts_ 1000
#Connect the TCP Agent with the TCPSink

38 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
$ns connect $tcp0 $sink
$udp0 set class_ 1
$tcp0 set class_ 2
#Schedule the Events
$ns at 0.1 "$cbr0 start"
$ns at 1.0 "$ftp0 start"
$ns at 4.0 "$ftp0 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run

v. Simulate to Compare Data Rate & Throughput

# Filename: sample27.tcl
# Simulator Instance Creation
set ns [new Simulator]

#Fixing the co-ordinate of simulation area


set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type

39 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 3 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 500 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end

# set up topography object


set topo [new
Topography]
$topo load_flatgrid $val(x) $val(y)

#Nam File Creation nam – network animator


set namfile [open sample27.nam w]

#Tracing all the events and cofiguration


$ns namtrace-all-wireless $namfile $val(x) $val(y)

#Trace File creation


set tracefile [open sample27.tr w]

#Tracing all the events and cofiguration


$ns trace-all $tracefile

# general operational descriptor- storing the hop details in the network


create-god $val(nn)

# configure the nodes


$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \

40 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
-macTrace OFF \
-movementTrace ON

# Node Creation

for {set i 0} {$i < 3} {incr i} {

set node_($i) [$ns node]


$node_($i) color black

#Location fixing for a single node

$node_(0) set X_ 258


$node_(0) set Y_ 222
$node_(0) set Z_ 0

for {set i 1} {$i < 3} {incr i} {

$node_($i) set X_ [expr rand()*$val(x)]


$node_($i) set Y_ [expr rand()*$val(y)]
$node_($i) set Z_ 0

}
# Label and coloring

for {set i 0} {$i < 3} {incr i} {

$ns at 0.1 "$node_($i) color blue"


$ns at 0.1 "$node_($i) label Node$i"

}
#Size of the node

for {set i 0} {$i < 3} {incr i} {

$ns initial_node_pos $node_($i) 30

}
#******************************Defining Communication Between node0 and all nodes
****************************8

41 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
for {set i 1} {$i < 3} {incr i} {

# Defining a transport agent for sending


set udp [new Agent/UDP]

# Attaching transport agent to sender node


$ns attach-agent $node_($i) $udp

# Defining a transport agent for receiving


set null [new Agent/Null]

# Attaching transport agent to receiver node


$ns attach-agent $node_(0) $null

#Connecting sending and receiving transport agents


$ns connect $udp $null

#Defining Application instance


set cbr [new Application/Traffic/CBR]

# Attaching transport agent to application agent


$cbr attach-agent $udp

#Packet size in bytes and interval in seconds definition


$cbr set packetSize_ 512
$cbr set interval_ 0.1

# data packet generation starting time


$ns at 1.0 "$cbr start"

# data packet generation ending time


#$ns at 6.0 "$cbr stop"

}
# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"

#Stopping the scheduler


$ns at 10.01 "puts \"end simulation\" ; $ns halt"
#$ns at 10.01 "$ns halt"

42 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
exec awk -f Throughput.awk sample27.tr > Throughput.tr &
#Starting scheduler
$ns run
#filename:Throughput.awk

#——— Formula ————:

Throughput =

received_data*8/DataTransmissionPeriod #———

AWK script Format——–#


The script has the following format:
BEGIN {}
{
content
}
END {}
Begin part comprises of initialization of variable.
Commands in the content part scan every row of trace file only once.
Ex:
if (pattern) {
action;
}

If the pattern is matched with the line in the trace in the trace file specified action will be
performed.

In the END part final calculation is performed on the data obtained from the content part. #

——— Steps ————:

1. set pattern and action for received data


2. apply the received data value in the formula to calculate throughput in

bits/s Execution:

ns sample27.tcl

43 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
vi. Simulate to Plot Congestion for Different Source/Destination

set ns [new Simulator]


set nf [open "lab3.nam" w]
$ns namtrace-all $nf
set tf [open "lab3.tr"
w]
$ns trace-all $tf
set n0 [$ns node]
$n0 label "src1"
$n0 color "magenta"

set n1 [$ns node]


$n1 color "red"
set n2 [$ns node]
$n2 label "src2"
$n2 color "magenta"
set n4 [$ns node]
$n4 color "red"
set n3 [$ns node]
$n3 label "dest2"
$n3 color "blue"
set n5 [$ns node]
$n5 label "dest1"

44 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
$n5 color "blue"
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 10ms LL Queue/DropTail Mac/802_3
$ns duplex-link $n4 $n5 100Mb 10ms DropTail
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 1000
$ftp0 set interval_ 0.005
set Sink0 [new Agent/TCPSink]
$ns attach-agent $n5 $Sink0
$ns connect $tcp0 $Sink0
set tcp1 [new Agent/TCP]
$ns attach-agent $n2 $tcp1
set ftp1 [ new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set packetSize_ 10000
$ftp1 set interval_ 0.0005
set Sink1 [new Agent/TCPSink]
$ns attach-agent $n3 $Sink1
$ns connect $tcp1 $Sink1
set file0 [open "file0.tr"
w]
$tcp0 attach $file0
set file1 [open "file1.tr" w]
$tcp1 attach $file1
$tcp0 trace cwnd_
$tcp1 trace
cwnd_ proc finish
{ } { global ns nf
tf
$ns flush-trace
close $nf
close $tf
exec nam lab3.nam &
exit 0
}
$ns at 0.1 "$ftp0 start"
$ns at 0.5 "$ftp0 stop"
$ns at 0.8 "$ftp0 start"
$ns at 0.9 "$ftp1 start"
$ns at 1.1 "$ftp1 stop"
$ns at 1.5 "$ftp0 stop"

45 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
$ns at 1.8 "$ftp1 start"
$ns at 2.2 "$ftp1 stop"
$ns at 2.9 "finish"
$ns run

AWK file (Open a new editor using “gedit command” and write awk file and save with “.awk”
extension)

cwnd:- means congestion window

BEGIN{
}
{
if($6 == "cwnd_")
{
printf("%f\t%f\n",$1,$7);
}
}
END{
}

vii. Simulate to Determine the Performance with respect to Transmission of Packets

# basic1.tcl simulation: A---R---B

#Create a simulator object


set ns [new Simulator]

#Open the nam file basic1.nam and the variable-trace file basic1.tr
set namfile [open basic1.nam w]
$ns namtrace-all $namfile
set tracefile [open basic1.tr w]
$ns trace-all $tracefile

#Define a 'finish' procedure


proc finish {} {
global ns namfile tracefile
$ns flush-trace
close $namfile
close $tracefile
exit 0
}

46 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
#Create the network nodes
set A [$ns node]
set R [$ns node]
set B [$ns node]

#Create a duplex link between the nodes

$ns duplex-link $A $R 10Mb 10ms DropTail


$ns duplex-link $R $B 800Kb 50ms DropTail

# The queue size at $R is to be 7, including the packet being sent


$ns queue-limit $R $B 7

# some hints for nam


# color packets of flow 0 red
$ns color 0 Red
$ns duplex-link-op $A $R orient right
$ns duplex-link-op $R $B orient right
$ns duplex-link-op $R $B queuePos 0.5

# Create a TCP sending agent and attach it to A


set tcp0 [new Agent/TCP/Reno]
# We make our one-and-only flow be flow 0
$tcp0 set class_ 0
$tcp0 set window_ 100
$tcp0 set packetSize_ 960
$ns attach-agent $A $tcp0

# Let's trace some variables


$tcp0 attach $tracefile
$tcp0 tracevar cwnd_
$tcp0 tracevar ssthresh_
$tcp0 tracevar ack_
$tcp0 tracevar maxseq_

#Create a TCP receive agent (a traffic sink) and attach it to B


set end0 [new Agent/TCPSink]
$ns attach-agent $B $end0

#Connect the traffic source with the traffic sink


$ns connect $tcp0 $end0

#Schedule the connection data flow; start sending data at T=0, stop at T=10.0

DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB
47
set myftp [new Application/FTP]
$myftp attach-agent $tcp0
$ns at 0.0 "$myftp start"
$ns at 10.0 "finish"

#Run the simulation


$ns run

48 DEPT OF MCA COMPUTER NETWORKS LAB


LLALAB

You might also like