Lab Manual
Lab Manual
Lab Manual
PART - A
Description:
For the experiments below modify the topology and parameters set for the experiment and
take multiple rounds of reading and analyze the results available in log files. Plot
necessary graphs and conclude. Use NS2/NS3.
1. Implement three nodes point – to – point network with duplex links between them. Set
the queue size, vary the bandwidth and find the number of packets dropped.
2. Implement transmission of ping messages/trace route over a network topology consisting
of 6 nodes and find the number of packets dropped due to congestion.
3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
4. Implement simple ESS and with transmitting nodes in wire-less LAN by simulation and
determine the performance with respect to transmission of packets.
5. Implement and study the performance of GSM on NS2/NS3 (Using MAC layer) or
equivalent environment.
6. Implement and study the performance of CDMA on NS2/NS3 (Using stack called Call
net) or equivalent environment.
PART B
Implement the following in Java:
7. Write a program for error detecting code using CRC-CCITT (16- bits).
8. Write a program to find the shortest path between vertices using bellman-ford algorithm.
9. Using TCP/IP sockets, write a client – server program to make the client send the file
name and to make the server send back the contents of the requested file if present.
10. Write a program on datagram socket for client/server to display the messages on client
side, typed at the server side.
11. Write a program for simple RSA algorithm to encrypt and decrypt the data.
12. Write a program for congestion control using leaky bucket algorithm.
PART A
Implement three nodes point – to – point network with duplex links between them. Set the queue
size, vary the bandwidth and find the number of packets dropped.
TCL Program:
set ns [new Simulator]
set nf [open lab01.nam w]
$ns namtrace-all $nf
set tf [open lab01.tr w]
$ns trace-all $tf
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab1.nam & exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns duplex-link $n0 $n2 200Mb 10ms DropTail
$ns duplex-link $n1 $n2 100Mb 5ms DropTail
$ns duplex-link $n2 $n3 1Mb 1000ms DropTail
$ns queue-limit $n0 $n2 10
$ns queue-limit $n1 $n2 10
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set udp2 [new Agent/UDP]
$ns attach-agent $n2 $udp2
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp2
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
$ns connect $udp0 $null0
$ns connect $udp1 $null0
$ns at 0.1 "$cbr0 start"
$ns at 0.2 "$cbr1 start"
$ns at 1.0 "finish"
$ns run
AWK Program:
BEGIN { c=0;
}
{
If ($1= ="d")
{
c++;
printf("%s\t%s\n",$5,$11);
}
}
END{
printf("The number of packets dropped =%d\n",c);
}
OUTPUT:
TCL Program:
set ns [ new Simulator ]
set nf [ open lab02.nam w ]
$ns namtrace-all $nf
set tf [ open lab02.tr w ]
$ns trace-all $tf
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab2.nam &
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 1.1 "$p1 send"
$ns at 1.2 "$p1 send"
$ns at 1.3 "$p1 send"
$ns at 1.4 "$p1 send"
$ns at 1.5 "$p1 send"
$ns at 1.6 "$p1 send"
$ns at 1.7 "$p1 send"
$ns at 1.8 "$p1 send"
$ns at 1.9 "$p1 send"
$ns at 2.0 "$p1 send"
$ns at 2.1 "$p1 send"
$ns at 2.2 "$p1 send"
$ns at 2.3 "$p1 send"
$ns at 2.4 "$p1 send"
$ns at 2.5 "$p1 send"
$ns at 2.6 "$p1 send"
$ns at 2.7 "$p1 send"
$ns at 2.8 "$p1 send"
$ns at 2.9 "$p1 send"
AWK Program:
BEGIN{
drop=0;
}
{
if($1=="d" )
{
drop++;
}
} END{
printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);
}
OUTPUT:
3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot congestion
window for different source / destination.
TCL Program:
set ns [new Simulator]
proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam lab3.nam &
exit 0
}
AWK Program:
BEGIN {
}
{
if($6=="cwnd_")
printf("%f\t%f\t\n",$1,$7);
}
END {
}
OUTPUT:
4. Implement simple ESS and with transmitting nodes in wire-less LAN by simulation and
determine the performance with respect to transmission of packets.
TCL Program:
create-god 3
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab4.nam &
close $tf
exit 0
}
AWK Program:
BEGIN{
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1=="r"&& $3=="_1_" && $4=="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1=="r" && $3=="_2_" && $4=="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %f Mbps \n", ((count1*pack1*8)/(time1*1000000)));
printf("The Throughput from n1 to n2: %f Mbps", ((count2*pack2*8)/(time2*1000000)));
}
OUTPUT:
5. Implement and study the performance of GSM on NS2/NS3 (Using MAC layer) or equivalent
environment.
TCL Program:
set val(ll) LL
set val(nn) 10
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-energyModel EnergyModel \
-initialEnergy 100 \
-rxPower 0.3 \
-txPower 0.6 \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
puts "---------------------------------------"
set m 0
puts "----------------------------------------"
puts "----------------------------------------"
set k 0
set f 0.5
set nei($m) $j
puts "----------------------------------------"
set holdtime 0
set holdseq 0
set holdrate1 0
proc record {} {
} else {
$ns at [expr $now+$time] "record" ;# Schedule Record after $time interval sec
source link.tcl
proc stop {} {
close $f0
close $f1
close $f2
$ns_ flush-trace
$ns_ run
Link.tcl
$ns_ at 0.1 "$node_(0) setdest 786 813 20"
6. Implement and study the performance of CDMA on NS2/NS3 (Using stack called Call net) or
equivalent environment.
TCL Program:
set val(ll) LL
Mac/802_11 set cdma_code_init_stop_ 127 ;# cdma code for initial request (stop)
Mac/802_11 set cdma_code_cqich_start_ 128 ;# cdma code for cqich request (start)
Mac/802_11 set cdma_code_cqich_stop_ 195 ;# cdma code for cqich request (stop)
Mac/802_11 set cdma_code_handover_start_ 196 ;# cdma code for handover request (start)
Mac/802_11 set cdma_code_handover_stop_ 255 ;# cdma code for handover request (stop)
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-energyModel EnergyModel \
-initialEnergy 100 \
-rxPower 0.3 \
-txPower 0.6 \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
set holdtime 0
set holdseq 0
set holdrate1 0
proc record {} {
} else {
$ns at [expr $now+$time] "record" ;# Schedule Record after $time interval sec
source link.tcl
proc stop {} {
close $f0
close $f1
close $f2
$ns_ flush-trace
$ns_ run
Link.tcl
graph.tcl
exec xgraph ACO-thr.tr -x Time -y Throughout -t Throughout -m -bg white -geometry 800*500 &
exec xgraph ACO-delay.tr -x Time -y Delay -t Delay -m -bg white -geometry 800*500 &
exec xgraph ACO-overhead.tr -x Time -y overhead -t Overhead -m -bg white -geometry 800*500 &
exec xgraph ACO-pdr.tr -x Time -y pdr -t PDR -m -bg white -geometry 800*500 &
PART B
7. Write a program for error detecting code using CRC-CCITT (16- bits).
import java.util.*;
public class CRC
{
void div(int a[],int k)
{
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int count=0;
for(int i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(int j=i;j<17+i;j++)
{
a[j]=a[j]^gp[count++];
}
count=0;
}
}
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
int a[]=new int[100];
int b[]=new int[100];
int len,k;
CRC ob=new CRC();
System.out.println("Enter the length of Data Frame:");
Scanner sc=new Scanner(System.in);
len=sc.nextInt();
int flag=0;
System.out.println("Enter the Message:");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<16;i++)
{
a[len++]=0;
}
k=len-16;
for(int i=0;i<len;i++)
{
b[i]=a[i];
}
ob.div(a,k);
for(int i=0;i<len;i++)
a[i]=a[i]^b[i];
System.out.println("Data to be transmitted: ");
for(int i=0;i<len;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Enter the Reveived Data: ");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
ob.div(a, k);
for(int i=0;i<len;i++)
{
if(a[i]!=0)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("error in data");
else
System.out.println("no error");
}
}
OUTPUT:
[root@localhost ~]# javac CRC.java
[root@localhost ~]# java CRC
8. Write a program to find the shortest path between vertices using bellman-ford algorithm.
import java.util.Scanner;
public class BellmanFord
{
private int d[];
private int nov;
public static final int MAX_VALUE = 999;
d[n] = MAX_VALUE;
}
d[s] = 0;
for (int n = 1; n <= nov - 1; n++)
{
for (int sn = 1; sn <= nov; sn++)
{
for (int dn = 1; dn <= nov; dn++)
{
if (am[sn][dn] != MAX_VALUE)
{
if (d[dn] > d[sn] + am[sn][dn])
d[dn] = d[sn] + am[sn][dn];
}
}
}
}
{
System.out.println("The Graph contains negative egde
cycle");
break;
}
}
}
}
OUTPUT:
[root@localhost ~]# javac BellmanFord.java
[root@localhost ~]# java BellmanFord
Enter the number of vertices
4
Enter the adjacency matrix
0500
5034
0302
0420
Enter the source vertex
2
Distance of source 2 to 1 is 5
Distance of source 2 to 2 is 0
Distance of source 2 to 3 is 3
Distance of source 2 to 4 is 4
9. Using TCP/IP sockets, write a client – server program to make the client send the file name and
to make the server send back the contents of the requested file if present. Implement the above
program using as message queues or FIFOs as IPC channels.
TCP Server:
import java.io.*;
import java.net.*;
import java.util.Scanner;
class TCPServer
{
public static void main(String args[])throws Exception
{
while(true)
{
ServerSocket ss=new ServerSocket(5000);
System.out.println ("Waiting for request");
Socket s=ss.accept();
System.out.println ("Connected With "+s.getInetAddress().toString());
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
try
{
String filename="";
filename=din.readUTF();
System.out.println("Receiving file name");
System.out.println("SendGet....Ok");
File f=new File(filename);
FileInputStream fin=new FileInputStream(f);
long sz=(int) f.length();
byte b[]=new byte [1024];
int read;
dout.writeUTF(Long.toString(sz));
dout.flush();
System.out.println ("File Size: "+sz+" Bytes");
System.out.println ("Receive Buffer size: "+ss.getReceiveBufferSize());
System.out.println("Sending file contents");
while((read = fin.read(b)) != -1)
{
dout.write(b, 0, read);
dout.flush();
}
fin.close();
dout.flush();
System.out.println("Send Complete");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("An error occured");
}
din.close();
s.close();
ss.close();
}
}
}
TCP Client:
import java.io.*;
import java.net.*;
import java.util.Scanner;
class TCPClient
{
public static void main(String args[])throws Exception
{
String address = "";
Scanner sc=new Scanner(System.in);
System.out.println("Enter Server Address: ");
address=sc.nextLine();
Socket s=new Socket(address,5000);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String filename,rcvfile;
System.out.println("Enter File Name: ");
filename=sc.nextLine();
sc.close();
try
{
dout.writeUTF(filename);
dout.flush();
rcvfile="client"+filename;
System.out.println("Saving file as: "+rcvfile);
FileOutputStream fos=new FileOutputStream(new File(rcvfile),true);
long bytesRead;
System.out.println("Receving file..");
long sz=Long.parseLong(din.readUTF());
System.out.println ("File Size: "+sz+" Bytes");
byte b[]=new byte [1024];
do
{
bytesRead = din.read(b, 0, b.length);
fos.write(b,0,b.length);
}while(!(bytesRead<1024));
System.out.println("Completed");
fos.close();
dout.close();
s.close();
}
catch(EOFException e)
{
//do nothing
}
}
}
OUTPUT:
10. Write a program on datagram socket for client/server to display the messages on client
side, typed at the server side.
UDP Server:
import java.net.*;
public class UDPServer
{
public static void main(String[] args)
{
DatagramSocket skt=null;
try
{
skt=new DatagramSocket(6788);
byte[] buffer = new byte[1000];
System.out.println("Listening on port 6788");
while(true)
{
DatagramPacket request = new DatagramPacket(buffer,buffer.length);
skt.receive(request);
String[] message = (new String(request.getData())).split(" ");
byte[] sendMsg= (message[1]+ " server processed").getBytes();
DatagramPacket reply = new DatagramPacket(sendMsg,sendMsg.length,
request.getAddress(),request.getPort());
skt.send(reply);
}
}
catch(Exception ex)
{
}
}
}
UDP Client:
import java.net.*;
public class UDPClient
{
public static void main(String[] args)
{
DatagramSocket skt;
try
{
skt=new DatagramSocket();
String msg= "Computer Networks ";
byte[] b = msg.getBytes();
InetAddress host=InetAddress.getByName("127.0.0.1");
int serverPort=6788;
DatagramPacket request =new DatagramPacket (b,b.length,host,serverPort);
skt.send(request);
byte[] buffer =new byte[1000];
DatagramPacket reply= new DatagramPacket(buffer,buffer.length);
skt.receive(reply);
String s1 = new String(reply.getData());
System.out.println("Client received:" + s1.trim());
skt.close();
}
catch(Exception ex)
{
}
}
}
OUTPUT:
11. Write a program for simple RSA algorithm to encrypt and decrypt the data.
import java.util.*;
import java.io.*;
public class RSA
{
static int gcd(int m,int n)
{
while(n!=0)
{
int r=m%n;
m=n;
n=r;
}
return m;
}
public static void main(String args[])
{
int p=0,q=0,n=0,e=0,d=0,phi=0;
int nummes[]=new int[100];
int encrypted[]=new int[100];
int decrypted[]=new int[100];
int i=0,j=0,nofelem=0;
Scanner sc=new Scanner(System.in);
String message ;
System.out.println("Enter the Message tobe encrypted:");
message= sc.nextLine();
System.out.println("Enter value of p and q\n");
p=sc.nextInt();
q=sc.nextInt();
n=p*q;
phi=(p-1)*(q-1);
for(i=2;i<phi;i++)
if(gcd(i,phi)==1)
break;
e=i;
for(i=2;i<phi;i++)
if((e*i-1)%phi==0)
break;
d=i;
nofelem=message.length();
for(i=0;i< nofelem;i++)
{
char c = message.charAt(i);
nummes[i]=c-96;
}
for(i=0;i<nofelem;i++)
{
encrypted[i]=1;
for(j=0;j<e;j++)
encrypted[i] =(encrypted[i]*nummes[i])%n;
}
System.out.println("\n Encrypted message\n");
for(i=0;i<nofelem;i++)
{
System.out.print(encrypted[i]);
System.out.print((char)(encrypted[i]+96));
}
for(i=0;i<nofelem;i++)
{
decrypted[i]=1;
for(j=0;j<d;j++)
decrypted[i]=(decrypted[i]*encrypted[i])%n;
}
System.out.println("\n Decrypted message\n ");
for(i=0;i<nofelem;i++)
System.out.print((char)(decrypted[i]+96));
}
}
Output
Enter the message to be encrypted
Networks
12. Write a program for congestion control using leaky bucket algorithm.
import java.util.Scanner;
public class LeakyBucket
{
public static void main(String args[])
{
int n, outgoing, store, bucketSize;
int incoming[];
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of inputs");
n = scan.nextInt();
incoming = new int[n];
for(int i = 0 ;i< n ; i++)
{
System.out.println("Enter incoming packet size of "+(i+1)+" packet");
incoming[i] = scan.nextInt();
}
System.out.println("Enter bucket size");
bucketSize = scan.nextInt();
System.out.println("Enter outgoing rate");
outgoing = scan.nextInt();
store = 0;
int i = 0;
System.out.println("Packet Recieved | Packet Dropped | Packet Sent | Packet
Left");
do
{
int pktReceived = 0, pktSent = 0,pktDrop = 0;
if(i < n)
{
pktReceived = incoming[i];
}
}