Sie sind auf Seite 1von 15

#LAB1

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
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]

$n0 label "TCP Source"


$n1 label "UDP Source"
$n2 label "Sink"

set color1 red


set color2 yellow

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


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

set udp0 [new Agent/UDP]


$ns attach-agent $n1 $udp0

set tcp0 [new Agent/TCP]


$ns attach-agent $n1 $tcp0

set cbr0 [new Application/Traffic/CBR]


$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.001

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0

set null0 [new Agent / null]


$ns attach-agent $n2 $null0
$ns connect $udp0 $null0

$udp0 set class_ 1


$tcp0 set class_ 2

set sink [new Agent/TCPSink]


$ns attach-agent $n2 $sink
$ftp0 set maxPkts_ 1000
$ns connect $tcp0 $sink

$ns at o.1 "$cbr0 start"


$ns at 1.4 "$ftp0 start"
$ns at 2.2 "$ftp0 stop"
$ns at 3.0 "$cbr0 stop"
$ns at 4.0 "finish"
$ns "run"
#LAB2
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 2Mb 10ms DropTail


$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 40ms DropTail
$ns duplex-link $n3 $n4 2Mb 10ms DropTail
$ns duplex-link $n3 $n5 2Mb 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_ 3
$ping5 set class_ 4
$ns at 0.01 "sendPingPacket"
$ns at 10.0 "finish"
$ns run
#LAB3
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 "TittleText: Congestion Window Plot for TCP1"


puts $outFile1 "XUnitText: SimulationTime(Secs)"
puts $outFile1 "YUnitText: CongestionWindowSize"
puts $outFile2 "TittleText: 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
#LAB4 AWK
set ns [new Simulator]
set tr [open lab4.tr w]
$ns trace-all $tr BEGIN{
set topo [new Topography]
$topo load_flatgrid 1000 1000 count1=0
set nam [open lab4.nam w]
$ns namtrace-all-wireless $nam 1000 1000
$ns node-config -adhocRouting DSDV \ count2=0

-llType LL \ pack1=0
-macType Mac/802_11 \
-ifqType Queue/DropTail \
pack2=0
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \ time1=0
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \ time2=0
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \ }
create-god 3
{
set n0 [$ns node]
set n1 [$ns node] if ($1=="r"&&$3=="_1_"&&$4=="AGT")
set n2 [$ns node]
$n0 label "tcp0"
$n1 label "sink1/tcp1" {
$n2 label "sink2"
count1++
$n0 set X_ 50
$n0 set Y_ 50 pack1=pack1+$8
$n0 set Z_ 0

$n1 set X_ 100 time1=$2


$n1 set Y_ 100
$n1 set Z_ 0 }
$n2 set X_ 600
$n2 set Y_ 600
$n2 set Z_ 0
if ($1=="r"&&$3=="_1_"&&$4=="AGT")
$ns at 0.1 "$n0 setdest 50 50 15"
$ns at 0.1 "$n1 setdest 100 100 25" {
$ns at 0.1 "$n2 setdest 600 600 25"

set tcp0 [new Agent/TCP] count2++


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP] pack2=pack2+$8
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink] time1=$2
$ns attach-agent $n1 $sink1
$ns connect $tcp0 $sink1
set tcp1 [new Agent/TCP] }
$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP] }
$ftp1 attach-agent $tcp1
set ftp1 [new Application/FTP] END{
$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2 printf("The Throughput from n0 to n1: %f Mbps \n",
$ns connect $tcp1 $sink2
((count1*pack1*8) / (time1*1000000)));
$ns at 5 "$ftp0 start"
$ns at 5 "$ftp1 start"
$ns at 100 "$n1 setdest 550 550 15"
$ns at 190 "$n1 setdest 70 70 15"
proc finish { } { printf("The Throughput from n1 to n2: %f Mbps \n",
global ns nam tr
$ns flush-trace ((count2*pack2*8) / (time2*1000000)));
exec nam lab4.nam &
close $tr
exit 0 }
}
$ns at 250 "finish"
$ns run
#LAB5
# General Parameters
set stop 100 ; # Stop time.
# Topology
set type gsm ; #type of link:
# AQM parameters
set minth 30 ;
set maxth 0 ;
set adaptive 1 ; # 1 for Adaptive RED, 0 for plain RED
# Traffic generation.
set flows 0 ; # number of long-lived TCP flows
set window 30 ; # window for long-lived traffic
set web 2 ; # number of web sessions
# Plotting statics.
set opt(wrap) 100 ; # wrap plots?
set opt(srcTrace) is ; # where to plot traffic
set opt(dstTrace) bs2 ; # where to plot traffic
#default downlink bandwidth in bps
set bwDL(gsm) 9600
#default uplink bandwidth in bps

set bwUL(gsm) 9600


#default downlink propagation delay in seconds
set propDL(gsm) .500
#default uplink propagation delay in seconds
set propUL(gsm) .500
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 10nodes(ms) 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 50nodes(ms) DropTail
puts " GSM Cell Topology"
}

proc set_link_para {t} {


global ns nodes bwUL bwDL propUL propDL buf
$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) duplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) duplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) duplex
$ns delay $nodes(bs2) $nodes(ms) $propDL($t) duplex
$ns queue-limit $nodes(bs1) $nodes(ms) 10
$ns queue-limit $nodes(bs2) $nodes(ms) 10
}

# RED and TCP parameters


Queue/RED set adaptive_ $adaptive
Queue/RED set thresh_ $minth
Queue/RED set maxthresh_ $maxth
Agent/TCP set window_ $window

source web.tcl

#Create topology
switch $type {
gsm -
gprs -
umts {cell_topo}
}
set_link_para $type

$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]


$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]
# Set up forward TCP connection

if {$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 {$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]
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 xgraph -x time -y packets plot.xgr &
exit 0
}
$ns at $stop "stop"
$ns run
#LAB6
# General Parameters
set stop 100 ;# Stop time.
# Topology
set type cdma ;#type of link:
# AQM parameters
set minth 30 ;
set maxth 0 ;
set adaptive 1 ;# 1 for Adaptive RED, 0 for plain RED
# Traffic generation.
set flows 0 ;# number of long-lived TCP flows
set window 30 ;# window for long-lived traffic
set web 2 ;# number of web sessions
# Plotting statics.
set opt(wrap) 100 ;# wrap plots?
set opt(srcTrace) is ;# where to plot traffic
set opt(dstTrace) bs2 ;# where to plot traffic

#default downlink bandwidth in bps

set bwDL(cdma) 384000


#default uplink bandwidth in bps
set bwUL(cdma) 64000
#default downlink propagation delay in seconds
set propDL(cdma) .150
#default uplink propagation delay in seconds
set propUL(cdma) .150

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 10nodes(ms) 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 50nodes(ms) DropTail
puts " cdma Cell Topology"
}
proc set_link_para {t} {
global ns nodes bwUL bwDL propUL propDL buf
$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) duplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) duplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) duplex
$ns delay $nodes(bs2) $nodes(ms) $propDL($t) duplex

$ns queue-limit $nodes(bs1) $nodes(ms) 20


$ns queue-limit $nodes(bs2) $nodes(ms) 20
}
# RED and TCP parameters
Queue/RED set adaptive_ $adaptive
Queue/RED set thresh_ $minth
Queue/RED set maxthresh_ $maxth
Agent/TCP set window_ $window

source web.tcl
#Create topology
switch $type {
cdma {cell_topo}
}
set_link_para $type
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]

# Set up forward TCP connection


if {$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 {$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]
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 xgraph -x time -y packets plot.xgr &
exit 0
}
$ns at $stop "stop"
$ns run
#LAB7_CRC
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];

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++)
{
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]);

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;
}
}

#LAB8_BELLMANFORD
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 edge 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 Adfjacency 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();
}
}

#LAB9_TCP

//Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Client {

public static void main(String args[])throws Exception


{
String filename;
System.out.println("Enter the File Name");
Scanner sc=new Scanner(System.in);
filename=sc.nextLine();
sc.close();
while(true)
{
ServerSocket ss=new ServerSocket(5000);
System.out.println("Waiting for the request");
Socket s=ss.accept();
System.out.println("Connected With "+s.getInetAddress().toString());
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
try
{
String str="";
str=din.readUTF();
System.out.println("SendGet...OK");
if(!str.equals("stop")) {
System.out.println("Sending File: "+filename);
dout.writeUTF(filename);
dout.flush();
File f=new File(filename);
FileInputStream fin=new FileInputStream(f);
long sz=(int) f.length();
byte b[]=new byte[1024];
int read;
dout.writeUTF(Long.toString(sz));
dout.flush();
System.out.println("Size: "+sz);
System.out.println("Buf size:"+ss.getReceiveBufferSize());
while((read = fin.read(b))!=-1) {
dout.write(b,0,read);
dout.flush();
}
fin.close();
System.out.println("..ok");
dout.flush();
}
dout.writeUTF("stop");;
System.out.println("Send Complete");
dout.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("An error Occurred");
}
din.close();
s.close();
ss.close();
}
}
}

//SERVER

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String args[])throws Exception
{
String address="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter Server Address");
address=sc.nextLine();

Socket s=new Socket(address,5000);


DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Send Get to start...");
String str="",filename="";
try
{
while(!str.equals("start"))
str=br.readLine();
dout.writeUTF(str);
dout.flush();
filename=din.readUTF();
System.out.println("Receiving File: "+filename);
filename="client"+filename;
System.out.println("Saving as file: "+filename);

long sz=Long.parseLong(din.readUTF());
System.out.println("File Size: "+(sz/(1024*1024))+" MB");
byte b[]=new byte[1024];
System.out.println("Recieving File...");
FileOutputStream fos=new FileOutputStream(new File(filename),true);
long bytesRead;
do
{
bytesRead=din.read(b,0,b.length);
fos.write(b,0,b.length);
}while(!(bytesRead<1024));
System.out.println("Completed");
fos.close();
dout.close();
s.close();
}
catch(EOFException e)
{
//do nothing
}

}
#LAB10_UDP
//CLIENT
import java.net.*;
public class UDPC
{
public static void main(String args[])
{
DatagramSocket skt;
try
{
skt = new DatagramSocket();
String msg=" text message";
byte[] b=msg.getBytes();
InetAddress host=InetAddress.getByName("127.0.0.1");
int serverSocket=6788;
DatagramPacket request=new
DatagramPacket(b,b.length,host,serverSocket);
skt.send(request);
byte[] buffer=new byte[1000];
DatagramPacket reply =new DatagramPacket(buffer,buffer.length);
skt.receive(reply);
System.out.println("client received:"+new
String(reply.getData()));
skt.close();

}
catch(Exception ex)
{
}
}}
//SERVER
import java.net.*;
public class UDPS {
public static void main(String[] args)
{
DatagramSocket skt=null;
try{
skt=new DatagramSocket(6788);
byte[] buffer=new byte[1000];
while(true)
{
DatagramPacket request =new
DatagramPacket(buffer,buffer.length);
skt.receive(request);
String msg="hello"+(new String(request.getData()));
byte[] sendMsg=(msg+"serverprocessed").getBytes();
DatagramPacket reply =new
DatagramPacket(sendMsg,sendMsg.length,request.getAddress(),request.getPort())
;
skt.send(reply);

}
}
catch(Exception ex)
{

}
}
}
#LAB11_RSA
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;

public class Rsa {

private BigInteger p,q,n,phi,e,d;


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) {


e = E;
d = D;
n = N;
}

@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
Rsa rsa = new Rsa();
DataInputStream in = new DataInputStream(System.in);
System.out.println("enter the plain text :");
String teststring = in.readLine();
System.out.println("encrypting string ......");

//encrypting
byte[] encrypted = rsa.encrypt(teststring.getBytes());
System.out.println("encrypted bytes : " + bytesToString(encrypted));
//decrypt
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[] decryptedBytes) {


String test = "";
for (byte b : decryptedBytes) {
test += Byte.toString(b);
}
return test;
}

private byte[] decrypt(byte[] encrypted) {

return (new BigInteger(encrypted)).modPow(d, n).toByteArray();

private byte[] encrypt(byte[] bytes) {

return (new BigInteger(bytes)).modPow(e, n).toByteArray();


} }
#LAB12_LEAKYBUCKET
import java.util.Scanner;

public class LeakyBucket {


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

System.out.println("Enter the bucket size : ");


int bucketSize = my.nextInt();

System.out.println("Enter the no of groups : ");


int noGroups = my.nextInt();

int noPackets[] = new int[noGroups];


int inBandwidth[] = new int[noGroups];

int outBandwidth, requiredBandwidth = 0 , totalPackets = 0;


for(int i = 0 ; i < noGroups ; i++)
{
System.out.println("Enter the no of packets for group " + (i+1));
noPackets[i] = my.nextInt();

System.out.println("Enter the input bandwidth for the group " + (i+1));


inBandwidth[i] = my.nextInt();

if( ( totalPackets+ noPackets[i] ) <= bucketSize)


{
totalPackets += noPackets[i];
}
else
{
do
{
System.out.println(" Bucket Overflow ");
System.out.println(" Enter value less than " + (bucketSize -
totalPackets));
noPackets[i] = my.nextInt();
}while( ( totalPackets + noPackets[i] ) > bucketSize);
totalPackets += noPackets[i];
}
requiredBandwidth += ( noPackets[i] * inBandwidth[i]);
}
System.out.println("\nThe total required bandwidth is " + requiredBandwidth);
System.out.println("Enter the output bandwidth ");

outBandwidth = my.nextInt();

int temp=requiredBandwidth;
int remainingPackets = totalPackets;
while((outBandwidth<=temp)&&(remainingPackets>0))
{
System.out.println("Data Sent \n" + (--remainingPackets) + " packets
remaining");
System.out.println("Remaining Bandwidth " + (temp -= outBandwidth));
if((outBandwidth>temp)&&(remainingPackets>0))
System.out.println(remainingPackets + " packet(s) discarded due to
insufficient bandwidth");
}
}
}

Das könnte Ihnen auch gefallen