CN Lab Manual
CN Lab Manual
PART B
PART-A
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.
set ns [ new Simulator ]
#The below code is used to set the queue size b/w the nodes
$ns set queue-limit $n0 $n2 10
$ns set queue-limit $n1 $n2 10
$ns set queue-limit $n2 $n3 5
#The below code sets the udp0 packets to red and udp1
#packets to blue color
$udp0 set class_ 1
$udp1 set class_ 2
Output:
awk2.awk
BEGIN{
#include<stdio.h>
tcp=0;
udp=0;
}
{
if($1=="r"&&$3=="2"&&$4=="3"&& $5=="tcp")
tcp++;
if($1=="r"&&$3=="2"&&$4=="3"&&$5=="cbr")
udp++;
}
END{
printf("\n Total number of packets sent by TCP : %d\n",tcp);
printf("\n Total number of packets sent by UDP : %d\n",udp);
}
Output:
awk3.awk
BEGIN {
}
{
if($6=="cwnd_")
printf("%f\t%f\t\n",$1,$7); }
END {
}
Output:
awk4.awk
BEGIN{
#include<stdio.h>
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: %fMbps\n",
((count1*pack1*8)/(time1*1000000)))
Output:
xg2gp.awk
#!/usr/bin/awk -f
# convert from raw2xg output into gnuplot format
BEGIN {system("rm -f acks packets drops")}
{if (ack) print $1 " " $2 >>"acks"}
{if (pkt) print $1 " " $2 >>"packets"}
{if (drp) print $1 " " $2 >>"drops"}
$1 ~ /^.ack/ {ack=1}
$1 ~ /^.packets/ {pkt=1}
$1 ~ /^.drops/ {drp=1}
$1 ~ /^$/ {ack=0; pkt=0; drp=0}
Output:
}
# RED and TCP parameters
Queue/RED set summarystats_ true
Queue/DropTail set summarystats_ true
Queue/RED set adaptive_ $opt(adaptive)
Queue/RED set q_weight_ 0.0
Queue/RED set thresh_ $opt(minth)
Queue/RED set maxthresh_ $opt(maxth)
Queue/DropTail set shrink_drops_ true
Agent/TCP set ecn_ $opt(ecn)
Agent/TCP set window_ $opt(window)
DelayLink set avoidReordering_ true
#Create topology
switch $opt(type) {
gsm - gprs - umts {cell_topo}
}
set_link_params $opt(type)
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(bs1) $nodes(ms) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]
$ns insert-delayer $nodes(bs2) $nodes(ms) [new Delayer]
awk6.awk
BEGIN {
PacketRcvd=0;
Throughput=0.0;
}
{
if(($1=="r") && ($5=="tcp") && ($10=4.0))
{
PacketRcvd++;
}
}
END {
Throughput=((PacketRcvd*1000*8)/(95.0*1000000));
printf("packet received:%f\n", PacketRcvd);
printf( "the throughput is:%f\n",Throughput);
}
Output:
PART B
7. Write a program for error detecting code using CRC-CCITT (16- bits).
import java.io.*;
class crc_gen
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
/* CRC GENERATION */
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.println("THANK YOU.");
}
if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}
OUTPUT 1:
Enter number of data bits :
10
Enter data bits :
1
1
0
1
0
1
1
0
1
1
Enter number of bits in divisor :
5
Enter Divisor bits :
1
0
0
1
1
Data bits are : 1101011011
divisor bits are : 10011
Dividend (after appending 0's) are : 11010110110000
CRC code :
11010110111110
Enter CRC code of 14 bits :
1
1
0
1
0
1
1
0
1
1
1
1
1
0
crc bits are : 11010110111110
No Error
THANK YOU.
OUTPUT 2:
Enter number of data bits :
10
Enter data bits :
1
1
0
1
0
1
1
0
1
1
Enter number of bits in divisor :
5
Enter Divisor bits :
1
0
0
1
1
Data bits are : 1101011011
divisor bits are : 10011
Dividend (after appending 0's) are : 11010110110000
CRC code :
11010110111110
Enter CRC code of 14 bits :
1
1
0
1
0
1
1
0
1
1
1
1
1
1
crc bits are : 11010110111111
Error
THANK YOU.
java.util.Scanner;
public class bellmanford
{
private int distances[];
private int numberofvertices;
public static final int MAX_VALUE = 999;
public bellmanford(int numberofvertices)
{
this.numberofvertices = numberofvertices;
distances = new int[numberofvertices + 1];
}
public void BellmanFordEvaluation(int source, int
destination,int adjacencymatrix[][])
{
for (int node = 1; node <= numberofvertices; node++)
{
distances[node] = MAX_VALUE;
}
distances[source] = 0;
for (int node = 1; node <= numberofvertices - 1;
node++)
{
for (int sourcenode = 1; sourcenode <=
numberofvertices; sourcenode++)
{
for (int destinationnode = 1;
destinationnode <= numberofvertices; destinationnode++)
{
if
(adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
{
if (distances[destinationnode] >
distances[sourcenode]+
adjacencymatrix[sourcenode][destinationnode])
distances[destinationnode]
= distances[sourcenode]+
adjacencymatrix[sourcenode][destinationnode];
}
}
}
}
for (int sourcenode = 1; sourcenode <=
numberofvertices; sourcenode++)
{
for (int destinationnode = 1; destinationnode
<= numberofvertices; destinationnode++)
{
if
(adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
{
if (distances[destinationnode] >
distances[sourcenode]+
adjacencymatrix[sourcenode][destinationnode])
System.out.println("The Graph
contains negative egde cycle");
}
}
}
for (int vertex = 1; vertex <= numberofvertices;
vertex++)
{
if (vertex == destination)
System.out.println("distance of source " +
source + " to "+ vertex + " is " + distances[vertex]);
}
}
public static void main(String[ ] args)
{
int numberofvertices = 0;
int source, destination;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
numberofvertices = scanner.nextInt();
int adjacencymatrix[][] = new int[numberofvertices +
1][numberofvertices + 1];
System.out.println("Enter the adjacency matrix");
for (int sourcenode = 1; sourcenode <=
numberofvertices; sourcenode++)
{
for (int destinationnode = 1; destinationnode
<= numberofvertices; destinationnode++)
{
adjacencymatrix[sourcenode][destinationnode] =
scanner.nextInt();
if (sourcenode == destinationnode)
{
adjacencymatrix[sourcenode][destinationnode] = 0;
continue;
}
if
(adjacencymatrix[sourcenode][destinationnode] == 0)
{
adjacencymatrix[sourcenode][destinationnode] = MAX_VALUE;
}
}
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
System.out.println("Enter the destination vertex:
");
destination = scanner.nextInt();
bellmanford bellmanford = new
bellmanford(numberofvertices);
bellmanford.BellmanFordEvaluation(source,
destination, adjacencymatrix);
scanner.close();
}
}
Output:
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.
Client Program:
import java.io.*;
import java.net.*;
public class client1
{
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 1136);
DataOutputStream outToServer = new DataOutputStream
(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader
(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Server Program:
import java.io.*;
import java.net.*;
public class server1
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(1136);
while (true)
{
Output:
Run Server program first and then run Client program.
hiiiiii, how are you. computer networks.
FROM SERVER: HIIIIII, HOW ARE YOU. COMPUTER NETWORKS.
Client Program
import java.io.*;
import java.net.*;
public class client
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, 9012);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new
String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
Server Program
import java.io.*;
import java.net.*;
public class server
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9012);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
11. Write a program for simple RSA algorithm to encrypt and decrypt
the data.
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.io.*;
import javax.crypto.Cipher;
public class rsa {
public static void main(String args[])
{
String srci="";
try{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please enter any string you want to
encrypt");
srci=br.readLine();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
try{
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);//initialize key pairs to 512 bits ,you can
//also take 1024 or 2048 bits
KeyPair kp=kpg.genKeyPair();
PublicKey publi=kp.getPublic();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publi);
byte[]src=srci.getBytes();//converting source data into byte
//array
byte[] cipherData = cipher.doFinal(src);//use this method to
finally encrypt data
String srco=new String(cipherData);//converting byte array into
//string
System.out.println();
System.out.println("Encrypted data is:-"+srco);
PrivateKey privatei=kp.getPrivate();//Generating private key
Output:
Please enter any string you want to encrypt
Bahubali College of Engineering, Shravanabelagola
12. Write a program for congestion control using leaky bucket algorithm.
import java.util.*;
public class leaky
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the bucket size");
int bktsize=scan.nextInt();
System.out.println("Enter the output rate:");
int oprate=scan.nextInt();
for(int i=1;i<=5;i++)
{
System.out.println("Enter the size of the packet of"+i);
int pktsize=scan.nextInt();
if(pktsize>bktsize)
{
System.out.println("Bucket Overflow");
}
else
{
while(pktsize>=oprate)
{
System.out.println("Bytes
Outputted"+oprate);
try
{
Thread.sleep(1000);
}
catch( Exception e)
{
}
pktsize=pktsize-oprate;
}
if(pktsize>0)
{
System.out.println("Last Byte
Outputted"+pktsize);
}
}
}
}
Output:
Enter the bucket size
500
Enter the output rate:
100
Enter the size of the packet of1
400
Bytes Outputted100
Bytes Outputted100
Bytes Outputted100
Bytes Outputted100
Enter the size of the packet of2
250
Bytes Outputted100
Bytes Outputted100
Last Byre Outputted50
Enter the size of the packet of3
375
Bytes Outputted100
Bytes Outputted100
Bytes Outputted100
Last Byre Outputted75
Enter the size of the packet of4
550
Bucket Overflow
Enter the size of the packet of5
250
Bytes Outputted100
Bytes Outputted100
Last Byre Outputted50