0% found this document useful (0 votes)
11 views20 pages

CN Manual

Uploaded by

taskeenafifa934
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)
11 views20 pages

CN Manual

Uploaded by

taskeenafifa934
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/ 20

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]
set f [open lab1.tr w]
set nf [open lab1.nam w]
$ns trace-all $f
$ns namtrace-all $nf

proc finish {} {
global f nf ns
$ns flush-trace
close $f
close $nf
exec nam lab1.nam &
exit 0
}ns
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "TCP Source"
$n1 label "UDP Source"
$n2 label "Sink"
$ns color 1 red
$ns color 2 yellow

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


$ns duplex-link $n1 $n2 1Mb 20ms DropTail
$ns queue-limit $n1 $n2 10

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


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

set udp0 [new Agent/UDP]


$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
set null0 [new Agent/Null]
$ns attach-agent $n2 $null0
$ns connect $udp0 $null0
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0
set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink
$ftp0 set maxPkts_ 1000
$ns connect $tcp0 $sink

$udp0 set class_ 1


$tcp0 set class_ 2

$ns at 0.1 "$cbr0 start"


$ns at 1.0 "$ftp0 start"
$ns at 4.0 "$ftp0 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run

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 f [open lab2.tr w]
set nf [open lab2.nam w]
$ns trace-all $f
$ns namtrace-all $nf

proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
exec nam lab2.nam &
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]

$n0 label "ping0"


$n1 label "ping1"
$n2 label "R1"
$n3 label "R2"
$n4 label "ping4"
$n5 label "ping5"

$ns color 1 red


$ns color 2 blue
$ns color 3 green
$ns color 4 orange

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


$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.5Mb 30ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
$ns duplex-link $n3 $n5 1Mb 10ms DropTail

set ping0 [new Agent/Ping]


$ns attach-agent $n0 $ping0
set ping1 [new Agent/Ping]
$ns attach-agent $n1 $ping1

set ping4 [new Agent/Ping]


$ns attach-agent $n4 $ping4
set ping5 [new Agent/Ping]
$ns attach-agent $n5 $ping5
$ns connect $ping0 $ping4
$ns connect $ping1 $ping5

proc sendPingPacket {} {
global ns ping0 ping1 ping4 ping5
set intervalTime 0.001
set now [$ns now]
$ns at [expr $now + $intervalTime] "$ping0 send"
$ns at [expr $now + $intervalTime] "$ping1 send"
$ns at [expr $now + $intervalTime] "$ping4 send"
$ns at [expr $now + $intervalTime] "$ping5 send"

$ns at [expr $now + $intervalTime] "sendPingPacket"


}

Agent/Ping instproc recv {from rtt} {


global seq
$self instvar node_
puts "The node [$node_ id] received an ACK from the node $from
with RTT $rtt ms"
}
$ping0 set class_ 1
$ping1 set class_ 2
$ping4 set class_ 4
$ping5 set class_ 5
$ns at 0.01 "sendPingPacket"
$ns at 10.0 "finish"
$ns run

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 f [open lab3.tr w]
set nf [open lab3.nam w]
$ns trace-all $f
$ns namtrace-all $nf

proc finish {} {
global ns f nf outFile1 outFile2
$ns flush-trace
close $f
close $nf
exec nam lab3.nam &
exec xgraph Congestion1.xg -geometry 400x400 &
exec xgraph Congestion2.xg -geometry 400x400 &
exit 0
}
$ns color 1 red
$ns color 2 green

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]

$n0 label "TCP FTP Source"


$n3 label "Sink Destination"
$n5 label "TCP Telnet Source"
$n7 label "Sink Destination"

$ns make-lan "$n0 $n1 $n2 $n3 $n4 $n5 $n6 $n7" 10Mb 30ms LL
Queue/DropTail Mac/802_3

set tcp1 [new Agent/TCP]


$ns attach-agent $n0 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1
$tcp1 set class_ 1

set tcp2 [new Agent/TCP]


$ns attach-agent $n5 $tcp2
set telnet1 [new Application/FTP]
$telnet1 attach-agent $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $n7 $sink2
$ns connect $tcp2 $sink2
$telnet1 set type_ $sink2
$tcp2 set class_ 2

set outFile1 [open Congestion1.xg w]


set outFile2 [open Congestion2.xg w]

puts $outFile1 "TitleText: Congestion Window Plot for TCP1"


puts $outFile1 "XUnitText: SimulationTime(Secs)"
puts $outFile1 "YUnitText: CongestionWindowSize"
puts $outFile2 "TitleText: Congestion Window Plot for TCP2"
puts $outFile2 "XUnitText: SimulationTime(Secs)"

puts $outFile2 "YUnitText: CongestionWindowSize"

proc findWindowSize {tcpSource outFile} {


global ns
set now [$ns now]
set cWindSize [$tcpSource set cwnd_]
puts $outFile "$now $cWindSize"
$ns at [expr $now + 0.1] "findWindowSize $tcpSource $outFile"
}

$ns at 0.0 "findWindowSize $tcp1 $outFile1"


$ns at 0.1 "findWindowSize $tcp2 $outFile2"
$ns at 0.3 "$ftp1 start"
$ns at 0.5 "$telnet1 start"
$ns at 50.0 "$ftp1 stop"
$ns at 50.0 "$telnet1 stop"
$ns at 50.0 "finish"
$ns run
4. Develop 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());

/* System.out.print("Data bits are : ");


for(int i=0; i< data_bits; i++)
System.out.print(data[i]);
System.out.println();
System.out.print("divisor bits are : ");
for(int i=0; i< divisor_bits; i++)
System.out.print(divisor[i]);
System.out.println();

*/ 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());

for(int j=0; j<crc.length; j++)


{

rem[j] = crc[j];
}

rem=divide(crc, divisor, rem);

for(int i=0; i< rem.length; i++)


{
if(rem[i]!=0)
{
System.out.println("Error");
break;
}
if(i==rem.length-1)
System.out.println("No Error");
}

System.out.println("THANK YOU.... :)");


}

static int[] divide(int div[],int divisor[], int rem[])


{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);

while(rem[cur]==0 && cur!=rem.length-1)


cur++;

if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}

5.Develop a program to implement a sliding window protocol in the data link layer.
import java.util.Scanner;

public class SlidingWindowProtocol {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Get the window size


System.out.print("Enter window size: ");
int windowSize = scanner.nextInt();

// Get the number of frames to send


System.out.print("Enter the number of frames to send: ");
int numFrames = scanner.nextInt();

// Simulate sending and receiving frames


int[] frames = new int[numFrames];
for (int i = 0; i < numFrames; i++) {
frames[i] = i + 1;
}

int ack = 0;
int i = 0;

while (i < numFrames) {


// Sending frames in the window
System.out.println("Sending frames:");
for (int j = i; j < i + windowSize && j < numFrames; j++) {
System.out.println("Sent frame " + frames[j]);
}

// Simulate receiving an acknowledgment for each frame


System.out.println("Receiving acknowledgment for frames:");
for (int j = i; j < i + windowSize && j < numFrames; j++) {
System.out.print("Enter acknowledgment for frame " + frames[j] + ": ");
ack = scanner.nextInt();

// Check if acknowledgment is correct


if (ack == frames[j]) {
System.out.println("Acknowledgment received for frame " + frames[j]);
} else {
System.out.println("Acknowledgment not received for frame " + frames[j] + ".
Resending...");
break;
}

// Move window if acknowledgment is received


if (j == i + windowSize - 1 || j == numFrames - 1) {
i = j + 1;
}
}
}

System.out.println("All frames sent successfully!");

scanner.close();
}
}

6.Develop a program to find the shortest path between vertices using the Bellman-Ford and
path vector routing algorithm
import java.util.Scanner;

public class BellmanFord


{
private int D[];
private int num_ver;
public static final int MAX_VALUE = 999;

public BellmanFord(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])
System.out.println("The Graph contains negative
egde cycle");
}
}
}
for (int vertex = 1; vertex <= num_ver; vertex++)
{
System.out.println("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(System.in);
System.out.println("Enter the number of
vertices"); num_ver = scanner.nextInt();
int A[][] = new int[num_ver + 1][num_ver + 1];
System.out.println("Enter the adjacency matrix");
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{

A[sn][dn] = scanner.nextInt();
if (sn == dn)
{
A[sn][dn] = 0;
continue;
}
if (A[sn][dn] == 0)
{
A[sn][dn] = MAX_VALUE;
}
}
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
BellmanFord b = new BellmanFord
(num_ver);
b.BellmanFordEvaluation(source, A);
scanner.close();
}
}

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 java.net.*;
import java.io.*;
class TCPserver{
public static void main(String args[]) throws Exception {
ServerSocket ss=new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket s=ss.accept();
System.out.println("Connection is successful and waiting for chatting");
InputStream istream=s.getInputStream();
BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));
String fname=fileRead.readLine();
BufferedReader contentRead=new BufferedReader(new FileReader(fname));
OutputStream ostream=s.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=contentRead.readLine())!=null)
pwrite.println(str);
s.close();
ss.close();
pwrite.close();
fileRead.close();
contentRead.close();
}
}

Note: Create two different files Client.java and Server.java. Follow the steps given:
1. Open a terminal run the server program and provide the filename to send
2. Open one more terminal run the client program and provide the IP address of the
server. We can give localhost address “127.0.0.1” as it is running on same machine
or give the IP address of the machine.
3. Send any start bit to start sending file.
4. Refer https://www.tutorialspoint.com/java/java_networking.htmfor all the
parameters, methods description in socket communication.

At server side:
C:\Users\SP\Desktop\JavaP>javac TCPserver.java
C:\Users\SP\Desktop\JavaP>java TCPserver
Server ready for connection
Connection is successful and waiting for chatting
At client side:
C:\Users\SP\Desktop\JavaP>javac TCPclient.java
C:\Users\SP\Desktop\JavaP>java TCPclient
Enter the file name
TCP server.java
import java.net.*;
import java.io.*;
class TCPserver{
public static void main(String args[]) throws Exception {
ServerSocket ss=new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket s=ss.accept();
System.out.println("Connection is successful and waiting for chatting");
InputStream istream=s.getInputStream();
BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));
String fname=fileRead.readLine();
BufferedReader contentRead=new BufferedReader(new FileReader(fname));
OutputStream ostream=s.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=contentRead.readLine())!=null)
pwrite.println(str);
s.close();
ss.close();
pwrite.close();
fileRead.close();
contentRead.close();
}
}
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 java.io.*;
import java.net.*;

class UDPServer
{
public static void main(String args[])
{
try{ String message=args[0];
int serverPortNumber=Integer.parseInt(args[1]);
ServerSocket connectionSocket=new ServerSocket(serverPortNumber);

Socket dataSocket=connectionSocket.accept();

PrintStream socketOutput=new PrintStream

(dataSocket.getOutputStream());

socketOutput.println(message);

socketOutput.println("Sent response to client");

socketOutput.flush();

dataSocket.close();

connectionSocket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Steps to Run the Program

> javac UDPServer.java

> java UDPServer "Hi" 1000

At Server side Output:

C:\Users\SP\Desktop\JavaP>javac UDPServer.java

C:\Users\SP\Desktop\JavaP>java UDPServer "Hi" 1000

C:\Users\SP\Desktop\JavaP>

UDP Client

import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[])
{
try
{
InetAddress acceptorHost=InetAddress.getByName(args[0]);
int ServerPortNum=Integer.parseInt(args[1]);
Socket clientSocket=new Socket(acceptorHost,ServerPortNum);
BufferedReader br=new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
System.out.println(br.readLine());
clientSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Steps to Run the Program

> javac UDPClient.java


> java UDPClient 127.0.0.1 1000

At Client side Output:

C:\Users\SP\Desktop\JavaP>javac UDPClient.java

C:\Users\SP\Desktop\JavaP>java UDPClient 127.0.0.1 1000


Hi

9.Develop a program for a simple RSA algorithm to encrypt and decrypt the data.
RSA Key Generation
import java.util.*;
import java.math.BigInteger;
import java.lang.*;

class RSAkeygen
{
public static void main(String[] args)
{
Random rand1=new Random(System.currentTimeMillis());
Random rand2=new Random(System.currentTimeMillis()*10);

int pubkey=Integer.parseInt(args[0]);

BigInteger bigB_p=BigInteger.probablePrime(32, rand1);


BigInteger bigB_q=BigInteger.probablePrime(32, rand2);

BigInteger bigB_n=bigB_p.multiply(bigB_q);

BigInteger bigB_p_1=bigB_p.subtract(new BigInteger("1"));


BigInteger bigB_q_1=bigB_q.subtract(new BigInteger("1"));

BigInteger bigB_p_1_q_1=bigB_p_1.multiply(bigB_q_1);

while(true)
{
BigInteger BigB_GCD=bigB_p_1_q_1.gcd(new BigInteger(""+pubkey));
if(BigB_GCD.equals(BigInteger.ONE))
{
break;
}
pubkey++;
}
BigInteger bigB_pubkey=new BigInteger(""+pubkey);

BigInteger bigB_prvkey=bigB_pubkey.modInverse(bigB_p_1_q_1);
System.out.println("public key : "+bigB_pubkey+","+bigB_n);
System.out.println("private key : "+bigB_prvkey+","+bigB_n);
}
}
Steps to Compile & Run the Program

> javac RSAkeygen.java

> java RSAkeygen 20

Public key : 23,11070015003782698357


Private key : 3850439998963313063,11070015003782698357

Output:
Key Generation

RSA Encryption and Decryption

import java.math.BigInteger;
import java.util.*;

class RSAEncDec
{
public static void main(String[] args)
{
BigInteger bigB_pubkey = new BigInteger(args[0]);
BigInteger bigB_prvkey = new BigInteger(args[1]);

BigInteger bigB_n = new BigInteger(args[2]);

int asciiVal=Integer.parseInt(args[3]);

BigInteger bigB_val=new BigInteger(""+asciiVal);


BigInteger bigB_cipherVal=bigB_val.modPow(bigB_pubkey,bigB_n);
System.out.println("Cipher text: " + bigB_cipherVal);

BigInteger bigB_plainVal=bigB_cipherVal.modPow(bigB_prvkey,bigB_n);
int plainVal=bigB_plainVal.intValue();

System.out.println("Plain text:" + plainVal);


}
}
Steps to Compile & Run the Program

> javac RSAEncDec.java

> java RSAEncDec 23 3850439998963313063 11070015003782698357 10

Encryption and Decryption


10 Develop a program for congestion control using a leaky bucket algorithm.
import java.io.*;
import java.util.*;

class Queue
{
int q[],f=0,r=0,size;
void insert(int n)
{
Scanner in = new Scanner(System.in);
q=new int[10];
for(int i=0;i<n;i++)
{
System.out.print("\nEnter " + i + " element: ");
int ele=in.nextInt();
if(r+1>10)
{
System.out.println("\nQueue is full \nLost Packet: "+ele);
break;
}
else
{
r++;
q[i]=ele;
}
}
}
void delete()
{
Scanner in = new Scanner(System.in);
Thread t=new Thread();
if(r==0)
System.out.print("\nQueue empty ");
else
{
for(int i=f;i<r;i++)
{
try
{
t.sleep(1000);
}

catch(Exception e){}
System.out.print("\nLeaked Packet: "+q[i]);
f++;
}
}
System.out.println();
}
}
class Leaky extends Thread
{
public static void main(String ar[]) throws Exception
{
Queue q=new Queue();
Scanner src=new Scanner(System.in);
System.out.println("\nEnter the packets to be sent:");
int size=src.nextInt();
q. insert(size);
q.delete();
}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy