CN Lab
CN Lab
*;
set ns [new Simulator] import java.net.*;
set nf [open 1.nam w] public class Sender {
$ns namtrace-all $nf public static void main(String[] args) throws
set tf [open 1.tr w] Exception {
$ns trace-all $tf DatagramSocket socket = new
proc finish { } { DatagramSocket();
global ns nf tf InetAddress receiverAddress =
$ns flush-trace InetAddress.getByName("localhost");
close $nf int receiverPort = 9876;
5
close $tf int windowSize = 4;
exec nam 1.nam &
exit 0
}
set n0 [$ns node]
1 byte[] data = "Hello, Sliding
Window!".getBytes();
int base = 0;
while (base < data.length) {
Set n1 [$ns node] for (int i = base; i < base + windowSize && i <
set n2 [$ns nodes] data.length; i++) {
$ns duplex-link $n0 $n1 500Mb 10ms DropTail DatagramPacket packet = new
$ns duplex-link $n1 $n2 100Mb 5ms Droptail DatagramPacket(data, i, 1, receiverAddress,
$ns queue-limit $n0 $n1 50 receiverPort);
$n0 label "UDP" socket.send(packet);
$n2 label "NULL’ }
set udp0 [new Agent/UDP] DatagramPacket ackPacket = new
$ns attach-agent $n0 $udp0 DatagramPacket(new byte[1], 1);
set null0 [new Agent/Null] socket.receive(ackPacket);
$ns attach-agent $n2 $null0 int ack = ackPacket.getData()[0];
$ns color 1 Red if (ack >= base) {
$udp0 set fid_ 1 base = ack + 1; } }
set cbr0 [new Application/Traffic/CBR] socket.close(); }}
$cbr0 set packetSize_ 5000 Receiver.java:
$cbr0 set interval_ 0.005 import java.io.*;
$cbr0 attach-agent $udp0 import java.net.*;
$ns connect $udp0 $null0 public class Receiver
$ns at 0.1 "$cbr0 start" {
$ns at 1.0 "finish" public static void main(String[] args) throws
$ns run Exception {
ns 1.tcl DatagramSocket socket = new
DatagramSocket(9876);
1.awk int expectedSeqNum = 0;
BEGIN {c=0;} while (true){
{ Output: DatagramPacket packet = new
If ($1= ="d") ns 1.tcl DatagramPacket(new byte[1], 1);
{ socket.receive(packet);
c++; int seqNum = packet.getData()[0];
printf("%s\t%s\n",$5,$11); if (seqNum == expectedSeqNum) {
} System.out.println("Received: " + seqNum);
} DatagramPacket ackPacket = new
END { DatagramPacket(new byte[] { (byte) seqNum
printf("The number of packets dropped }, 1, packet.
=%d\n",c); } getAddress(), packet.getPort());
ns 1.awk socket.send(ackPacket);
After simulation is completed run: awk -f expectedSeqNum++; }}}}
1.awk 1.tr
gedit 2.tcl $ns at 0.6 "$p1 send"
set ns [ new Simulator ] $ns at 0.7 "$p1 send"
set nf [ open 2.nam w ] $ns at 0.8 "$p1 send"
2
$ns namtrace-all $nf $ns at 0.9 "$p1 send"
set tf [ open 2.tr w ] $ns at 1.0 "$p1 send"
$ns trace-all $tf $ns at 1.1 "$p1 send"
set n0 [$ns node] $ns at 1.2 "$p1 send"
set n1 [$ns node] $ns at 1.3 "$p1 send“
set n2 [$ns node $ns at 1.4 "$p1 send"
set n3 [$ns node] $ns at 1.5 "$p1 send"
set n4 [$ns node] $ns at 1.6 "$p1 send"
set n5 [$ns node] $ns at 1.7 "$p1 send" $ns at 3.0 "finish"
$ns duplex-link $n0 $n4 1005Mb 1ms DropTail $ns at 1.8 "$p1 send" $ns run
$ns duplex-link $n1 $n4 50Mb 1ms droptail $ns at 1.9 "$p1 send" AWK file
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail $ns at 2.0 "$p1 send" 2.awk
$ns duplex-link $n3 $n4 200mb 1ms DropTail $ns at 2.1 "$p1 send" BEGIN{
$ns duplex-link $n4 $n5 1Mb 1ms DropTaii $ns at 2.2 "$p1 send" drop=0;
set p1 [new Agent/Ping] $ns at 2.3 "$p1 send" }
$ns attach-agent $n0 $p1 $ns at 2.4 "$p1 send" {
$p1 set packetSize_ 50000 $ns at 2.5 "$p1 send" if($1=="d")
$p1 set interval_ 0.0001 $ns at 2.6 "$p1 send" {
set p2 [new Agent/Ping] $ns at 2.7 "$p1 send" drop++;
$ns attach-agent $n1 $p2 $ns at 2.8 "$p1 send" }
set p3 [new Agent/Ping] $ns at 2.9 "$p1 send” }
$ns attach-agent $n2 $p3 $ns at 0.1 "$p3 send" END{
$p3 set packetSize_ 30000 $ns at 0.2 "$p3 send" printf("Total number of %s
$p3 set interval_ 0.00001 $ns at 0.3 "$p3 send" packets dropped
set p4 [new Agent/Ping] $ns at 0.4 "$p3 send" due to congestion
$ns attach-agent $n3 $p4 $ns at 0.5 "$p3 send" =%d\n",$5,drop);
set p5 [new Agent/Ping] $ns at 0.6 "$p3 send" }
$ns attach-agent $n5 $p5 $ns at 0.7 "$p3 send"
$ns queue-limit $n0 $n4 5 $ns at 0.8 "$p3 send"
$ns queue-limit $n2 $n4 3 $ns at 0.9 "$p3 send" Output:
$ns queue-limit $n4 $n5 2 $ns at 1.0 "$p3 send"
Agent/Ping instproc recv {from rtt} { $ns at 1.1 "$p3 send" awk –f 2.awk 2.tr
$self instvar node_ $ns at 1.2 "$p3 send"
puts "node [$node_ id]received answer from $ns at 1.3 "$p3 send"
$from with round trip time $rtt msec" $ns at 1.4 "$p3 send"
} $ns at 1.5 "$p3 send"
$ns connect $p1 $p5 $ns at 1.6 "$p3 send"
$ns connect $p3 $p4 $ns at 1.7 "$p3 send"
proc finish { } { $ns at 1.8 "$p3 send"
global ns nf tf $ns at 1.9 "$p3 send"
$ns flush-trace $ns at 2.0 "$p3 send"
close $nf $ns at 2.1 "$p3 send"
close $tf $ns at 2.2 "$p3 send"
exec nam p2.nam & $ns at 2.3 "$p3 send"
exit 0 $ns at 2.4 "$p3 send"
} $ns at 2.5 "$p3 send"
$ns at 0.1 "$p1 send" $ns at 2.6 "$p3 send"
$ns at 0.2 "$p1 send" $ns at 2.7 "$p3 send"
$ns at 0.3 "$p1 send" $ns at 2.8 "$p3 send"
$ns at 0.4 "$p1 send" $ns at 2.9 "$p3 send"
$ns at 0.5 "$p1 send"
set ns [new Simulator]
$ns color 1 Blue set sink1 [new Agent/TCPSink/DelAck]
$ns color 2 Red $ns attach-agent $n(1) $sink1
3
set ntrace [open 3.tr w] $ns connect $tcp1 $sink1
$ns trace-all $ntrace $tcp1 set fid_ 2
set namfile [open 3.nam w] $tcp1 set window_ 8000
$ns namtrace-all $namfile $tcp1 set packetSize_ 552
proc Finish {} { set ftp1 [new Application/FTP]
global ns ntrace namfile $ftp1 attach-agent $tcp1
$ns flush-trace $ftp1 set type_ FTP
close $ntrace set file1 [open file1.tr w]
close $namfile $tcp0 attach $file1
exec nam 3.nam & set file2 [open file2.tr w]
puts "The number of packet drops due to $tcp1 attach $file2
collision is:" $tcp0 trace cwnd_
exec grep "^d" 3.tr | cut -d "" -f 4 | grep -c "3"& $tcp1 trace cwnd_
exit 0 $ns at 0.1 "$ftp0 start"
} $ns at 0.2 "$ftp1 start"
for {set i 0} {$i < 6} {incr i} { $ns at 24.8 "$ftp0 stop"
set n($i) [$ns node] $ns at 24.9 "$ftp1 stop"
} $ns at 25.0 "Finish"
$ns duplex-link $n(0) $n(2) 2Mb 10ms DropTail $ns run
$ns duplex-link $n(1) $n(2) 2Mb 10ms DropTail AWK
$ns duplex-link $n(2) $n(3) 0.2Mb 100ms BEGIN {
DropTail }
set lan0 [$ns newLan "$n(3) $n(4) $n(5)" 0.5Mb {
40ms LL Queue/DropTail MAC/802_3 if($6 == "cwnd_")
Channel] printf("%f\t%f\t\n",$1,$7);
$ns duplex-link-op $n(0) $n(2) orient right-down }
$ns duplex-link-op $n(1) $n(2) orient right-up END {
$ns duplex-link-op $n(2) $n(3) orient right }
$ns queue-limit $n(2) $n(3) 20
$ns simplex-link-op $n(2) $n(3) queuePos 0.5
set tcp0 [new Agent/TCP/Newreno]
$ns attach-agent $n(0) $tcp0
set sink0 [new Agent/TCPSink/DelAck]
$ns attach-agent $n(4) $sink0
$ns connect $tcp0 $sink0
$tcp0 set fid_ 1
$tcp0 set window_ 8000
$tcp0 set packetSize_ 552
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set type_ FTP
set tcp1 [new Agent/TCP/Newreno]
$ns attach-agent $n(5) $tcp1
import java.util.Scanner;
public class crc
{
public static StringBuffer t=new StringBuffer();
public static StringBuffer cs=new StringBuffer();
public static StringBuffer g=new
StringBuffer("10001000000100001");
public static int a,e,c;
public static int N=g.length();
public static void xor()
{
for(c=1;c<N;c++)
cs.setCharAt(c, ((cs.charAt(c)
==g.charAt(c))?'0':'1'));
}
public static void crc1()
{
for(e=0;e<N;e++)
cs.insert(e, t.charAt(e));
e--;
Do
4 System.out.println("Enter position
where error is to inserted:");
{ e=sc.nextInt();
if(cs.charAt(0)=='1') t.setCharAt(e, ((t.charAt(e)=='0')?'1':'0'));
xor(); System.out.println("Erroneous data :"+t);
for(c=0;c<N-1;c++) crc1();
cs.setCharAt(c, cs.charAt(c+1)); for(e=0;(e<N-1)&&(cs.charAt(e)!='1');e++);
cs.setCharAt(c, t.charAt(e++)); if(e<N-1)
}while(e<a+N-1); System.out.println("Error detected");
cs.setCharAt(16,'\0'); else
} System.out.println("No error detected");
public static void main(String[] args) }
{ else
System.out.println("\n Enter polynomial:"); System.out.println("No error detected");
Scanner sc = new Scanner(System.in); sc.close();
t.append(sc.nextLine()); }
System.out.println("\n generating polynomial is }
:"+g);
a=t.length();
for(e=a;e<a+N-1;e++)
t.insert(e,'0');
System.out.println("\n modified t[u] is :"+t);
crc1();
System.out.println("\n checksum is : "+cs);
for(e=a;e<a+N-1;e++)
t.setCharAt(e,cs.charAt(e-a));
System.out.println("\n final codeword is:"+ t);
System.out.println(" Test error detection 0(yes)
1(no)?:");
e=sc.nextInt();
if(e==0)
{
import java.util.Scanner;
public class BellmanFord
{
private int D[];
{
private int num_ver;
System.out.println("distance of source " + source
public static final int MAX_VALUE = 999;
+ " to "+ vertex + " is " + D[vertex]);
public BellmanFord(int num_ver)
}
{
}
this.num_ver = num_ver; D = new int[num_ver +
public static void main(String[ ] args)
1];
{
}
int num_ver = 0;
public void BellmanFordEvaluation(int source,
int source;
int A[][])
Scanner scanner = new Scanner(System.in);
{
System.out.println("Enter the number of
for (int node = 1; node <= num_ver; node++)
vertices");
{
num_ver = scanner.nextInt();
D[node] = MAX_VALUE;
int A[][] = new int[num_ver + 1][num_ver + 1];
}
System.out.println("Enter the adjacency
D[source] = 0;
matrix");
for (int node = 1; node <= num_ver - 1; node++)
for (int sn = 1; sn <= num_ver; sn++)
{
{
for (int sn = 1; sn <= num_ver; sn++)
for (int dn = 1; dn <= num_ver; dn++)
{
{
for (int dn = 1; dn <= num_ver; dn++)
A[sn][dn] = scanner.nextInt();
{
if (sn == dn)
if (A[sn][dn] != MAX_VALUE)
{
{
6
A[sn][dn] = 0;
if (D[dn] > D[sn]+ A[sn][dn])
continue;
D[dn] = D[sn] + A[sn][dn];
}
}
if (A[sn][dn] == 0)
}
{
}
A[sn][dn] = MAX_VALUE;
}
}
for (int sn = 1; sn <= num_ver; sn++)
}
{
}
for (int dn = 1; dn <= num_ver; dn++)
System.out.println("Enter the source vertex");
{
source = scanner.nextInt();
if (A[sn][dn] != MAX_VALUE)
BellmanFord b = new BellmanFord (num_ver);
{
b.BellmanFordEvaluation(source, A);
if (D[dn] > D[sn]+ A[sn][dn])
scanner.close();
System.out.println("The Graph contains negative
}
egde cycle");
}
}
}
}
for (int vertex = 1; vertex <= num_ver; vertex++)
TCP ServeR
import java.util.Scanner;
TCP Client Program: import java.net.*;
import java.io.*;
import java.net.*; public class TcpServer
import java.io.*; {
import java.util.Scanner; public static void main(String args[]) throws
public class TcpClient IOException
{ {
public static void main(String args[]) ServerSocket ss=null;
{
Socket s;
while(true)
{
7a Socket s=null;
try
{
7B
ss=new ServerSocket(3000);
try
{ }
s=new Socket("127.0.0.1",3000); catch(Exception e)
OutputStream ostream=s.getOutputStream(); { e.printStackTrace();
System.out.println("enter filename"); }
Scanner input= new Scanner(System.in); while(true)
String fname= input.nextLine(); {
PrintWriter pwrite = new PrintWriter(ostream, try
true); {
pwrite.println(fname); System.out.println("SERVER READY!!..");
InputStream istream= s.getInputStream(); s=ss.accept();
Scanner cRead= new Scanner (new System.out.println("CLIENT CONNECTED..");
InputStreamReader(istream)); InputStream istream = s.getInputStream();
while(cRead.hasNext()) Scanner fread= new Scanner(new
System.out.println(cRead.nextLine()); InputStreamReader(istream));
pwrite.close(); String fileName= fread.nextLine();
s.close(); System.out.println("reading contents of
} "+fileName);
catch(Exception e) Scanner contentRead= new Scanner(new
{ e.printStackTrace();} FileReader(fileName));
} OutputStream ostream= s.getOutputStream();
} PrintWriter pwrite = new
} PrintWriter(ostream,true);
String str;
while(contentRead.hasNext())
pwrite.println(contentRead.nextLine());
pwrite.close();
s.close();
}
catch(FileNotFoundException e1)
{
System.out.println("file not found");
OutputStream ostream =s.getOutputStream();
PrintWriter pwrite= new
PrintWriter(ostream,true);
pwrite.close();
}
catch(Exception e)
{}}}}
UDP Client: UDP Server:
import java.util.Scanner;
import java.net.*;
import java.io.*; 8a
import java.util.Scanner;
import java.net.*;
import java.io.*;
public class p10udpserver
8B
public class p10udpclient
{ {
public static void main(String args[]) public static void main(String args[])
{ {
Scanner in=new Scanner(System.in); Scanner in=new Scanner(System.in);
DatagramSocket skt; DatagramSocket skt=null;
try try
{ {
skt=new DatagramSocket(); skt=new DatagramSocket(3780);
InetAddress System.out.println("Server is ready");
host=InetAddress.getByName("127.0.0.1"); while(true)
int s_port=3780; {
while(true) byte buffer[]=new byte[1024];
{ DatagramPacket req= new
System.out.println("Client"); DatagramPacket(buffer,buffer.length);
String msg=in.nextLine(); skt.receive(req);
byte[] b = msg.getBytes(); String msg=new String(req.getData());
DatagramPacket request=new System.out.println("Client:" +msg);
DatagramPacket(b,b.length,host,s_port); System.out.println("Server");
skt.send(request); String m=in.nextLine();
byte buffer[]=new byte[1024]; byte[] sendmsg = m.getBytes();
DatagramPacket reply=new DatagramPacket reply=new
DatagramPacket(buffer,buffer.length); DatagramPacket(sendmsg,sendmsg.length,req.get
skt.receive(reply); Address(),req.getPort());
System.out.println("Server:"+new skt.send(reply);
String(reply.getData())); }
} }
} catch(Exception e)
catch(Exception e) {
{ e.printStackTrace();
e.printStackTrace(); }
} }
} }
}
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random; System.out.println("Encrypting String: " +
public class rsa teststring);
{ System.out.println("String in Bytes: "
private BigInteger p; + bytesToString(teststring.getBytes()));
private BigInteger q; // encrypt
9
private BigInteger N; byte[] encrypted =
private BigInteger phi; rsa.encrypt(teststring.getBytes());
private BigInteger e; // decrypt
private BigInteger d; byte[] decrypted = rsa.decrypt(encrypted);
private int bitlength = 1024; System.out.println("Decrypting Bytes: " +
private Random r; bytesToString(decrypted));
public rsa() System.out.println("Decrypted String: " + new
{ String(decrypted));
r = new Random(); }
p = BigInteger.probablePrime(bitlength, r); private static String bytesToString(byte[]
q = BigInteger.probablePrime(bitlength, r); encrypted)
N = p.multiply(q); {
phi = String test = "";
p.subtract(BigInteger.ONE).multiply(q.subtract( for (byte b : encrypted)
BigInteger.ONE)); {
e = BigInteger.probablePrime(bitlength / 2, r); test += Byte.toString(b);
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 }
&& e.compareTo(phi) < 0) return test;
{ }
e.add(BigInteger.ONE); // Encrypt message
} public byte[] encrypt(byte[] message)
d = e.modInverse(phi); {
} return (new BigInteger(message)).modPow(e,
public rsa(BigInteger e, BigInteger d, BigInteger N).toByteArray();
N) }
{ // Decrypt message
this.e = e; public byte[] decrypt(byte[] message)
this.d = d; {
this.N = N; return (new BigInteger(message)).modPow(d,
} N).toByteArray();
@SuppressWarnings("deprecation") }
public static void main(String[] args) throws }
IOException
{
RSA rsa = new RSA();
DataInputStream in = new
DataInputStream(System.in);
String teststring;
System.out.println("Enter the plain text:");
teststring = in.readLine();
import java.util.Scanner;
import java.util.*;
public class Leaky {
public static int bucketsize=512;
public static void bktinput(int pktsize, int
op)throws Interrupted Exception
{
if(pktsize > bucketsize)
System.out.println("\n packet
discarded");
else
{
while(pktsize>op)
{
Thread.sleep(100);
10
System.out.println("\n bytes outputted"+
op);
pktsize = pktsize - op;
}
if(pktsize > 0)
System.out.println("\n last"+pktsize+"
bytes sent");
System.out.println("\n bucket outputted
scccessful\n");
}
}
public static void main(String[]
args)throws InterruptedException {
// TODO Auto-generated method stub
int i, op, pktsize;
Scanner sc = new Scanner(System.in);
Random r=new Random();
System.out.println("enter output rate:
\n");
op=sc.nextInt();
for(i=0;i<5;i++)
{
Thread.sleep(100);
pktsize = Math.abs(r.nextInt())%1000;
System.out.println("\n pktno:"+i+"\t
pktsize:"+ pktsize);
bktinput(pktsize, op);
}
sc.close();
}
}