Escolar Documentos
Profissional Documentos
Cultura Documentos
import java.io.*; import java.net.*; class TCPServerFile { public static void main(String args[])throws IOException { int p=0,c; String s,inp; ServerSocket ss=new ServerSocket(2500); Socket soc=ss.accept(); System.out.println("CLIENT AND SERVER CONNECTED!!!"); DataInputStream i=new DataInputStream(soc.getInputStream()); DataOutputStream o=new DataOutputStream(soc.getOutputStream()); File folder = new File("E:/"); File[] listOfFiles = folder.listFiles(); for (File listOfFile : listOfFiles) if (listOfFile.isFile()) {
p++; } o.write(p); o.flush(); for (File listOfFile : listOfFiles) if (listOfFile.isFile()) { o.writeUTF(listOfFile.getName()); o.flush(); } inp=i.readUTF(); FileInputStream fin = new FileInputStream("E:/"+inp); while((c=fin.read())!=-1) { o.write(c); o.flush(); } fin.close(); } }
import java.io.*; import java.net.*; import java.util.*; class TCPClientFile { public static void main(String args[])throws IOException { Scanner src=new Scanner(System.in); String inp,rec,list[]; int p=0,ch; Socket s=new Socket(InetAddress.getByName("localhost"),2500 ); System.out.println("CLIENT AND SERVER CONNECTED!!!"); DataInputStream i=new DataInputStream(s.getInputStream()); DataOutputStream o=new DataOutputStream(s.getOutputStream()); list=new String[10]; System.out.println("MENU"); ch=i.read(); for(p=0;p<ch;p++) { list[p]=i.readUTF(); System.out.println((p+1)+". "+list[p]); }
System.out.println("Enter your choice"); ch=src.nextInt(); o.writeUTF(list[(ch-1)]); o.flush(); FileOutputStream fout = new FileOutputStream("E:/Download/"+list[ch-1]); try { do { ch=i.read(); fout.write(ch); }while(ch!=-1); } catch(SocketException e) { System.out.println("Task Complete......"); } } }
import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String( receivePacket.getData()); System.out.println("RECEIVED: " + sentence); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData,
UDP CLIENT --------------------------------------------------------------------------------------------------------------------import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { System.out.println("Enter String"); BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("localhost"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } }
#Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red
#Open the NAM trace file set nf [open out.nam w] $ns namtrace-all $nf
#Define a 'finish' procedure proc finish {} { global ns nf $ns flush-trace #Close the NAM trace file close $nf #Execute NAM on the trace file
#Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
#Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
#Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right
#Monitor the queue for link (n2-n3). (for NAM) $ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1
#Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP
#Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n3 $null $ns connect $udp $null $udp set fid_ 2
#Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false
$ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop"
#Detach tcp and sink agents (not really necessary) $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish"
#Print CBR packet size and interval puts "CBR packet size = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]"
#Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red
#Open the Trace file set file1 [open out.tr w] $ns trace-all $file1
#Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2
$ns flush-trace close $file1 close $file2 exec nam out.nam & exit 0 }
# Next line should be commented out to have the static routing $ns rtproto DV
#Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node]
#Create links between the nodes $ns duplex-link $n0 $n1 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail $ns duplex-link $n2 $n3 0.3Mb 10ms DropTail $ns duplex-link $n1 $n4 0.3Mb 10ms DropTail $ns duplex-link $n3 $n5 0.5Mb 10ms DropTail $ns duplex-link $n4 $n5 0.5Mb 10ms DropTail
#Give node position (for NAM) $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n1 $n2 orient right $ns duplex-link-op $n2 $n3 orient up $ns duplex-link-op $n1 $n4 orient up-left $ns duplex-link-op $n3 $n5 orient left-up $ns duplex-link-op $n4 $n5 orient right-up
#Setup a TCP connection set tcp [new Agent/TCP/Newreno] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink/DelAck] $ns attach-agent $n5 $sink
#Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP
$ns rtmodel-at 1.0 down $n1 $n4 $ns rtmodel-at 4.5 up $n1 $n4
$ns multicast
$ns color 1 red # the nam colors for the prune packets $ns color 30 purple # the nam colors for the graft packets $ns color 31 green
# create multicast capable nodes; for {set i 1} {$i <= $nod} {incr i} {
#Create links between the nodes $ns duplex-link $n(1) $n(2) 0.3Mb 10ms DropTail $ns duplex-link $n(2) $n(3) 0.3Mb 10ms DropTail $ns duplex-link $n(2) $n(4) 0.5Mb 10ms DropTail $ns duplex-link $n(2) $n(5) 0.3Mb 10ms DropTail $ns duplex-link $n(3) $n(4) 0.3Mb 10ms DropTail $ns duplex-link $n(4) $n(5) 0.5Mb 10ms DropTail $ns duplex-link $n(4) $n(6) 0.5Mb 10ms DropTail $ns duplex-link $n(5) $n(6) 0.5Mb 10ms DropTail
# all nodes will contain multicast protocol agents; set mrthandle [$ns mrtproto $mproto]
set src1 [new Application/Traffic/CBR] $src1 attach-agent $udp1 $udp1 set dst_addr_ $group $udp1 set dst_port_ 0 $src1 set random_ false
set src2 [new Application/Traffic/CBR] $src2 attach-agent $udp2 $udp2 set dst_addr_ $group $udp2 set dst_port_ 1 $src2 set random_ false
# joining and leaving the group; $ns at 0.6 "$n(3) join-group $rcvr $group" $ns at 1.3 "$n(4) join-group $rcvr $group" $ns at 1.6 "$n(5) join-group $rcvr $group" $ns at 1.9 "$n(4) leave-group $rcvr $group" $ns at 2.3 "$n(6) join-group $rcvr $group" $ns at 3.5 "$n(3) leave-group $rcvr $group"
exit 0 }
$ns color 1 red # the nam colors for the prune packets $ns color 30 purple # the nam colors for the graft packets $ns color 31 green
# create multicast capable nodes; for {set i 1} {$i <= $nod} {incr i} { set n($i) [$ns node] }
#Create links between the nodes $ns duplex-link $n(1) $n(2) 0.3Mb 10ms DropTail $ns duplex-link $n(2) $n(3) 0.3Mb 10ms DropTail $ns duplex-link $n(2) $n(4) 0.5Mb 10ms DropTail $ns duplex-link $n(2) $n(5) 0.3Mb 10ms DropTail $ns duplex-link $n(3) $n(4) 0.3Mb 10ms DropTail $ns duplex-link $n(4) $n(5) 0.5Mb 10ms DropTail $ns duplex-link $n(4) $n(6) 0.5Mb 10ms DropTail $ns duplex-link $n(5) $n(6) 0.5Mb 10ms DropTail
# configure multicast protocol; set mproto CtrMcast # all nodes will contain multicast protocol agents; set mrthandle [$ns mrtproto $mproto]
# set RV and bootstrap points $mrthandle set_c_rp $n(5) # $mrthandle set_c_bsr $n(1):0 $n(3):1
$udp1 set dst_addr_ $group $udp1 set dst_port_ 0 $src1 set random_ false
set src2 [new Application/Traffic/CBR] $src2 attach-agent $udp2 $udp2 set dst_addr_ $group $udp2 set dst_port_ 1 $src2 set random_ false
# joining and leaving the group; $ns at 0.6 "$n(3) join-group $rcvr $group" $ns at 1.3 "$n(4) join-group $rcvr $group" $ns at 1.6 "$n(5) join-group $rcvr $group" $ns at 1.9 "$n(4) leave-group $rcvr $group" $ns at 2.3 "$n(6) join-group $rcvr $group"
proc finish {} { global ns $ns flush-trace exec nam out.nam & exit 0 }
#Define different colors for data flows (for NAM) $ns color 1 Blue
#Open the Trace files set file1 [open out.tr w] set winfile [open WinFile w] $ns trace-all $file1
#Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2
#Define a 'finish' procedure proc finish {} { global ns file1 file2 $ns flush-trace close $file1 close $file2 exec nam out.nam & exit 0 }
#Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node]
#Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns simplex-link $n2 $n3 0.3Mb 100ms DropTail $ns simplex-link $n3 $n2 0.3Mb 100ms DropTail $ns duplex-link $n3 $n4 0.5Mb 40ms DropTail $ns duplex-link $n3 $n5 0.5Mb 30ms DropTail
#Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up
$ns simplex-link-op $n2 $n3 orient right $ns simplex-link-op $n3 $n2 orient left $ns duplex-link-op $n3 $n4 orient right-up $ns duplex-link-op $n3 $n5 orient right-down
#Setup a TCP connection set tcp [new Agent/TCP/Newreno] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink/DelAck] $ns attach-agent $n4 $sink $ns connect $tcp $sink $tcp set fid_ 1 $tcp set window_ 8000 $tcp set packetSize_ 552
set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP
#Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n5 $null $ns connect $udp $null $udp set fid_ 2
#Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 0.01mb $cbr set random_ false
$ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 124.0 "$ftp stop" $ns at 124.5 "$cbr stop"
# next procedure gets two arguments: the name of the # tcp source node, will be called here "tcp", # and the name of output file.
proc plotWindow {tcpSource file} { global ns set time 0.1 set now [$ns now] set cwnd [$tcpSource set cwnd_] set wnd [$tcpSource set window_] puts $file "$now $cwnd" $ns at [expr $now+$time] "plotWindow $tcpSource $file" } $ns at 0.1 "plotWindow $tcp $winfile"
//Change this number according to the Network Interface which you wish to Sniff //I used wlan0 interface to sniff packets since my lappy is connected via wifi
public static void main(String[] args) { Main sniff=new Main(); sniff.printNetworkInterfaceList(); try { sniff.capture(); } catch (Exception ex) { } }
/** * Run this method first and it will list out the available network interfaces in your computer * All of the interfaces will be put into an array (interfaceList) */ public void printNetworkInterfaceList()
interfaceList = JpcapCaptor.getDeviceList();
for (int i = 0; i < interfaceList.length; i++) { System.out.println("Index :" + i + ", Network Device Name :" + interfaceList[i].name + ", Description : " + interfaceList[i].description); } }
public void capture() throws Exception { captor=JpcapCaptor.openDevice(interfaceLi st[interfaceNumber], 65535, true, 20000); //use javadoc to see what these parameters are captor.loopPacket(-1, new PacketPrinter()); //captures infinite number of packets by
public void receivePacket(Packet pkt) //loopPacket() calls this method by default hence we write the code to display packets here. { System.out.println("Packet :"+pkt.toString()) ; }