CNLAb Commands - Updated
CNLAb Commands - Updated
knsit@dell-optiplex-3060:~$ ns lab11.tcl
knsit@dell-optiplex-3060:~$ nam out.nam
Segmentation fault (core dumped)
https://drive.google.com/file/d/1dkNPNN23Vy6o_zT0uIRBgUAo1VCXcVOY/view
copy that folder which you have downloaded to the desktop from downloads
nam_1.14_amd64.deb
gedit lab11.tcl
ns lab11.tcl
gedit lab11.awk
gedit one.tcl
awk file
BEGIN{ c=0; }
{
If ($1== "d")
{
c++;
printf("%s\t%s\n",$5,$11);
}
}
END{printf("the total number of packets dropped due to congestion %d\n",c);
}
program 2
gedit lab12.tcl
proc finish {} {
global ns tf nf
exec nam lab12.nam &
$ns flush-trace
close $tf
close $nf
exit 0
}
$ns run
BEGIN {
#include<stdio.h>
COUNT=0
}
{
if($1=="d")
count++
}
END {
printf("\nthe total number of packet dropped due to conjution %d\n",count);
}
Run
ns lab12.tcl
awk -f lab12.awk lab12.tr
output:
ns lab12.tcl
proc finish {} {
global nf tf ns
$ns flush-trace
exec nam lab13.nam &
close $nf
close $tf
exit 0
}
$ns make-lan "$n0 $n1 $n2 $n3" 10Mb 10ms LL Queue/DropTail Mac/802_3
$ns run
AWK file
BEGIN{
#include<stdio.h>
}
{
if($6=="cwnd_")
printf("%f\t%f\n",$1,$7);
}
END{
puts "DONE"
}
commands to Run
gedit lab13.awk
knsit@dell-optiplex-3060:~$ ns lab13.tcl
warning: no class variable LanRouter::debug_
remove this line from tcl file and note the graph without congestion
create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$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 file
BEGIN {
c1 = 0;
c2 = 0;
p1 = 0;
p2 = 0;
t1 = 0;
t2 = 0;
}
{
if ($1 == "r" && $3 == "_1_" && $4 == "AGT") {
c1++;
p1 += $8;
t1 = $2;
}
END {
printf("The throughput from n0 to n1 is %f Mbps\n", (c1 * p1 * 8) / (t1 * 1000000));
printf("The throughput from n1 to n2 is %f Mbps\n", (c2 * p2 * 8) / (t2 * 1000000));
}
outpur
knsit@dell-optiplex-3060:~$ ns lab4.tcl
warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
num_nodes is set 3
INITIALIZE THE LIST xListHead
channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
highestAntennaZ_ = 1.5, distCST_ = 550.0
SORTING LISTS ...DONE!
knsit@dell-optiplex-3060:~$ gedit lab4.awk
knsit@dell-optiplex-3060:~$ awk -f lab4.awk lab4.tr
The throughput from n0 to n1 is 5863.442245 Mbps
The throughput from n1 to n2 is 1307.611834 Mbps
knsit@dell-optiplex-3060:~$ gedit lab4.tcl
PART B
12.Write a program for congestion control using leaky bucket algorithm.
The main concept of the leaky bucket algorithm is that the output data flow remains constant
despite the variant input traffic, such as the water flow in a bucket with a small hole at the bottom.
In case the bucket contains water (or packets) then the output flow follows a constant rate, while if
the bucket is full any additional load will be lost because of spillover. In a similar way if the bucket
is empty the output will be zero. From network perspective, leaky bucket consists of a finite queue
(bucket) where all the incoming packets are stored in case there is space in the queue, otherwise
the packets are discarded. In order to regulate the output flow, leaky bucket transmits one packet
from the queue in a fixed time (e.g. at every clock tick). In the following figure we can notice the
main rationale of leaky bucket algorithm, for both the two approaches (e.g. leaky bucket with
water (a) and with packets (b)).
While leaky bucket eliminates completely bursty traffic by regulating the incoming data flow its
main drawback is that it drops packets if the bucket is full. Also, it doesn’t take into account the
idle process of the sender which means that if the host doesn’t transmit data for some time the
bucket becomes empty without permitting the transmission of any packet.
import java.util.*;
System.out.println("Bucket overflow");
} else {
if (pktsize > 0) {
output = read.nextInt();
n = read.nextInt();
pktsize = rand.nextInt(1000);
solution(pktsize, output);
}
commands to Run
java Bucket
Enter output rate
100
Enter the number of packets
5
packetno: 1 packetsize=735
Bucket overflow
packetno: 2 packetsize=39
39bytes outputed
packetno: 3 packetsize=772
Bucket overflow
packetno: 4 packetsize=965
Bucket overflow
packetno: 5 packetsize=102
100bytes outputed
2bytes outputed
program 11
11.WRITE A PROGRAM FOR SIMPLE RSA ALGORITHM TO ENCRYPT AND DECRYPT THE
DATA.
RSA is an example of public key cryptography. It was developed by Rivest, Shamir and Adelman.
The RSA algorithm can be used for both public key encryption and digital signatures. Its security
is based on the difficulty of factoring large integers.
The RSA algorithm's efficiency requires a fast method for performing the modular exponentiation
operation. A less efficient, conventional method includes raising a number (the input) to a power
(the secret or public key of the algorithm, denoted e and d, respectively) and taking the remainder
of the division with N. A straight-forward implementation performs these two steps of the operation
sequentially: first, raise it to the power and second, apply modulo. The RSA algorithm comprises
of three steps, which are depicted below:
Key Generation Algorithm:
Ø Generate two large random primes, p and q, of approximately equal size such that their product
n = p*q
Ø Compute n = p*q and Euler‟s totient function (φ) phi(n) = (p-1)(q-1).
Ø Choose an integer e, 1 < e < phi, such that gcd(e, phi) = 1.
Ø Compute the secret exponent d, 1 < d < phi, such that e*d ≡ 1 (mod phi).
Ø The public key is (e, n) and the private key is (d, n). The values of p, q, and phi should also be
kept secret.
Encryption:
Sender A does the following:
Ø Using the public key (e,n)
Ø Represents the plaintext message as a positive integer M
Ø Computes the cipher text C = M^e mod n.
Ø Sends the cipher text C to B (Receiver).
Decryption:
Recipient B does the following:
Ø Uses his private key (d, n) to compute M = C^d mod n.
Ø Extracts the plaintext from the integer representative m.
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
public class RSA {
BigInteger n; // n is the modulus for both the private and public keys
// We use 512 bits here, but best practice for security is 2048 bits.
// Change 512 to 2048, recompile, and run the program again and you will
// notice it takes much longer to do the math with that many bits.
n = p.multiply(q);
e = new BigInteger("65537");
d = e.modInverse(phi);
String s;
s = in.nextLine();
//m is the byte representation of the message to be encrypted
commands to run
Program 8: Write a program to find the shortest path between vertices using bellman-ford
algorithm.
Distance Vector Algorithm is a decentralized routing algorithm
that requires that each router simply inform its neighbors of its routing table. For each network path, t
he receiving routers pick the neighbor advertising the lowest cost, then add this entry into its routing
table for re-advertisement. To find the shortest path, Distance Vector Algorithm is based on one of two
basic algorithms: the Bellman-Ford and the Dijkstra algorithms.
Routers that use this algorithm have to maintain the distance tables (which is a one- dimension array
-- "a vector"), which tell the distances and shortest path to sending packets to each node in the network.
The information in the distance table is always up date by exchanging information with the neighborin
g nodes. The number of data in the table equals to that of all nodes in networks (excluded itself). Th
e columns of table represent the directly attached neighbors whereas the rows represent all destinati
ons in the network. Each data contains the path for sending packets to each destination in the network
and distance/or time to transmit on that path (we call this as "cost"). The measurements in this algorith
m are the number of hops, latency, the number of outgoing packets, etc.
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source
vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for th
e same problem, but more versatile, as it is capable of handling graphs in which some of the edge weig
hts are negative numbers. Negative edge weights are found in various applications of graphs, hence th
e usefulness of this algorithm. If a graph contains a "negative cycle" (i.e. a cycle whose edges sum to a
negative value) that is reachable from the source, then there is no cheapest
path: any path that has a point on the negative cycle can be made cheaper by one
more walk around the negative cycle. In such a case, the Bellman–Ford algorithm can detect negative
cycles and report their existence.
import java.util.Scanner;
}
}
for (int vertex = 1; vertex <= num_ver; vertex++) {
System.out.println("distance of source " + source + " to " + vertex + " is " + D[vertex]);
}
}
commands to Run:
knsit@dell-optiplex-3060:~$ gedit BellmanFord.java
knsit@dell-optiplex-3060:~$ javac BellmanFord.java
knsit@dell-optiplex-3060:~$ java BellmanFord
OUTPUT 1:
Enter the number of vertices
4
Enter the cost adjacency matrix, 0 for self loop, 999 for no edge
0 4 999 5
999 0 999 5
999 -10 0 999
999 999 3 0
Enter the source vertex
1
The Graph contains negative edge cycle
output 2:
knsit@dell-optiplex-3060:~$ java BellmanFord
Enter the number of vertices
4
Enter the cost adjacency matrix, 0 for self loop, 999 for no edge
0 4 999 5
999 0 999 999
999 -10 0 999
999 999 3 0
Enter the source vertex
1
distance of source 1 to 1 is 0
distance of source 1 to 2 is -2
distance of source 1 to 3 is 8
distance of source 1 to 4 is 5
OUTPUT 3:
2 0 3 999 1 5
999 3 0 4 999 2
4 1 999 999 0 1
999 5 2 3 1 0
Enter the source vertex
1
distance of source 1 to 1 is 0
distance of source 1 to 2 is 2
distance of source 1 to 3 is 5
distance of source 1 to 4 is 7Distance Vector Algorithm is a decentralized routing algorithm that
requires that each router simply inform its neighbors of its routing table. For each network path,
the receiving routers pick the neighbor advertising the lowest cost, then add this entry into its
routing table for re-advertisement. To find the shortest path, Distance Vector Algorithm is based
on one of two basic algorithms: the Bellman-Ford and the Dijkstra algorithm
distance of source 1 to 5 is 3
distance of source 1 to 6 is 4
10. Write a program on datagram socket for client/server to display the messages on
client side, typed at the server side
Server file
import java.util.Scanner;
import java.net.*;
import java.io.*;
String ch;
try {
System.out.println("Server Started!!");
skt.receive(request);
ch = input.nextLine();
skt.send(reply);
skt.close();
Client file
import java.util.Scanner;
import java.net.*;
import java.io.*;
DatagramSocket skt;
try {
byte[] b = msg.getBytes();
skt.send(request);
skt.receive(reply);
skt.close();
Commands to run
first terminal
second terminal
first terminal
java Udps
second terminal
java Udpc
Server file
import java.net.*;
import java.io.*;
try
/*The server invokes the accept() method of the ServerSocket class. This method
waits until a client connects to the server on the given port.*/
Socket sock=sersock.accept();
InputStream istream=sock.getInputStream();
String fname=fileRead.readLine();
// reading file contents
OutputStream ostream=sock.getOutputStream();
String str;
sock.close();
sersock.close();
pwrite.close();
fileRead.close();
catch(Exception e)
}
Client Server:
import java.net.*;
import java.io.*;
//client creates a Socket object, specifying the server name and the port number to
connect to.
String fname=keyread.readLine();
try{
OutputStream ostream=sock.getOutputStream();
pwrite.println(fname);
InputStream istream=sock.getInputStream();
String str;
// reading line-by-line
while((str=socketRead.readLine())!=null)
System.out.println(str);
catch(Exception e)
Output:
open 2 terminal
type any file name which is in that directory /.java or text file etc
client will display the content of the file
1. Write a program for error detecting code using CRC-CCITT (16-bits).
import java.io.*;
class Crc
{
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];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/*------------------ CRC GENERATION-----------------------*/
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("Dividend (after appending 0's) are : ");
for(int i=0; i< div.length; i++) System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++)
{
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
for(int i=0;i<div.length;i++) //append dividend and ramainder
{
crc[i]=(div[i]^rem[i]);
}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
/*-------------------ERROR DETECTION---------------------*/
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
output: