0% found this document useful (0 votes)
3 views29 pages

Lab Program Computer Networks

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

Lab Program Computer Networks

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program1

 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 ntrace [open [Link] w]
$ns trace-all $ntrace

set namfile [open [Link] w]


$ns namtrace-all $namfile
proc Finish {} {
global ns ntrace namfile
$ns flush-trace
close $ntrace
close $namfile

exec nam [Link] &

set drops [exec grep -c "^d" [Link]]

puts "The number of packet drops is $drops"

flush stdout

exit 0

}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "TCP Source"
$n2 label "Sink"
$ns color 1 blue
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
$ns queue-limit $n0 $n1 10
$ns queue-limit $n1 $n2 10
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n2 $sink0
$ns connect $tcp0 $sink0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set type_ CBR
$cbr0 set packetSize_ 100
$cbr0 set rate_ 1Mb
$cbr0 set random_ false
$cbr0 attach-agent $tcp0
$tcp0 set class_ 1
$ns at 0.0 "$cbr0 start"
$ns at 5.0 "Finish"
$ns run
Program 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.

set ns [new Simulator]

set namfile [open [Link] w]

$ns namtrace-all $namfile

set ntrace [open [Link] w]

$ns trace-all $ntrace

proc Finish {} {

global ns namfile ntrace

$ns flush-trace

close $namfile

close $ntrace

exec nam [Link] &

exec echo "Number of Packets dropped :" &

exec grep -c "^d" [Link] &

exit 0

set n0 [$ns node]

set n1 [$ns node]


set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

set n6 [$ns node]

$ns duplex-link $n1 $n0 1Mb 10ms DropTail

$ns duplex-link $n2 $n0 1Mb 10ms DropTail

$ns duplex-link $n3 $n0 1Mb 10ms DropTail

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

$ns duplex-link $n5 $n0 1Mb 10ms DropTail

$ns duplex-link $n6 $n0 1Mb 10ms DropTail

Agent/Ping instproc recv {from rtt} {

$self instvar node_

puts "node [$node_ id]received ping answer from \ $from with roun-trip-time
$rtt ms."

}set p1 [new Agent/Ping]

set p2 [new Agent/Ping]

set p3 [new Agent/Ping]

set p4 [new Agent/Ping]

set p5 [new Agent/Ping]

set p6 [new Agent/Ping]


$ns attach-agent $n1 $p1

$ns attach-agent $n2 $p2

$ns attach-agent $n3 $p3

$ns attach-agent $n4 $p4

$ns attach-agent $n5 $p5

$ns attach-agent $n6 $p6

$ns queue-limit $n0 $n4 3

$ns queue-limit $n0 $n5 2

$ns queue-limit $n0 $n6 2

$ns connect $p1 $p4

$ns connect $p2 $p5

$ns connect $p3 $p6

$ns at 0.2 "$p1 send"

$ns at 0.4 "$p2 send"

$ns at 0.6 "$p3 send"

$ns at 1.0 "$p4 send"

$ns at 1.2 "$p5 send"

$ns at 1.4 "$p6 send"

$ns at 2.0 "Finish"

$ns run
Program 3

 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 namfile [open [Link] w]
$ns namtrace-all $namfile
set ntrace [open [Link] w]
$ns trace-all $ntrace
$ns color 1 Blue
$ns color 2 Red
proc Finish {} {
global ns namfile ntrace
$ns flush-trace
close $namfile
close $ntrace
exec nam [Link] &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
set n7 [$ns node]
set n8 [$ns node]
$n7 shape box
$n7 color Blue
$n8 shape hexagon
$n8 color Red
$ns duplex-link $n1 $n0 2Mb 10ms DropTail
$ns duplex-link $n2 $n0 2Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 20ms DropTail
$ns make-lan "$n3 $n4 $n5 $n6 $n7 $n8" 512Kb 40ms LL Queue/DropTail
Mac/802_3
$ns duplex-link-op $n1 $n0 orient right-down
$ns duplex-link-op $n2 $n0 orient right-up
$ns duplex-link-op $n0 $n3 orient right
$ns queue-limit $n0 $n3 20
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n7 $sink1
$ns connect $tcp1 $sink1
$tcp1 set class_ 1
$tcp1 set packetSize_ 55
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set tfile [open [Link] w]
$tcp1 attach $tfile
$tcp1 trace cwnd_
$ns at 0.5 "$ftp1 start"
$ns at 5.0 "$ftp1 stop"
$ns at 5.5 "Finish"
$ns run
Program 4

 Develop a program for error detecting code using CRC-CCITT (16- bits).

import [Link];
import [Link].*;
public class CRC19
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("*--------- Error Detection using CRC ----------- *");
[Link](" Enter message bits : ");
String message = [Link]();
[Link](" Enter generator : ");
String generator = [Link]();
int[] data = new int[[Link]() + [Link]() - 1];
int[] divisor = new int[[Link]()];
for(int i=0;i < [Link](); i++)
{
data[i] = [Link]([Link](i) + "");
}
for(int i=0;i<[Link](); i++)
{
divisor[i] = [Link]([Link](i) + "");
}
for(int i=0;i<[Link]();i++)
{
if(data[i] == 1)
{
for(int j=0;j<[Link];j++)
{
data[i+j] ^= divisor[j];
}
}
}
[Link]("\n The Checksum code is : ");
for(int i=0;i<[Link](); i++)
{
data[i] = [Link]([Link](i) + "");
}
for(int i=0;i<[Link]; i++)
{
[Link](data[i]);
}
[Link]();
[Link](" Enter checksum code : ");
message = [Link]();
[Link]("\n Enter generator : ");
generator = [Link]();
data = new int[[Link]() + [Link]() -1];
divisor = new int[[Link]()];
for(int i=0;i<[Link]();i++)
{
data[i] = [Link]([Link](i) + "");
}
for(int i=0;i<[Link](); i++)
{
divisor[i] = [Link]([Link](i) + "");
}
for(int i=0;i<[Link]();i++)
{
if(data[i]==1)
{
for(int j=0;j<[Link];j++)
{
data[i+j] ^= divisor[j];
}
}
}
boolean valid = true;
for(int i=0;i<[Link];i++)
{
if(data[i] == 1)
{
valid = false;
break;
}
}
if ( valid == true)
[Link](" \n Data stream is valid ");
else
[Link]("\n Data stream is invalid, CRC error occurred");
}
}
Program 5

 Develop a program to implement a sliding window protocol in the data link


layer.
Sender
import [Link].*;
import [Link].*;
public class Sender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
InetAddress receiverAddress = [Link]("localhost");
int receiverPort = 9876;
int windowSize = 4;
byte[] data = "Hello, Sliding Window!".getBytes();
int base = 0;
while (base < [Link]) {
for (int i = base; i < base + windowSize && i < [Link]; i++) {
byte[] packetData = new byte[2];
packetData[0] = (byte) i;
packetData[1] = data[i];
DatagramPacket packet =new
DatagramPacket(packetData,[Link],receiverAddress, receiverPort);
[Link](packet);
[Link]("Sent seq=" + i + " data=" + (char)data[i]);
}
DatagramPacket ackPacket = new DatagramPacket(new byte[1], 1);
[Link](ackPacket);
int ack = [Link]()[0] & 0xFF;
[Link]("Received ACK: " + ack);
if (ack >= base) {
base = ack + 1;
}
}
[Link]();
[Link]("All bytes sent.");
}
}

Receiver
import [Link].*;
import [Link].*;
public class Receiver {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9876);
int expectedSeqNum = 0;
while (true) {
byte[] buffer = new byte[2]; // seq + data
DatagramPacket packet = new DatagramPacket(buffer, [Link]);
[Link](packet);
int seqNum = buffer[0] & 0xFF;
if (seqNum == expectedSeqNum) {
[Link]("Received: " + seqNum);
DatagramPacket ackPacket =
new DatagramPacket(new byte[]{ (byte) seqNum }, 1,
[Link](), [Link]());
[Link](ackPacket);
expectedSeqNum++;
}
}
}
}
Program 6

 Develop a program to find the shortest path between vertices using


the Bellman-Ford and path vector routing algorithm.

import [Link];
public class ford
{
private int D[];
private int num_ver;
public static final int MAX_VALUE = 999;
public ford(int num_ver)
{
this.num_ver = num_ver;
D = new int[num_ver + 1];
}
public void BellmanFordEvaluation(int source, int A[][])
{
for (int node = 1; node <= num_ver; node++)
{
D[node] = MAX_VALUE;
}
D[source] = 0;
for (int node = 1; node <= num_ver - 1; node++)
{
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
D[dn] = D[sn] + A[sn][dn];
}
}
}
}
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
[Link]("The Graph contains negative egde cycle");
}
}
}
for (int vertex = 1; vertex <= num_ver; vertex++)
{
[Link]("distance of source"+source+"to"+vertex+"is" + D[vertex]);
}
}
public static void main(String[ ] args)
{
int num_ver = 0;
int source;
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of vertices");
num_ver = [Link]();
int A[][] = new int[num_ver + 1][num_ver + 1];
[Link]("Enter the adjacency matrix");
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
A[sn][dn] = [Link]();
if (sn == dn)
{
A[sn][dn] = 0;
continue;
}
if (A[sn][dn] == 0)
{
A[sn][dn] = MAX_VALUE;
}
}
}
[Link]("Enter the source vertex");
source = [Link]();
ford b = new ford (num_ver);
[Link](source, A);
[Link]();
}
}
Program 7

 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.

//TCP SERVER

import [Link].*;
import [Link].*;
public class TCPS
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock=new ServerSocket(4000);
[Link]("Server ready for connection");
Socket sock=[Link]();
[Link]("Connection Is successful and waiting for chatting");
InputStream istream=[Link]();
BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));
String fname=[Link]();
BufferedReader ContentRead=new BufferedReader(new FileReader(fname));
OutputStream ostream=[Link]();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=[Link]())!=null){
[Link](str);
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

//TCP CLIENT
import [Link].*;
import [Link].*;
public class TCPC
{
public static void main(String[] args) throws Exception
{
Socket sock=new Socket("127.0.01",4000);
[Link]("Enter the filename");
BufferedReader keyRead=new BufferedReader(new InputStreamReader([Link]));
String fname=[Link]();
OutputStream ostream=[Link]();
PrintWriter pwrite=new PrintWriter(ostream,true);
[Link](fname);
InputStream istream=[Link]();
BufferedReader socketRead=new BufferedReader(new InputStreamReader(istream));
String str;
while((str=[Link]())!=null)
{
[Link](str);
}
[Link]();
[Link]();
[Link]();
}
}
Program 8

 Develop a program on a datagram socket for client/server to display


the messages on client side, typed at the server side.

//UDP SERVER

import [Link].*;

import [Link];

class UDPServer

public static void main(String args[])throws Exception

DatagramSocket serverSocket = new DatagramSocket(9876);

byte[] receiveData=new byte[1024];

byte[] sendData=new byte[1024];

while(true)

[Link]("Server is Up");

DatagramPacket receivePacket=new DatagramPacket(receiveData,[Link]);

[Link](receivePacket);

String sentence=new String([Link]());

[Link]("RECEIVED:"+sentence);

InetAddress IPAddress=[Link]();

int port=[Link]();

String capitalizedSentence=[Link]();

sendData=[Link]();

DatagramPacket sendPacket=new DatagramPacket(sendData,[Link],IPAddress,port);

[Link](sendPacket);
}

//UDP CLIENT

import [Link].*;

import [Link].*;

import [Link];

class UDPClient

public static void main(String[] args)throws Exception

BufferedReader inFromUser=new BufferedReader(new InputStreamReader([Link]));

DatagramSocket clientSocket=new DatagramSocket();

InetAddress IPAddress=[Link]("localhost");

byte[] sendData=new byte[1024];

byte[] receiveData=new byte[1024];

[Link]("Enter the sting to be converted in to Upper case");

String sentence=[Link]();

sendData=[Link]();

DatagramPacket sendPacket=new
DatagramPacket(sendData,[Link],IPAddress,9876);

[Link](sendPacket);

DatagramPacket receivePacket=new DatagramPacket(receiveData,[Link]);

[Link](receivePacket);
String modifiedSentence=new String([Link]());

[Link]("FROM SERVER:"+modifiedSentence);

[Link]();

}
Program 9

 Develop a program for a simple RSA algorithm to encrypt and decrypt


the data.

import [Link];
import [Link];
import [Link];
import [Link];
public class RSA
{
private BigInteger p,q,N,phi,e,d;
private int bitlength=1024;
private Random r;
public RSA()
{
r=new Random();
p=[Link](bitlength,r);
q=[Link](bitlength,r);
[Link]("Prime number p is"+p);
[Link]("prime number q is"+q);
N=[Link](q);
phi=[Link]([Link]).multiply([Link]([Link]));
e=[Link](bitlength/2,r);
while([Link](e).compareTo([Link])>0&&[Link](phi)<0)
{
[Link]([Link]);
}
[Link]("Public key is"+e);
d=[Link](phi);
[Link]("Private key is"+d);
}
public RSA(BigInteger e,BigInteger d,BigInteger N)
{
this.e=e;
this.d=d;
this.N=N;
}
public static void main(String[] args)throws IOException
{
RSA rsa=new RSA();
DataInputStream in=new DataInputStream([Link]);
String testString;
[Link]("Enter the plain text:");
testString=[Link]();
[Link]("Encrypting string:"+testString);
[Link]("string in bytes:"+bytesToString([Link]()));
byte[] encrypted=[Link]([Link]());
byte[] decrypted=[Link](encrypted);
[Link]("Dcrypting Bytes:"+bytesToString(decrypted));
[Link]("Dcrypted string:"+new String(decrypted));
}
private static String bytesToString(byte[] encrypted)
{
String test=" ";
for(byte b:encrypted)
{
test+=[Link](b);
}
return test;
}
public byte[]encrypt(byte[]message)
{
return(new BigInteger(message)).modPow(e,N).toByteArray();
}
public byte[]decrypt(byte[]message)
{
return(new BigInteger(message)).modPow(d,N).toByteArray();
}
}
Program 10

 Develop a program for congestion control using a leaky bucket


algorithm.

import [Link];

import [Link].*;

public class lab7 {

public static void main(String[] args)

int i;

int a[]=new int[20];

int buck_rem=0,buck_cap=4,rate=3,sent,recv;

Scanner in = new Scanner([Link]);

[Link]("Enter the number of packets");

int n = [Link]();

[Link]("Enter the packets");

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

a[i]= [Link]();

[Link]("Clock \t packet size \t accept \t sent \t remaining");

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

if(a[i]!=0)

if(buck_rem+a[i]>buck_cap)

recv=-1;

else

{
recv=a[i];

buck_rem+=a[i];

else

recv=0;

if(buck_rem!=0)

if(buck_rem<rate)

{sent=buck_rem;

buck_rem=0;

else

sent=rate;

buck_rem=buck_rem-rate;

else

sent=0;

if(recv==-1)

[Link](+i+ "\t\t" +a[i]+ "\t dropped \t" + sent +"\t" +buck_rem);

else

[Link](+i+ "\t\t" +a[i] +"\t\t" +recv +"\t" +sent + "\t" +buck_rem);

}
CONTENT BEYOND SYLLABUS

 Develop a program to implement a Stop and wait protocol using


Sliding window

set ns [new Simulator]

set nf [open [Link] w]

$ns namtrace-all $nf

proc finish {} {

global ns nf

$ns flushtrace-all

close $nf

exec nam [Link] &

exit 0

set n0 [$ns node]

set n1 [$ns node]

$ns duplex-link $n0 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right

$ns queue-limit $n0 $n1 10

Agent/TCP set nam_tracevar_ true

set tcp [new Agent/TCP]

$tcp set window_ 4

$tcp set maxcwnd_ 4


set sink [new Agent/TCPSink]

$ns attach-agent $n0 $tcp

$ns attach-agent $n1 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns add-agent-trace $tcp tcp

$ns monitor-agent-trace $tcp

$tcp tracevar cwnd_

$ns at 1.0 '$ftp start'

$ns at 2.0 '$ftp stop'

$ns at 5.0 'finish'

You might also like