Computer Network Lab Manual r22 CSD
Computer Network Lab Manual r22 CSD
Computer Network Lab Manual r22 CSD
Prepared By
INDEX
2 COs,POs, PSOs&PEOs
3 Syllabus
4 List of Experiments
5 Programs
Mission of the trends in industry and to invoke the desire of innovation as a process
Institute of lifelong learning for a successful career in engineering and
management.
To evolve the department of computer science and engineering as a
Vision of the center of academic excellence with latest technologies to transform
Department
students into innovative global leaders.
M1:To produce competitive graduates having creative skills and
Ethical values to succeed in their fields as well as the foundation for
lifelong learning.
Mission of the
M2: By promoting research and development activities
Department
in collaboration with reputed industries and laboratories.
M3:Toanalyze,design and develop high quality software systems
Using the appropriate theory, principles, tools and processes.
Course Outcomes
PROGRAMOUTCOMES
To prepare students to fit into any industry associated with developing and
PEO3
implementation of software products or technologies.
To equip the graduates with ability to analyze, design and test the novel
PEO4
Products.
Course Outcomes
1. Implement data link layer farming methods
2. Analyze error detection and error correction codes.
3. Implement and analyze routing and congestion issues in network design.
4. Implement Encoding and Decoding techniques used in presentation layer
5. 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.
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.
1. Implement the data 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++;
}
Enterthenumberof
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.
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("\n frame after stuffing:\
n"); printf("%s",b);
}
Program Output:
Enter string
MLRITM
Enter position
2
Enter the character frame
after stuffing:
dlestxMdldleLRITMdleetx
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();
return0;
}
OUTPUT:
Enter the data:10101111 Enter the divisor:1011
0011
0111
1111
1001
0100
1000
0110
The data to be sent is 10101111110
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
if(ack==windowsize)
break;
else
} sent=ack;
return0;
}
output:-
Output
Enterwindowsize: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 a teach stage sender waits for acknowledgement sent by the
receiver
12589
Acknowledgement of above frames sent is received by
sender 4 6
Acknowledgement of above frames sent is received by sender
GOBACKN 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("\n Please enter the last Acknowledgement received.\n");
scanf("%d",&ack);
if(ack==windowsize)
break;
else
sent=ack;
}
return0;
}
output:-
enter window
size 8
Frame0 has been transmitted.
Frame1 has been transmitted.
Frame2 has been transmitted.
Frame3 has been transmitted.
Frame4 has been transmitted.
Frame5 has been transmitted.
Frame6 has been transmitted.
Frame7 has been transmitted.
Please enter the last Acknowledgement
received. 2
Frame2 has been transmitted.
Frame3 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("Enterno.ofvertices:");
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 startingnode:");
scanf("%d",&u);
dijkstra(G,n,u);
return0;
}
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;
while(count<n-1)
{
mindistance=INFINITY;
//next node 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;
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);
}
}
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("Enteradjacentmatrix\ n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("Enterconnectingof%d>%d::",i,j);
scanf("%d",&a[i][j]);
}
printf("Enterrootnode:");
scanf("%d",&root);
adj(root);
}
adj(int k)
{
int i,j;
printf("Adjacent node of rootnode::\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:
Enterno.ofnodes:5
Enteradjacent matrix
Enterconnectingof1–>1::0Enterconnectingof 1–>2::1Enter connectingof1–>3::1
Enterconnectingof1–>4::0Enterconnectingof 1–>5::0Enter connectingof2–>1::1
Enterconnectingof2–>2::0Enterconnectingof 2–>3::1Enter connectingof2–>4::1
Enterconnectingof2–>5::0Enterconnectingof 3–>1::1Enter connectingof3–>2::1
Enterconnectingof3–>3::0Enterconnectingof 3–>4::0Enter connectingof3–>5::0
Enterconnectingof4–>1::0Enterconnectingof 4–>2::1Enter connectingof4–>3::0
Enterconnectingof4–>4::0Enterconnectingof 4–>5::1Enter connectingof5–>1::0
Enter connecting of 5–>2::0
Enterconnectingof5–>3::0Enterconnectingof5–>4::1Enterconnectingof5–
>5::0 Enter root node:2
Adjacentnodeofrootnode: 2
134
5
Dept.of CSE CS508PC:COMPUTERNETWORKSLAB
6. Implement distance vector routing algorithm for obtaining routing tables at each node.
#include<stdio.h>
struct node
{
unsigned dist[20];
unsignedfrom[20];
}
rt[10];
int main()
{
int dmat[20][20];
intn,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%dis\n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode%dvia%dDistance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}
OUTPUT:
[cseb332@Insat5E~]$ccdst.c
[cseb332@Insat5E ~]$ ./a.out
Enterthenumberofnodes:4 Enter
the cost matrix:
035 99
3099 1
540 2
9912 0
Statevalueforrouter1is
node 1 via 1 Distance0
node2via2Distance3
node3via3Distance5
node4via2Distance4
Statevalueforrouter2is
node 1 via 1 Distance3
node2via2Distance0
node3via4Distance3
node4via4Distance1
Statevalueforrouter3is
node 1 via 1 Distance5
node2via4Distance3
node3via3Distance0
node4via4Distance2
Statevalueforrouter4is
node 1 via 2 Distance4
node2via2Distance1
node3via3Distance2
node4via4Distance0
if(strlen(plain)!=8)
printf("invalidlengthplaintext!!");
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("decryptedtextis::");
puts(plain);
}
getch();
}
OUTPUT:
Enter10bitkey:1456987203Subkeyk1::17062538 Sub
keyk2::25893401
Enter8bitplaintext:computerCiphertextis::epfnmrct
Decrypted text is::computer
int main()
{
intdrop=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("EnterTheNo.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|PacketRecieved|PacketSent|PacketLeft|PacketDropped|\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++)
{
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 CongestionCongestion.c
$./Congestion
EnterTheBucketSize 5
EnterTheProcessingRate 2
EnterTheNo.OfSecondsYouWantToStimulate 3
EnterTheSizeOfThePacketEnteringAt1sec 5
EnterTheSizeOfThePacketEnteringAt1sec 4
EnterTheSizeOfThePacketEnteringAt1sec 3
Second|PacketRecieved|PacketSent|Packet Left|PacketDropped|
1 5 2 3 0
2 4 2 3 2
3 3 2 3 1
4 0 2 1 0
5 0
#include<stdio.h>
#include<string.h>
#defineFRAM_TXT_SIZ3
#defineMAX_NOF_FRAM127
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
structframe//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;//intoframesandassignssequenceno
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("\nAfterassigningsequencenumbers:\n");
for(i=0; i < k; i++)
printf("%d:%s ",i,fr[i].text);
returnk;//kgivesnoofframes
}
voidgenerate(int *random_ary,constintlimit)//generatearrayofrandomnos
{
intr,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;
}
}
Dept.of CSE CS508PC:COMPUTERNETWORKSLAB
voidshuffle(constintno_frames)//functionshufflesthe frames
{
inti,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]];
printf("\n\nAFTERSHUFFLING:\n");
for(i=0; i < no_frames; i++)
printf("%d:%s",shuf_ary[i].seq_no,shuf_ary[i].text);
}
void sort(constintno_frames)//sortsthe frames
{
inti,j,flag=1;
struct frame hold;
for(i=0;i<no_frames-1&&flag==1;i++)//searchforframesinsequence
{
flag=0;
for(j=0;j<no_frames-1-i;j++)//(basedonseqno.)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;
}
}
}
intmain()
{
int no_frames,i;
printf("Enterthemessage:");
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@localhostnwcn]#./a.out
Enter the message:Welcome To Acharya Institute of Technology
After assigning sequence numbers:
0:Wel1:com2:eT3:oA4:cha5:rya6:In 7:sti8:tut9:eo10:fT11:ech12:nol13:ogy AFTER
SHUFFLING:
1:com4:cha9:eo5:rya3:oA10:fT2:eT6:In11:ech13:ogy0:Wel8:tut12:nol7:sti AFTER
SORTING
WelcomeToAcharyaInstituteofTechnology
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
Inspecting Packets
Click a packet to select it and you can dig down to view its details.
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 map
2. Scan specific ports or scan entire port ranges on a local or remote server nmap -p 1-65535 local host
In this example, we scanned all 65535 ports for our local host computer.
Nmap is able to scan all possible ports, but you can also scan specific ports, which will report
faster results. See below:
nmap-p80,4438.8.8.8
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):
nmap-sV192.168.0.9
i. NS-2SimulatorIntroduction
NS2standsforNetworkSimulatorVersion2.Itisanopen-sourceevent-drivensimulator designed
specifically for research in computer communication networks.
FeaturesofNS2
It is a discrete event simulator for networking research.
It provides substantial support to simulate bunch of protocols likeTCP,FTP,UDP,httpsand DSR.
It simulates wired and wireless network.
It is primarily Unix based.
Uses TCL as its scripting language.
Otcl:Object oriented support
Tclcl:C++ and otcl linkage
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
$nsflush-trace
close $f
close$nf
exec namout.nam &
#execawk-f1.awkout.tr&
exit 0
}
#Create the
nodes set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node]
$n2 label
"Sink"# Set the
color
$ns color1 red
$ns color 2 yellow
#Create the Topology
$ns duplex-link $n0 $n1 $band width10 ms DropTail
$ns duplex-link $n1 $n21.75Mb 20ms DropTail
#Attach a Queue of size N Packets between the nodes n1 n2
$ns queue-limit $n1 $n210
#Make the Link Orientation
$ns duplex-link-op $n0 $n1orient 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
$cbr0setpacketSize_500
$cbr0setinterval_ 0.005
#Create a Null Agent and attachtothenoden2
set null0 [new Agent/Null]
$nsattach-agent$n2$null0
#Connect the CBR Traffic source to the Null agent
$ns connect $udp0 $null0
#Create a TCP agent and attach to the noden0
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 TCP Sink agent and attach to the noden2
set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink
#Specify the Maxfile Size in Bytes
$ftp0 setmaxPkts_ 1000
#Connect the TCP Agent with the TCPSink
$ns connect $tcp0 $sink
Dept.of CSE CS508PC:COMPUTERNETWORKSLAB
$udp0 setclass_1
$tcp0 setclass_2
#Schedule the
Events
$ns at0.1"$cbr0 start"
$ns at1.0"$ftp0 start"
$ns at4.0"$ftp0 stop"
$ns at4.5"$cbr0 stop"
$ns at5.0 "finish"
$ns run
$nscolor1 red
$nscolor2 yellow
#Create the Topology
$ns duplex-link$n0 $n1$band width10ms DropTail
$ns duplex-link$n1 $n21.75Mb 20ms DropTail
#Attach a Queue of size N Packets between the nodes n1 n2
$ns queue-limit $n1$n210
#Make the Link Orientation
#Filename:sample27.tcl
#Simulat or Instance Creation
set ns [new Simulator]
#configurethe nodes
$ns node-config –adhoc Routing $val(rp)\
-llType $val(ll)\
-mac Type $val(mac)\
-ifq Type $val(ifq)\
-ifq Len $val(ifqlen) \
-ant Type $val(ant)\
-prop Type$val(prop)\
-phy Type$val(netif)\
-channel Type$val(chan)\
-topo Instance$topo\
-agent TraceON\
-router TraceON\
-mac TraceOFF\
-movement Trace ON
# Node Creation
for{seti0}{$i<3}{incri}
{
$node_(0)setX_ 258
$node_(0)setY_ 222
$node_(0)setZ_ 0
for{seti1}{$i<3}{incri}
{
$node_($i)setX_[expr rand()*$val(x)]
$node_($i)setY_[expr rand()*$val(y)]
$node_($i)setZ_ 0
}
#Label and coloring
for{seti0}{$i<3}{incri} {
$nsat0.1 "$node_($i)colorblue"
$nsat 0.1"$node_($i)labelNode$i"
}
#Size of the node
for{seti0}{$i<3}{incri} {
$nsinitial_node_pos$node_($i)30
}
#******************************Defining Communication Between node0 and all nodes
****************************8
for{seti1}{$i<3}{incri} {
}
#ending nam and the simulation
$ns at$val(stop)"$ns nam-end-wireless$ val(stop)"
$ns at$val(stop)"stop"
#———Formula————:
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 partfinal calculation is performed on the data obtained from the content part.#
———Steps————:
bits/s Execution:
ns sample27.tcl
$n5 label"dest1"
AWKfile(Open a new edit or using“gedit command”and write a wkfile and save with“.awk”
extension)
BEGIN{
}
{
if($6=="cwnd_")
{
printf("%f\t%f\n",$1,$7);
}
}
END{
}
# basic1.tclsimulation: A---R---B
$ns duplex-link$A$R10Mb10msDropTail
$ns duplex-link$R$B800Kb50msDropTail
$ns run