Cn Lab Manual
Cn Lab Manual
(Autonomous)
LAB MANUAL
YEAR : 2024-25
REGULATION : R22
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++;
}
Output:
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
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.
#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;
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);
}
}
#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");
}
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
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++)
#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;
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
How to do it…
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
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:
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
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):
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
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]
# Filename: sample27.tcl
# Simulator Instance Creation
set ns [new Simulator]
# Node Creation
}
# Label and coloring
}
#Size of the node
}
#******************************Defining Communication Between node0 and all nodes
****************************8
}
# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
Throughput =
received_data*8/DataTransmissionPeriod #———
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. #
bits/s Execution:
ns sample27.tcl
AWK file (Open a new editor using “gedit command” and write awk file and save with “.awk”
extension)
BEGIN{
}
{
if($6 == "cwnd_")
{
printf("%f\t%f\n",$1,$7);
}
}
END{
}
#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
#Schedule the connection data flow; start sending data at T=0, stop at T=10.0