0% found this document useful (0 votes)
166 views17 pages

Network Security Lab

The document contains code for implementing Remote Method Invocation (RMI) in Java. It defines a remote interface with an add method, a remote implementation class that extends the interface, a server class that binds the implementation object, and a client class that looks up the remote object and calls the add method. The client passes two numbers, the server adds them and returns the result, which is printed by the client.

Uploaded by

wertdr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views17 pages

Network Security Lab

The document contains code for implementing Remote Method Invocation (RMI) in Java. It defines a remote interface with an add method, a remote implementation class that extends the interface, a server class that binds the implementation object, and a client class that looks up the remote object and calls the add method. The client passes two numbers, the server adds them and returns the result, which is printed by the client.

Uploaded by

wertdr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

Ex. No.

: Date :

Name : Roll No :

IMPLEMENTATION OF TCP CODING: TCP SERVER: import java.net.*; import java.io.*; public class tcpserver { public static void main(String s[])throws I !"ception { ## Initialising the ServerSoc$et ServerSoc$et so$ % new ServerSoc$et(&'()); ## *ives the Server +etails ,achine name- .ort number S/stem.out.println(0Server Started 102so$); ## ma$es a soc$et connection to particular client a3ter ## which two wa/ communication ta$e place Soc$et so % so$.accept(); S/stem.out.println(04lient 4onnected 102 so); InputStream in % so.getInputStream(); utputStream os % so.get utputStream(); .rint5riter pr % new .rint5riter(os); 6u33ered7eader br % new 6u33ered7eader(new InputStream7eader(in)); 6u33ered7eader br' % new 6u33ered7eader(new InputStream7eader(S/stem.in)); while(true) { S/stem.out.println(0,sg 3rm client1 02 br.read8ine()); pr.println(br'.read8ine()); pr.3lush(); 9 9 9

TCP CLIENT: import java.net.*; import java.io.*; public class tcpclient { public static void main(String s[])throws I !"ception { Soc$et so$ % new Soc$et(0localhost0-&'()); InputStream in % so$.getInputStream(); utputStream ou % so$.get utputStream(); .rint5riter pr % new .rint5riter(ou); 6u33ered7eader br' % new 6u33ered7eader(new InputStream7eader(in)); 6u33ered7eader br % new 6u33ered7eader(new InputStream7eader(S/stem.in)); while(true) { pr.println(br.read8ine()); pr.3lush(); S/stem.out.println(0,sg 3rm server1 02br'.read8ine()); 9 9 9 OUTPUT: TCP SERVER:

TCP CLIENT:

Ex. No. : Date :

Name : Roll No :

IMPLEMENTATION OF UDP

CODING: UDP SERVER: import java.net.*; import java.io.*; import java.lang.*; public class udpserver { public static +atagramSoc$et ds; public static b/te bu33er[] %new b/te[':(;]; public static int clientport%<=:-serverport%<(:; public static void main(String args[])throws !"ception { String str; Inet>ddress ia %Inet>ddress.get8ocal?ost(); 6u33ered7eader br%new 6u33ered7eader(new InputStream7eader(S/stem.in)); ds%new +atagramSoc$et(serverport); while(true) { +atagram.ac$et dp%new +atagram.ac$et(bu33er-bu33er.length-ia-clientport); ds.receive(dp); String s%new String(dp.get+ata()-:-dp.get8ength()); S/stem.out.println(s); str%br.read8ine(); i3(str%%null @@ str.eAuals(0end0)) brea$; bu33er%str.get6/tes(); ds.send(new +atagram.ac$et(bu33er-bu33er.length-ia-clientport)); 9 9

9 UDP CLIENT: import java.net.*; import java.io.*; import java.lang.*; public class udpclient { public static +atagramSoc$et ds; public static b/te bu33er[] %new b/te[':(;]; public static int clientport%<=:-serverport%<(:; public static void main(String args[])throws I !"ception-Soc$et!"ception { ds%new +atagramSoc$et(clientport); Inet>ddress ia %Inet>ddress.get8ocal?ost(); 6u33ered7eader br%new 6u33ered7eader(new InputStream7eader(S/stem.in)); String str; while(true) { str%br.read8ine(); i3(str%%null @@ str.eAuals(0end0)) brea$; bu33er%str.get6/tes(); ds.send(new +atagram.ac$et(bu33er-bu33er.length-ia-serverport)); +atagram.ac$et dp%new +atagram.ac$et(bu33er-bu33er.length-ia-serverport); ds.receive(dp); String s%new String(dp.get+ata()-:-dp.get8ength()); S/stem.out.println(s); 9 9 9

OUTPUT: UDP SERVER:

UDP CLIENT:

Ex. No. : Date :

Name : Roll No :

IMPLEMENTATION OF PING CODING: PING SERVER: import java.io.*; import java.net.*; public class pingserver { public static void main(String a[]) { String line'-line(; int i; ServerSoc$et es; +ataInputStream di; .rintStream ps; Soc$et csoc; es%null; csoc%null; tr/ { es%new ServerSoc$et(====); 9 catch(!"ception e) { S/stem.out.println(e); 9 S/stem.out.println(0ping Server0); tr/ { csoc%es.accept(); di%new +ataInputStream(csoc.getInputStream()); ps%new .rintStream(csoc.get utputStream()); 3or(i%:;iB;;i22) { line'%di.read8ine();

S/stem.out.println(0pinged b/ client0); ps.println(line'20repl/ 3rom host1b/tes%&BtimeB'ms CC%'()0); 9 di.close(); ps.close(); 9 catch (!"ception e) { S/stem.out.println(e); 9 9 9 PING CLIENT: import java.io.*; import java.net.*; public class pingclient { public static void main(String args[]) { .rint5riter out%null; int i-j-$; 6u33ered7eader networ$In%null; tr/ { S/stem.out.println(0!nter the I. >ddress0); +ataInputStream in%new +ataInputStream(S/stem.in); String ip%in.read8ine(); Soc$et thesoc$et%new Soc$et(ip-====); networ$In%new 6u33ered7eader(new InputStream7eader(S/stem.in)); out%new .rint5riter(thesoc$et.get utputStream()); S/stem.out.println(0Dnpinging02ip20with &( b/tes o3 dataDn0); 3or(i%:;iB;;i22) { out.println(ip); out.3lush(); String inp%networ$In.read8ine(); i3(inpE%null) { 3or(j%:;jB'::::;j22) {

3or($%:;$BF::::;$22) {9 9 S/stem.out.println(0repl/ 3rom02inp); 9 else { 3or(i%:;iB;;i22) { 3or(j%:;jB'::::;j22) { 3or($%:;$BF::::;$22) {9 S/stem.out.println(0DnreAuest time out0); 9 9 9 9 9 catch(I !"ception e) { 3or(i%:;iB;;i22) { 3or(j%:;jB':::;j22) { 3or($%:;$BF:::;$22) {9 9 S/stem.out.println(0DnreAuest time out0); 9 9 tr/ { i3(networ$InE%null) networ$In.close(); i3(outE%null) out.close(); 9 catch(!"ception e) { S/stem.out.println(0DnreAuest time out0); 9 9

9 OUTPUT: PING SERVER:

PING CLIENT:

Ex. No. : Date :

Name : Roll No :

IMPLEMENTATION OF ADDRESS RESOLUTION PROTOCOL(ARP) CODING: import java.net.*; import java.io.*; import java.lang. bject; ## Should be a capital import java.util.*; class arp { public static void main(String args[]) { tr/ { .rocess p%7untime.get7untime().e"ec(0arp Ga0); 6u33ered7eader br%new 6u33ered7eader(new InputStream7eader(p.getInputStream())); String str-str'%00-st'-st(; while((str%br.read8ine())E%null) str'2%str20Dn0; StringCo$eniHer st%new StringCo$eniHer(str'-0Dn0); 6u33ered7eader br'%new 6u33ered7eader(new InputStream7eader(S/stem.in)); S/stem.out.println(0!nter the I. >++7!SS0); st(%br'.read8ine(); while(st.has,oreCo$ens()) { st'%st.ne"tCo$en(); i3(st'.inde" 3(st()E%G') S/stem.out.println(st'); 9 9 catch(!"ception !) { !.printStac$Crace(); 9 9 9

OUTPUT:

Ex. No. : Date :

Name : Roll No :

IMPLEMENTATION OF REVERSE ADDRESS RESOLUTION PROTOCOL(RARP) CODING: import java.net.*; import java.io.*; import java.lang. bject; ## Should be a capital import java.util.*; class rarp { public static void main(String args[]) { tr/ { .rocess p%7untime.get7untime().e"ec(0arp Ga0); 6u33ered7eader br%new 6u33ered7eader(new InputStream7eader(p.getInputStream())); String str-str'%00-st'-st(; while((str%br.read8ine())E%null) str'2%str20Dn0; StringCo$eniHer st%new StringCo$eniHer(str'-0Dn0); 6u33ered7eader br'%new 6u33ered7eader(new InputStream7eader(S/stem.in)); S/stem.out.println(0!nter the ph/sical ;)Gbit >++7!SS0); st(%br'.read8ine(); while(st.has,oreCo$ens()) { st'%st.ne"tCo$en(); i3(st'.inde" 3(st()E%G') S/stem.out.println(st'); 9 9 catch(!"ception !) { !.printStac$Crace(); 9 9 9

OUTPUT:

Ex. No. : Date :

Name : Roll No :

IMPLEMENTATION OF REMOTE METHOD INVOCATION (RMI) CODING: REMOTE INTERFACE: import java.rmi.*; public inter3ace >ddServerInt3 e"tends 7emote { double add(double d'- double d() throws 7emote!"ception; 9 REMOTE IMPLEMENTATION: import java.rmi.*; import java.rmi.server.*; public class >ddServerImpl e"tends Inicast7emote bject implements >ddServerInt3 { public >ddServerImpl() throws 7emote!"ception {9 public double add(double d'-double d() throws 7emote!"ception { return d'2d(; 9 9 SERVER: import java.net.*; import java.rmi.*; public class >ddServer { public static void main(String s[]) throws !"ception { >ddServerImpl addServerImpl % new >ddServerImpl(); Jaming.rebind(0>ddServer0-addServerImpl); 9 9

CLIENT: import java.rmi.*; public class >dd4lient { public static void main(String s[]) throws !"ception { String addServerI78 % 0>ddServer0; >ddServerInt3 addServerInt3 %(>ddServerInt3)Jaming.loo$up(addServerI78); S/stem.out.println(0Kirst no is 102 s[:]); double d'%+ouble.value 3(s[:]).doubleLalue(); S/stem.out.println(0Second no is102 s[']); double d(%+ouble.value 3(s[']).doubleLalue(); S/stem.out.println(0Sum % 0 2 addServerInt3.add(d'-d()); 9 9

OUTPUT:

You might also like