Sunday, March 3, 2019

CN 6 - PERFORMANCE OF CDMA ON NS2

6. Implement and study the performance of CDMA on NS2/NS3 (Using stack called Call net) or equivalent environment.

cn6.tcl - PROGRAM


set opt(title) zero ;
set opt(stop) 100 ;
set opt(ecn) 0 ;
set opt(type) umts ;
set opt(secondDelay) 55 ;
set opt(minth) 30 ;
set opt(maxth) 0 ;
set opt(adaptive) 1 ;
0 for plain RED
set opt(flows) 0 ;
set opt(window) 30 ;
set opt(web) 2 ;
set opt(quiet) 0 ;
set opt(wrap) 100 ;
set opt(srcTrace) is ;
set opt(dstTrace) bs2 ;
set opt(umtsbuf) 10 ;
set bwDL(umts) 384000
set bwUL(umts) 64000
set propDL(umts) .150
set propUL(umts) .150
set buf(umts) 20

set ns [new Simulator]
set tf [open out.tr w]
$ns trace-all $tf
set nodes(is) [$ns node]
set nodes(ms) [$ns node]
set nodes(bs1) [$ns node]
set nodes(bs2) [$ns node]
set nodes(lp) [$ns node]

proc cell_topo {} {
global ns nodes
$ns duplex-link $nodes(lp) $nodes(bs1) 3Mbps 10ms DropTail
$ns duplex-link $nodes(bs1) $nodes(ms) 1 1 RED
$ns duplex-link $nodes(ms) $nodes(bs2) 1 1 RED
$ns duplex-link $nodes(bs2) $nodes(is) 3Mbps 50ms DropTail
puts "Cell Topology"}
proc set_link_params {t} {
global ns nodes bwUL bwDL propUL propDL buf
$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) simplex
$ns bandwidth $nodes(ms) $nodes(bs1) $bwUL($t)
simplex $ns delay $nodes(bs1) $nodes(ms) $propDL($t)
simplex $ns delay $nodes(ms) $nodes(bs1) $propDL($t)
simplex $ns queue-limit $nodes(bs1) $nodes(ms) $buf($t)
$ns queue-limit $nodes(ms) $nodes(bs1) $buf($t)
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t)
simplex $ns bandwidth $nodes(ms) $nodes(bs2) $bwUL($t)
simplex $ns delay $nodes(bs2) $nodes(ms) $propDL($t)
simplex $ns delay $nodes(ms) $nodes(bs2) $propDL($t)
simplex $ns queue-limit $nodes(bs2) $nodes(ms) $buf($t)
$ns queue-limit $nodes(ms) $nodes(bs2) $buf($t)
}

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
source web.tcl
switch $opt(type) {
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]
if {$opt(flows) == 0} {
set tcp1 [$ns create-connection TCP/Sack1 $nodes(is) TCPSink/Sack1 $nodes(lp) 0]
set ftp1 [[set tcp1] attach-app FTP]
$ns at 0.8 "[set ftp1] start" } if {$opt(flows) > 0} {
set tcp1 [$ns create-connection TCP/Sack1 $nodes(is) TCPSink/Sack1 $nodes(lp) 0]
set ftp1 [[set tcp1] attach-app FTP] $tcp1
set window_ 100
$ns at 0.0 "[set ftp1] start"
$ns at 3.5 "[set ftp1] stop"
set tcp2 [$ns create-connection TCP/Sack1 $nodes(is) TCPSink/Sack1 $nodes(lp) 0]
set ftp2 [[set tcp2] attach-app FTP]
$tcp2 set window_ 3
$ns at 1.0 "[set ftp2] start"
$ns at 8.0 "[set ftp2] stop" }
proc stop {} { global nodes opt nf
set wrap $opt(wrap)
set sid [$nodes($opt(srcTrace)) id]
set did [$nodes($opt(dstTrace)) id]
if {$opt(srcTrace) == "is"} { set a "-a out.tr" } else { set a "out.tr" }
set GETRC "../../../bin/getrc"
set RAW2XG "../../../bin/raw2xg"
exec $GETRC -s $sid -d $did -f 0 out.tr | \
$RAW2XG -s 0.01 -m $wrap -r > plot.xgr
exec $GETRC -s $did -d $sid -f 0 out.tr | \
$RAW2XG -a -s 0.01 -m $wrap >> plot.xgr
exec $GETRC -s $sid -d $did -f 1 out.tr | \
$RAW2XG -s 0.01 -m $wrap -r >> plot.xgr
exec $GETRC -s $did -d $sid -f 1 out.tr | \
$RAW2XG -s 0.01 -m $wrap -a >> plot.xgr
exec ./xg2gp.awk plot.xgr if {!$opt(quiet)} {
exec xgraph -bb -tk -nl -m -x time -y packets plot.xgr &
}
exit 0
}
$ns at $opt(stop) "stop"
$ns run

OUTPUT :
( click on image to zoom )


CN 7 - CRC

7. Write a program for error detecting code using CRC-CCITT (16- bits).

Crc.java - PROGRAM

import java.util.Scanner;
public class Crc {

static int n,m,msb,i,j,k,g[],d[],z[],r[];
public static void main(String[] args) 
{
Scanner s=new Scanner(System.in);
System.out.println("Enter no. of databits:");
n=s.nextInt();
System.out.println("Enter no. of generator bits:");
m=s.nextInt();
d=new int[m+n];
g=new int[n];
System.out.println("Enter databits:");
for(i=0;i<n;i++)
d[i]=s.nextInt();
System.out.println("Enter generator bits:");
for(i=0;i<m;i++)
g[i]=s.nextInt();
crcEval();
System.out.println("\nThe code data is:");
for(i=0;i<n+m-1;i++)
System.out.println(d[i]);
crcEval();
boolean t=true;
for(i=n;i<n+m-1;i++) {
if(r[i]==1)
t=false;
}
if(t)
System.out.println("\nNo error");
else
System.out.println("Error");
}
private static void crcEval() 
{
r=new int[m+n];
for(i=0;i<m;i++)
r[i]=d[i];
z=new int[m];
for(i=0;i<n;i++) {
k=0;
msb=r[i];
for(j=i;j<m+i;j++) {
if(msb==0)
r[j]=r[j]^z[k];
else
r[j]=r[j]^g[k];
k++;
}
r[m+i]=d[m+i];
}
//System.out.println("The code bits added are:");
for(i=n;i<n+m-1;i++) {
d[i]=r[i];
//System.out.print(d[i]+" ");
}
}
}

OUTPUT :
( click on image to zoom )


CN 8 - BELLMAN FORD ALGORITHM

8. Write a program to find the shortest path between vertices using bellman-ford algorithm.

BellmanFord.java - PROGRAM

import java.util.Scanner;

public class BellmanFord 
{
int d[],noofvertices;
public BellmanFord(int noofvertices) 
{
this.noofvertices = noofvertices;
d = new int[noofvertices + 1];
}

public static void main(String[] args) 
{
int noofvertices,source;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of vertices:");
noofvertices = s.nextInt();

int a[][] = new int[noofvertices + 1][noofvertices + 1];
System.out.println("Enter the adjacency matrix:");
for(int sn = 1; sn <= noofvertices; sn++) {
for(int dn = 1; dn <= noofvertices; dn++) {
a[sn][dn] = s.nextInt();
if(sn == dn) {
a[sn][dn] = 0;
continue;
}
if(a[sn][dn] == 0)
a[sn][dn] = 999;
}
}

System.out.println("Enter src vertex:");
source = s.nextInt();
BellmanFord b = new BellmanFord(noofvertices);
b.bellmanFordeval(source,a);
}

private void bellmanFordeval(int source, int[][] a) 
{
for(int node = 1; node <= noofvertices; node++)
d[node] = 999;
d[source] = 0;

for(int node = 1; node <= noofvertices-1; node++) 
for(int sn=1; sn <= noofvertices; sn++)
for(int dn = 1;dn <= noofvertices; dn++)
if(a[sn][dn] != 999)
if(d[dn] > d[sn]+a[sn][dn])
d[dn] = d[sn]+a[sn][dn];

for(int sn = 1; sn <= noofvertices; sn++)
for(int dn = 1; dn <= noofvertices; dn++)
if(d[dn] > d[sn]+a[sn][dn])
System.out.println("-ve cycle");

for(int vertex = 1; vertex <= noofvertices; vertex++)
System.out.println( source+" to "+ vertex +" is "+ d[vertex] );
}

}

OUTPUT :
( click on image to zoom )


CN 9 - CLIENT & SERVER PROGRAM USING TCP

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.

Server,java - PROGRAM

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Server 
{
public static void main(String[] args) throws Exception 
{
System.out.println("Server ready for communication");
ServerSocket serverSocket=new ServerSocket(4000);
Socket socket=serverSocket.accept();

InputStream iStream=socket.getInputStream();
Scanner sin=new Scanner(iStream);
String fname=sin.next();

OutputStream oStream=socket.getOutputStream();
PrintWriter pWriter=new PrintWriter(oStream,true);
File file=new File(fname);
Scanner fin=new Scanner(file);
while(fin.hasNext())
pWriter.println(fin.next());
System.out.println("Connection is successful and file contents are displayed in the client window");
}

}

Client.java - PROGRAM

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client 
{
public static void main(String[] args) throws Exception 
{
Scanner s=new Scanner(System.in);
System.out.println("Enter file name:");
String fname=s.next();
Socket socket=new Socket("127.0.0.1",4000);

OutputStream osStream=socket.getOutputStream();
PrintWriter pwWriter=new PrintWriter(osStream,true);
pwWriter.println(fname);

InputStream inStream=socket.getInputStream();
Scanner sin=new Scanner(inStream);
while(sin.hasNext())
System.out.println(sin.next());
}

}

OUTPUT :
( click on image to zoom )


CN 10 - CLIENT & SERVER PROGRAM USING UDP

10. Write a program on datagram socket for client/server to display the messages on client side, typed at the server side.

UDPServer.java - PROGRAM 

import java.net.*;
import java.util.Scanner;

public class UDPServer {
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(System.in);
DatagramSocket socket=new DatagramSocket();
String msg=in.nextLine();

byte code[]=msg.getBytes();
InetAddress iAddress=InetAddress.getByName("127.0.0.1");
DatagramPacket request=new DatagramPacket(code,code.length,iAddress,4000);
socket.send(request);
}
}

UDPClient.java

import java.net.*;

public class UDPClient {
public static void main(String[] args) throws Exception{
DatagramSocket socket=new DatagramSocket(4000);
byte data[]=new byte[1000];

while(true) {
DatagramPacket request=new DatagramPacket(data, data.length);
socket.receive(request);
String str=new String(request.getData());
System.out.println(str);
}
}

}

OUTPUT :
( click on image to zoom )


CN 11 - RSA ALGORITHM

11. Write a program for simple RSA algorithm to encrypt and decrypt the data.

RSA.java - PROGRAM

import java.io.DataInputStream; 
import java.io.IOException; 
import java.math.BigInteger; 
import java.util.Random; 
public class RSA 

    private BigInteger d, e, p, q, N, phi; 
    private int bitlength = 1024; 
    private Random r; 
    
    public RSA() 
    { 
        r = new Random(); 
        p = BigInteger.probablePrime(bitlength, r); 
        q = BigInteger.probablePrime(bitlength, r); 
        N = p.multiply(q); 
        phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); 
        e = BigInteger.probablePrime(bitlength / 2, r); 
        while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0)          
            e.add(BigInteger.ONE);          
        d = e.modInverse(phi); 
    } 
    
    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(System.in); 
        String teststring; 
        System.out.println("Enter the plain text:"); 
        teststring = in.readLine(); 
        System.out.println("Encrypting String: " + teststring+" "); 
        System.out.println("String in Bytes:  " + bytesToString(teststring.getBytes())); 
         
        byte[] encrypted = rsa.encrypt(teststring.getBytes()); 
      
        byte[] decrypted = rsa.decrypt(encrypted); 
        System.out.println("Decrypting Bytes: " + bytesToString(decrypted)); 
        System.out.println("Decrypted String: " + new String(decrypted)); 
    } 

    private static String bytesToString(byte[] encrypted) 
    { 
        String test = ""; 
        for(byte b : encrypted) 
            test += Byte.toString(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(); 
    } 



OUTPUT :
( click on image to zoom )


CN 12 - LEAKY BUCKET ALGORITHM

12. Write a program for congestion control using leaky bucket algorithm.

Leaky.java - PROGRAM

import java.util.Scanner;

public class Leaky 
{
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
int bcktsize, iter, rate, line, total = 0;
int[] pkt = new int[25];

System.out.print("Enter the bucket size and output rate(both in MB):");
bcktsize = sc.nextInt();
rate = sc.nextInt();

System.out.println("Enter the number of input lines");
line = sc.nextInt();

System.out.println("Enter input packet rate of " + line + " lines");
for(int i = 0; i < line; i++)
pkt[i] = sc.nextInt();

System.out.println("Enter the number of iterations");
iter = sc.nextInt();

for(int i = 0; i < iter; i++) 
{
System.out.println("\nIteration " + (i + 1));
for(int j = 0; j < line; j++) 
{
total += pkt[j];
if(total <= bcktsize)
System.out.println("\nInput from line "+(j+1)+" withrate "+pkt[j]+
" is added to the bucket\nCurrent bucket size(Mb) is " + total);
else 
{
total -= pkt[j];
System.out.println("\nInput from line " + (j + 1) +" withrate " + pkt[j] + 
" is thrown out of bucket\nCurrent bucket size(Mb)is " + total);
}
}
if(total <= rate) 
{
System.out.print("packet sent to outputline at rate " + total);
total = 0;
System.out.print("current bucket size is " + total);
}
else
{
total -= rate;
System.out.println("\npacket sent to output line at rate " + rate +
" \n Current bucket size(Mb)is " + total);
}
}
}

}

OUTPUT :
( click on image to zoom )


Thursday, January 3, 2019

DAA 01 A - TO CREATE N STUDENTS

1 A - Create a Java class called Student with the following details as variables within it. (i) USN (ii) Name (iii) Branch (iv) Phone Write a Java program to create n Student objects and print the USN, Name, Branch, and Phone of these objects with suitable headings.

class student 
{
String USN,NAME,BRANCH,PH;
student(String U,String N,String b,String p)

this.USN=U;
this.NAME=N;
this.BRANCH=b;
this.PH=p;
}

void display()
{
System.out.println("\nEnter the students detail");
System.out.println("USN="+this.USN);
System.out.println("Name="+this.NAME);
System.out.println("Branch="+this.BRANCH);
System.out.println("Phone No="+this.PH);
}
}
public class lab1a 
{
     public static void main(String[]args)
     {
      student ob1=new student("CS112","xxx","CSE","119");
      student ob2=new student("CS132","yyy","CSE","118");
      student ob3=new student("CS098","zzz","CSE","117");
      ob1.display();
      ob2.display();
      ob3.display();
     }

}


OUTPUT : (click on image to zoom)