0% found this document useful (0 votes)
10 views37 pages

CN Lab All Programs (Bcs502)

The document outlines a series of experiments in a computer networks laboratory, focusing on various networking protocols and simulations using a network simulator. Each experiment includes specific aims, configurations, and code snippets for implementing network topologies, measuring packet drops, and simulating protocols like TCP, UDP, and error detection codes. The document also provides expected outputs and execution steps for the experiments.

Uploaded by

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

CN Lab All Programs (Bcs502)

The document outlines a series of experiments in a computer networks laboratory, focusing on various networking protocols and simulations using a network simulator. Each experiment includes specific aims, configurations, and code snippets for implementing network topologies, measuring packet drops, and simulating protocols like TCP, UDP, and error detection codes. The document also provides expected outputs and execution steps for the experiments.

Uploaded by

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

COMPUTER NETWORKS LABORATORY - BCS502

Program 1
AIM : 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.

set ns *new Simulator+ set nf *open lab1.nam w+


$ns namtrace-all $nf set tf *open lab1.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/Traficc/BR]+
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set udp1 *new Agent/UDP+
$ns attach-agent $n1 $udp1set cbr1 *new Application/Traficc/BR]+
$cbr1 attach-agent $udp1
set udp2 *new Agent/UDP+
$ns attach-agent $n2 $udp2
set cbr2 *new Application/Traficc/BR]+
$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

OUTPUT:
Awk file :

REGIN,
drop=0;
-
,
if($1=="d")
,
drop++; -
- END,
printf("total no of %s type packets dropped=%d\n",$5,drop);
-
Experiment – 2
AIM:- 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.

set ns * new Simulator+


set nf * open lab2.nam w+
$ns namtrace-all $nf
set tf * open lab2.tr w+
$ns trace-all $tf

set n0 *$ns node+


set n1 *$ns node+
set n2 *$ns node+
set n3 *$ns node+
set n4 *$ns node+
set n5 *$ns node+

$n4 shape box

$ns duplex-link $n0 $n4 1005Mb 1ms DropTail


$ns duplex-link $n1 $n4 50Mb 1ms DropTail
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail
$ns duplex-link $n3 $n4 200Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set p0 *new Agent/Ping+


$ns attach-agent $n0 $p0
$p0 set packetSize_ 50000
$p0 set interval_ 0.0001

set p1 *new Agent/Ping+


$ns attach-agent $n1 $p1

set p2 *new Agent/Ping+


$ns attach-agent $n2 $p2
$p2 set packetSize_ 30000
$p2 set interval_ 0.00001

set p3 *new Agent/Ping+


$ns attach-agent $n3 $p3

set p5 *new Agent/Ping+


$ns attach-agent $n5 $p5

$ns queue-limit $n0 $n4 5


$ns queue-limit $n2 $n4 3
$ns queue-limit $n4 $n5 2

Agent/Ping instproc recv ,from rtt- ,


$self instvar node_
puts "node *$node_ id+ received answer from $from with round
trip time $rtt msec"
-

$ns connect $p0 $p5


$ns connect $p2 $p3

proc finish , - ,
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab2.nam &
exit 0
-

$ns at 0.1 "$p0 send"


$ns at 0.2 "$p0 send"
$ns at 0.3 "$p0 send"
$ns at 0.4 "$p0 send"
$ns at 0.5 "$p0 send"
$ns at 0.6 "$p0 send"
$ns at 0.7 "$p0 send"
$ns at 0.8 "$p0 send"
$ns at 0.9 "$p0 send"
$ns at 1.0 "$p0 send"
$ns at 1.1 "$p0 send"
$ns at 1.2 "$p0 send"
$ns at 1.3 "$p0 send"
$ns at 1.4 "$p0 send"
$ns at 1.5 "$p0 send"
$ns at 1.6 "$p0 send"
$ns at 1.7 "$p0 send"
$ns at 1.8 "$p0 send"
$ns at 1.9 "$p0 send"
$ns at 2.0 "$p0 send"
$ns at 2.1 "$p0 send"
$ns at 2.2 "$p0 send"
$ns at 2.3 "$p0 send"
$ns at 2.4 "$p0 send"
$ns at 2.5 "$p0 send"
$ns at 2.6 "$p0 send"
$ns at 2.7 "$p0 send"
$ns at 2.8 "$p0 send"
$ns at 2.9 "$p0 send"

$ns at 0.1 "$p2 send"


$ns at 0.2 "$p2 send"
$ns at 0.3 "$p2 send"
$ns at 0.4 "$p2 send"
$ns at 0.5 "$p2 send"
$ns at 0.6 "$p2 send"
$ns at 0.7 "$p2 send"
$ns at 0.8 "$p2 send"
$ns at 0.9 "$p2 send"
$ns at 1.0 "$p2 send"
$ns at 1.1 "$p2 send"
$ns at 1.2 "$p2 send"
$ns at 1.3 "$p2 send"
$ns at 1.4 "$p2 send"
$ns at 1.5 "$p2 send"
$ns at 1.6 "$p2 send"
$ns at 1.7 "$p2 send"
$ns at 1.8 "$p2 send"
$ns at 1.9 "$p2 send"
$ns at 2.0 "$p2 send"
$ns at 2.1 "$p2 send"
$ns at 2.2 "$p2 send"
$ns at 2.3 "$p2 send"
$ns at 2.4 "$p2 send"
$ns at 2.5 "$p2 send"
$ns at 2.6 "$p2 send"
$ns at 2.7 "$p2 send"
$ns at 2.8 "$p2 send"
$ns at 2.9 "$p2 send"

$ns at 3.0 "finish"


$ns run
Awk file :

REGIN,
drop=0;
-
,
if($1=="d")
,
drop++; -
- END,
printf("total no of %s type packets dropped=%d\n",$5,drop);
-

OUTPUT:
Experiment – 3
AIM:- Implement an Ethernet LAN using n nodes and set multiple
traffic nodes and plot congestion window for different source /
destination.
set ns *new Simulator+
set tf *open lab3.tr w+
$ns trace-all $tf
set nf *open lab3.nam w+
$ns namtrace-all $nf
set n0 *$ns node+
$n0 color "magenta"
$n0 label "src1"
set n1 *$ns node+
set n2 *$ns node+
$n2 color "magenta"
$n2 label "src2"
set n3 *$ns node+
$n3 color "blue"
$n3 label "dest2"
set n4 *$ns node+
set n5 *$ns node+
$n5 color "blue"
$n5 label "dest1"
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL
Queue/DropTail Mac/802_3
$ns duplex-link $n4 $n5 1Mb 1ms DropTail
set tcp0 *new Agent/TBP+
$ns attach-agent $n0 $tcp0
set ftp0 *new Application/FTP+
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001
set sink5 *new Agent/TBPSink+
$ns attach-agent $n5 $sink5
$ns connect $tcp0 $sink5
set tcp2 *new Agent/TBP+
$ns attach-agent $n2 $tcp2
set ftp2 *new Application/FTP+
$ftp2 attach-agent $tcp2
$ftp2 set packetSize_ 600
$ftp2 set interval_ 0.001
set sink3 *new Agent/TBPSink+
$ns attach-agent $n3 $sink3
$ns connect $tcp2 $sink3
set file1 *open file1.tr w+
$tcp0 attach $file1
set file2 *open file2.tr w+
$tcp2 attach $file2
$tcp0 trace cwnd_
$tcp2 trace cwnd_
proc finish ,- ,
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam lab3.nam &
exit 0
-
$ns at 0.1 "$ftp0 start"
$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"
$ns at 14 "$ftp0 stop"
$ns at 0.2 "$ftp2 start"
$ns at 8 "$ftp2 stop"
$ns at 10 "$ftp2 start"
$ns at 15 "$ftp2 stop"
$ns at 16 "finish"
$ns run

AWK FILE:
REGIN,
-
,
if($6=="cwnd_")
printf("%f\t%f\t\n",$1,$7);
- END,
-

OUTPUT:

Steps for execution:


ns lab3.tcl
awk -f lab3.awk file1.tr >a1
awk -f lab3.awk file2.tr>a2
xgraph a1 a2

xgraph
Experiment - 4
AIM - Write a program for error detecting code using CRC-
CCITT (16- bits)

import java.util.*;

public class p3 ,
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;
-
-
-

public static void main(String args*+) ,


int a*+ = new int*100+;
int b*+ = new int*100+;
int len, k;
p3 ob = new p3();
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 ]eceived 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");
-
-

Output1 –
Enter the length of Data Frame: 4
Enter the Message: 1 0 1 1
Data to be transmitted: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
Enter the ]eveived Data: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
no error

Output2 –
Enter the length of Data Frame: 4
Enter the Message: 1 0 1 1
Data to be transmitted: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
Enter the ]eveived Data: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 0 1
error in data
Experiment - 5
AIM:-Develop a program to implement a sliding window protocol in
the data link layer.

import java.util.]andom;
import java.util.Scanner;

class SlidingWindowProtocol ,
private int windowSize;
private int totalFrames;

public SlidingWindowProtocol(int windowSize, int totalFrames) ,


this.windowSize = windowSize;
this.totalFrames = totalFrames;
-

public void sendFrames() ,


int framesSent = 0;
int frameNumber = 0;
]andom random = new ]andom();

while (framesSent < totalFrames) ,


for (int i = 0; i < windowSize && frameNumber < totalFrames;
i++) ,
System.out.println("Sending frame: " + frameNumber);
frameNumber++;
-
for (int i = framesSent; i < framesSent + windowSize && i <
totalFrames; i++) ,
boolean ack]eceived = random.nextRoolean();
if (ack]eceived) ,
System.out.println("Acknowledgment received for frame:
" + i);
framesSent++;
- else ,
System.out.println("Frame " + i + " lost, resending...");
frameNumber = i;
break;
-
-
-
System.out.println("All frames sent successfully.");
-

public static void main(String*+ args) ,


Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the sliding window: ");
int windowSize = sc.nextInt();
System.out.print("Enter the total number of frames to send: ");
int totalFrames = sc.nextInt();
SlidingWindowProtocol swp = new
SlidingWindowProtocol(windowSize, totalFrames);
swp.sendFrames();
-
-

OUTPUT:
Experiment – 6
AIM:-Write a program to find the shortest path between
vertices using bellman-ford algorithm.

import java.util.*;

public class Relmanford ,


private int*+ D;
private int n;
public static final int MAX_VALUE = 999;

public Relmanford(int n) ,
this.n = n;
D = new int*n + 1+;
-

public void shortest(int s, int*+*+ a) ,


Arrays.fill(D, MAX_VALUE);
D*s+ = 0;

for (int k = 1; k <= n - 1; k++) ,


for (int i = 1; i <= n; i++) ,
for (int j = 1; j <= n; j++) ,
if (a*i+*j+ != MAX_VALUE) ,
if (D*j+ > D*i+ + a*i+*j+) ,
D*j+ = D*i+ + a*i+*j+;
-
-
-
-
-

for (int i = 1; i <= n; i++) ,


for (int j = 1; j <= n; j++) ,
if (a*i+*j+ != MAX_VALUE) ,
if (D*j+ > D*i+ + a*i+*j+) ,
System.out.println("The graph contains a negative edge
cycle");
return;
-
-
-
-

for (int i = 1; i <= n; i++) ,


System.out.println("Distance from source " + s + " to " + i + " is
" + D*i+);
-
-

public static void main(String*+ args) ,


Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices:");
int n = sc.nextInt();
int*+*+ a = new int*n + 1+*n + 1+;

System.out.println("Enter the weighted adjacency matrix:");


for (int i = 1; i <= n; i++) ,
for (int j = 1; j <= n; j++) ,
a*i+*j+ = sc.nextInt();
if (i == j) ,
a*i+*j+ = 0;
-
if (a*i+*j+ == 0) ,
a*i+*j+ = MAX_VALUE;
-
-
-

System.out.println("Enter the source vertex:");


int s = sc.nextInt();
Relmanford b = new Relmanford(n);
b.shortest(s, a);
sc.close();
-
-

OUTPUT:
Experiment – 7
AIM:- 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.

import java.net.*;
import java.io.*;
public class TCPServer,
public static void main(String args*+) throws Exception ,
ServerSocket sersock=new ServerSocket(4000);
System.out.println("Server ready for Connection");
Socket sock=sersock.accept();
System.out.println("Connection is Successful and waiting for chatting");
InputStream istream=sock.getInputStream();
BufferedReaderfileRead=newBufferedReader(newInputStreamReader(istrea
m));
String fname=fileRead.readLine();
BufferedReader contentRead=new BufferedReader(new FileReader(fname));
OutputStream ostream=sock.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=contentRead.readLine())!=null),
pwrite.println(str);
-
sock.close();
sersock.close();
pwrite.close();
fileRead.close();
contentRead.close();
-
-

import java.net.*;
import java.io.*;

public class TCPClient,


public static void main(String args*+) throws Exception ,
Socket sock=new Socket("127.0.0.1",4000);
System.out.println("Enter the filename");
BufferedReader keyRead=new BufferedReader(new
InputStreamReader(System.in));
String fname=keyRead.readLine();
OutputStream ostream=sock.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream,true);
pwrite.println(fname);
InputStream istream=sock.getInputStream();
BufferedReader socketRead=new BufferedReader(new
InputStreamReader(istream));
String str;
while((str=socketRead.readLine())!=null),
System.out.println(str);
-
pwrite.close();
socketRead.close();
keyRead.close();
-
-

OUTPUT:
Experiment – 8
AIM:- Develop a program on a datagram socket for
client/server to display the messages on client side, typed at
the server side.

import java.io.*;
import java.net.*;
public class UDPServer,
public static void main(String*+ args),
DatagramSocket skt=null;
try,
System.out.println("server is started");
skt=new DatagramSocket(6942);
byte*+ buffer = new byte*1000+;
while(true),
DatagramPacket request = new DatagramPacket
(buffer,buffer.length);
skt.receive(request);
String*+ message = (new String(request.getData())).split(" ");
byte*+ sendMsg= (message*1+.toUpperCase()+ " from server
to client.").getBytes();
DatagramPacket reply = new
DatagramPacket(sendMsg,sendMsg.length,request.getAddr
ess(),request.getPort());
skt.send(reply);
-
-
catch(Exception ex),
System.out.println(ex.getMessage());
-
-
-

import java.io.*;
import java.net.*;
public class UDPClient,
public static void main(String*+ args),
DatagramSocket skt;
try,
skt=new DatagramSocket();
String msg= "Redundant college";
byte*+ b = msg.getBytes();
InetAddress host=InetAddress.getByName("127.0.0.1");
int serverSocket=6942;
DatagramPacket request =new DatagramPacket
(b,b.length,host,serverSocket);
skt.send(request);
byte*+ buffer =new byte*2000+;
DatagramPacket reply= new
DatagramPacket(buffer,buffer.length);
skt.receive(reply);
System.out.println("client received:" +new
String(reply.getData()));
skt.close();
-
catch(Exception ex),
System.out.println(ex.getMessage());
-
-
-
OUTPUT:
Experiment – 9
AIM:- Develop a program for a 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 to be 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;
for (i = 0; i < message.length(); i++) ,
char c = message.charAt(i);
int a = (int) c;
nummes*i+ = c - 96;
-
nofelem = message.length();

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("\nEncrypted 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("\nDecrypted message\n");

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


System.out.print((char) (decrypted*i+ + 96));
System.out.println();
return;
-
-

Output:
Experiment – 10
AIM:- Develop a program for congestion control using a leaky
bucket algorithm.

import java.util.Scanner;

public class LeakyBucket ,

static int min(int x, int y) ,


if (x < y)
return x;
else
return y;
-

public static void main(String*+ args) ,


int drop = 0, mini, nsec, cap, count = 0, i, process;
int inp*+ = new int*25+;
Scanner sc = new Scanner(System.in);

System.out.print("Enter The Bucket Size: ");


cap = sc.nextInt();
System.out.print("Enter The Operation Rate: ");
process = sc.nextInt();

System.out.print("Enter The No. Of Seconds You Want To Stimulate:


");
nsec = sc.nextInt();

for (i = 0; i < nsec; i++) ,


System.out.print("Enter The Size Of The Packet Entering At " + (i + 1)
+ " sec: ");
inp*i+ = sc.nextInt();
-

System.out.println("\nSecond | Packet Recieved | Packet Sent |


Packet Left | Packet Dropped|\n");
System.out.println("----------------------------------------------------------------
--------\n");

for (i = 0; i < nsec; i++) ,


count += inp*i+;
if (count > cap) ,
drop = count - cap;
count = cap;
-
System.out.print(i + 1);
System.out.print("\t\t" + inp*i+);
mini = min(count, process);
System.out.print("\t\t" + mini);
count = count - mini;
System.out.print("\t\t" + count);
System.out.print("\t\t" + drop);
drop = 0;
System.out.println();
-
for (; count != 0; i++) ,
if (count > cap) ,
drop = count - cap;
count = cap;
-
System.out.print(i + 1);
System.out.print("\t\t0");
mini = min(count, process);
System.out.print("\t\t" + mini);
count = count - mini;
System.out.print("\t\t" + count);
System.out.print("\t\t" + drop);
System.out.println();
-
-
-
Output:

You might also like