Download - Java Network programs
-
8/14/2019 Java Network programs
1/91
EX: NO: 1 IP ADDRESS OF LOCAL HOST
AIM:
To write a java program to find the IP address of the system.
ALGORITHM:
STEP1:Start
STEP:Declare a variable ip as a static InetAddress
STEP!:Using a function get ocal!ost "# to find the address of the s$stem.
STEP":Using the function get!ost%ame"# find the s$stem no and print it.
STEP#:Using the function get&$%ame"# find the name of an$ other s$stem.
STEP$:Displa$ the name
STEP%:Stop
-
8/14/2019 Java Network programs
2/91
PROGRAM:
import java.io.'(
import java.net.'(class Address
)public InetAddress ip(
public static void main"String arg*+#throws Un,nown!ost-ception)
InetAddress ip/InetAddress.getocal!ost"#(
S$stem.out.println"01n Ip address is 203ip#(String si/ip.get!ost%ame"#(
S$stem.out.println/"01n S$stem number is203si#(
InetAddress ip4/InetAddress.get&$%ame"0s$s 450#(S$stem.out.println"01n %ame of the other s$stem is203ip#(
6
6
-
8/14/2019 Java Network programs
3/91
O&TP&T:
-217cn1javac Address.java
-217cn1java Address
Ip address is2 s$s8894:8.4;
-
8/14/2019 Java Network programs
4/91
EX: NO: &DP' ONE (A) COMM&NICATION
AIM:
To write a java program to implement o*e +ay ,omm-*.,at.o* -s.*/ &DP0
ALGORITHM:
SENDER:'
STEP1:Import the net pac,age and other pac,ages.
STEP:String of argument is passed to the args *+.
STEP!:=reate a Datagram soc,et and bind it to the special port specified.
STEP":>et the buffer si?e and I@ Address of local host and communication with
the client.
STEP#:Send the Datagram pac,et to the destination address of given port.
STEP$:Store the input in the buffer at the specific location.
RECEIER:'
STEP1:Import the net pac,age and other pac,ages.
STEP:String of argument is passed to the args *+.
STEP!:=reate a Datagram soc,et and bind it to the special port specified.
STEP":=reate pac,et for receving arra$ of b$tes with maimum b$tes.
STEP#:Specif$ the common port for connecting datagram soc,et.
-
8/14/2019 Java Network programs
5/91
PROGRAM:
SENDER:'
import java.io.'(
import java.net.'(
class Sender)
DatagramSoc,et ds(
Datagram@ac,et dp(b$te buff*+/new b$te*458+(
String strBstr4(
boolean i/true(public void send"# throws IC-ception
)
while"i#
)ds/new DatagramSoc,et"#(
DataInputStream in/new DataInputStream"S$stem.in#(
S$stem.out.println"0-nter the message0#(str/in.readine"#(
buff/str.get&$tes"#(
dp/new Datagram@ac,et"buffBbuff.lengthBInetAddress.getocal!ost"#B
-
8/14/2019 Java Network programs
6/91
RECEIER:'
import java.io.'(
import java.net.'(class Geceiver
)DatagramSoc,et ds(
Datagram@ac,et dp(
b$te buff*+/new b$te*458H+(
String str(public void receiver"# throws IC-ception
)
ds/new DatagramSoc,et"
-
8/14/2019 Java Network programs
7/91
O&TP&T:
SENDER:'
-21cn7javac Sender.java
-21cn7java Sender
-nter the message2 hi
Do $ou want to continue Ees or %o
%o
RECEIER:
-21cn7javac Geceiver.java
-21cn7java Geceiver
essage received2 hi
Ip Address2 94:8.4;
-
8/14/2019 Java Network programs
8/91
EX0NO: ! TCP' ONE (A) COMM&NICATIONS
AIM:
To write a java program to implement o*e +ay ,omm-*.,at.o* -s.*/ TCP0
ALGORITHM:
SERER:'
STEP 1:Start the program.
STEP :Import net pac,age and other pac,ages.
STEP !:Declare object for ServerSoc,etB Soc,et and @rintStream to transfer data.
STEP ":Using @rintStream transfer the data in CutputStream via a port.
STEP #:Gun a loop to send data from server until an J-%D or -KITL string is
transferred.
STEP $:If J-%D or -KITL is encounteredB close the soc,ets and eit the loop.
CLIENT:'
STEP 1:Start the program.
STEP :Import net pac,age and other pac,ages.
STEP !:Declare object for Soc,et and DataInputStream to receive server data.
STEP ":Gun a loop to receive data from server and store it in a string using
DataInputStream.
STEP #:@rint the string to displa$ the server data and eit when an J-%D or -KITL
message is encountered .
-
8/14/2019 Java Network programs
9/91
PROGRAM:
SERER:'
import java.io.'(
import java.net.'(
class Server)
public static void main"String a*+#throws IC-ception
)Serversoc,et ss/ new ServerSoc,et"
-
8/14/2019 Java Network programs
10/91
O&TP&T:
SERER:'
-21cn7Server.java
-21cn7java Server
-nter the message to send2
=omputer networ,
-%D
CLIENT:'
-21cn7javac =lient.java
-21cn7java =lient
essage Geceived2
=omputer networ,
-%D
RES<:
Thus the java program implementing TCP' o*e +ay ,omm-*.,at.o*has been
successfull$ coded and eecuted
EX: NO: "a IMPLEMENTATION OF ECHO COMMAND
-
8/14/2019 Java Network programs
11/91
AIM:
To write a java program to demonstrate the -=!C command.
ALGORITHM:
SERER:'
STEP1:Start
STEP:Import net pac,age and other pac,ages.
STEP!:Declare objects for DataInputStreamB soc,et and @rintMriter to receive server
and sendit bac,.
STEP":Store message in a string and print message using print method.
STEP#: Send the same received message to server using @rintMriter and Soc,et.
STEP$:Mhen received message is -%D then eit program eecution.
CLIENT:'
STEP1:Start
STEP:Import net pac,age and other necessar$ pac,ages.
STEP!:Declare objects from ServerSoc,et and Soc,et.
STEP":Declare Cbjects for @rintStream to write message from server to client.
STEP#:>et user input and store it in a string.
STEP$:Using print method receive client echo message and print it.
STEP%:Terminate program when message is sent.
PROGRAM:
-
8/14/2019 Java Network programs
12/91
ECHO SERER:'
import java.io.'(
import java.net.'(class -choServer
)public static void main"String a*+#
)tr$
)
Soc,et s/null(ServerSoc,et ss/new ServerSoc,et"
-
8/14/2019 Java Network programs
13/91
DataInputStream in/new DataInputStream"S$stem.in#(
&ufferedGeader br4/new &ufferedGeader "new InputStreamGeader"S$stem.in##(
&ufferedGeader br8/new &ufferedGeader "new InputStreamGeader"s.getInputStream"###(@rintMriter print/new @rintMriter"s.getCutputStream"##(
S$stem.out.println"Jessage to be echoed20#(String str/br4.readine"#(
print.println"str#(
print.flush"#(S$stem.out.println"br8.readine"##(
6
catch"Un,nown!ost-ception e#
)6
catch"IC-ception e#
)
S$stem.out.println"0error203e#(6
66
O&TP&T:
-
8/14/2019 Java Network programs
14/91
ECHO SERER:'
-21cn7javac -choServer.java
-21cn7java -choServer
Soc,et*addr/94:8.4;
-
8/14/2019 Java Network programs
15/91
AIM:
To write a java program to implement the PING ,omma*d.
ALGORITHM:
STEP 1:Start the program.
STEP :Import net and pac,ages.
STEP !:Initiali?e the ping server with both soc,ets as null value.
STEP ":Start the ServerSoc,et at the clientand give the I@ address of the server.
STEP #:The client program is then started b$ starting soc,et.
STEP $: At the receiver endB the server is pinged.
PROGRAM:
-
8/14/2019 Java Network programs
16/91
SERER:'
import java .io.'(
import java.net.'(public class @server
)public static void main"String a*+#
)String line4Bline8(
int i(
ServerSoc,et ss(DataInputStream di(
@rintStream ps(
Soc,et soc(ss/null(
soc/null(
tr$)
ss/new ServerSoc,et"::::#(
6
catch"-ception e#)
S$stem.out.print"e#(
6
S$stem.out.println"0@ing server0#(
tr$)
soc/ss.accept"#(
di/new DataInputStream"soc.getInputStream"##(
ps/new @rintStream"soc.getCutputStream"##(for"i/5(iN(i33#
)
line4/di.readine"#(S$stem.out.println"0@inged b$ client0#(
ps.println"line430repl$ from host2b$tes/H8 timeNms TT/48O0#(
6di.close"#(
ps.close"#(
6catch"-ception e#
)
S$stem.out.print"e#(
66 6
CLIENT:'
-
8/14/2019 Java Network programs
17/91
import java.io.'(
import java.net.'(
public class @client)
public static void main"String a*+#
)@rintMriter out/null(
int iBjB,(
&ufferedGeader networ,In/null(tr$
)
S$stem.out.print"0-nter I@ Address20#(
DataInputStream in/new DataInputStream"S$stem.in#(String ip/in.readine"#(
Soc,et theSoc,et/new Soc,et"ipB::::#(
networ,In/new &ufferedGeader"new InputStreamGeader"S$stem.in##(
out/new @rintMriter"theSoc,et.getCutputStream"##(S$stem.out.print"0pinging 03ip30 with H8 b$tes of data0#(
for"i/5(iN(i33#)
out.println"ip#(
out.flush"#(
String inp/networ,In.readine"#(
if"inpP/null#
)for"j/5(jN45555(j33#
)
for",/5(,NO5555(,33#)66
S$stem.out.println"0Gepl$ from03inp#(
6else
)
for"i/5(iN(i33#
)for"j/5(jN45555(j33#
)
for",/5(,NO5555(,33#)6S$stem.out.println"01n GeFuested time out0#(
6
66
6
6
catch"IC-ception e#
-
8/14/2019 Java Network programs
18/91
)
for"i/5(iN(i33#)
for"j/5(jN45555(j33#
)for",/5(,NO5555(,33#)6
6
S$stem.out.println"01n GeFuested time out0#(6
6
tr$
)if"networ,InP/null#
networ,In.close"#(
if"outP/null#
out.close"#(6catch"-ception e#
)S$stem.out.println"01n GeFuested time out0#(
6
66
O&TP&T:
-
8/14/2019 Java Network programs
19/91
SERER:'
-21cn7javac @ingServer.java
-21cn7java @ingServer
@ing server
@inged b$ client
CLIENT:'
-21cn7javac @ing=lient.java
-21cn7java @ing=lient
-nter I@ Address2
4;:.8O.4.8
@inging 4;:.8O.4.8 with H8 b$tes of data
Gepl$ from 4;:.8O.4.4 Gepl$ from2 b$tes/H8 timeN 4 ms TT/48et an user input in the form of bit data is string.
STEP":>et an generator data from the user
.
STEP#:Gead the length of the string and convert the data into another format b$
deducing < from it.
STEP$:%ow add the generator data to the original data and send the string as
transmitter string.
STEP%:In the receiving endB enter the generator code.
STEP5:Using the generator codeB to the length of the received stringB add < to the
. number format of the string b$ character
STEP7:If the generator string is wrongB displa$ Jmessage received with error.L
STEP18:If the generator string is correctB perform step < and displa$ Jmessage
received with no error.L
STEP11:-nd the program.
PROGRAM:
import java.io.'(
-
8/14/2019 Java Network programs
34/91
import java.lang.'(
public class crc
)
public static void main"String args*+#throws IC-ception
)
int f*+/new int*8O+(
int gen*+/new int*45+(
int rem*+/new int*45+(
int flenBglenBrlenBiBj(
int pBsumBiframeBigenBirem(
String data(
&ufferedGeader in/new &ufferedGeader"new InputStreamGeader"S$stem.in##(
S$stem.out.print"01n-%T-G %-M GA- 20#(
data/in.readine"#(
flen/data.length"#4(for"i/5(iNdata.length"#(i33#
f*i+/""int#"data.charAt"i###
-
8/14/2019 Java Network programs
35/91
irem/iframeYigen(
irem/igenirem(
iframe3/irem(
i/5(
while"iframe75#
)
f*i+/iframeY8(
iframe/iframe98(
i33(
6
if"iframe//4#
f*i+/iframe(
S$stem.out.print"01n1nTGA%SITT-D STGI%> 20#(
for"i/flen(i7/5(i#
S$stem.out.print"f*i+#(S$stem.out.print"01n1n-%T-G T!- G-=-IR-D STGI%> 20#(
data/in.readine"#(
flen/data.length"#4(
for"i/5(iNdata.length"#(i33#
f*i+/""int#"data.charAt"i###
-
8/14/2019 Java Network programs
36/91
45454
TGA%SITT-D STGI%>
45455454
-%T-G T!- G-=-IR-D STGI%>
45454
-SSA>- G-=-IR-D MIT! %C -GGCG
RES<:
Thus the java program implementing the process of ,y,9., red-*da*,y ,he, has
been successfull$ coded and eecuted.
EX: NO: 5 ONE ;IT SLIDING (INDO( PROTOCOL
AIM:
-
8/14/2019 Java Network programs
37/91
To write a java program to implement the process of o*e 2.t s9.d.*/ +.*do+
-
8/14/2019 Java Network programs
38/91
STEP 18:=ontinue the process till all the specified number of frames are sent and
ac,nowledgement is received
STEP 11:Mhen all the frames are sent and ac,nowledgement is received eit
the program.
PROGRAM:
SERER:'
import java.lang.s$stem(
import java.net.'(
-
8/14/2019 Java Network programs
39/91
import java.io.'(
class Server4
)public static void main"String args*+#
)
tr$)
&uferedInputStream in(
ServerSoc,et ss / new ServerSoc,et"O555#(S$stem.out.println"JMaiting for connectionL#(
Soc,et client / ss.accept"#(
S$stem.out.println"JGeceived reFuest for sending framesL#(
in / new &ufferedInputStream"client.getInputStream"##(DataCutputStream out/ new DataCutputStream"client.getCutputStream"##(
int p/ in.read"#(
S$stem.out.println"JSendingL#(
for"int i/4( iN/p( 33i#)
S$stem.out.println"JSending frame no2L3i#(
out.write"i#(
out.flush"#(S$stem.out.println"Jwaiting for ac,nowledgeZL#(
Thread.sleep"O555#(
int a/ in.read"#(
S$stem.out.println"JGeceived ac,nowledge for frame no23i3LasL 3a#(6
out.flush"#(
in.close"#2out.close"#(
client.close"#(
ss.close"#(S$stem.out.println"J[uiting..L#(
6catch"IC-ception e#
)
S$stem.out.println"e#(6catch"Interrupted-ception e#)6
6
6
CLIENT:'
import java.lang.'(
Import java.net.'(
import java.io.'(
-
8/14/2019 Java Network programs
40/91
import java.math.'(
class =lient4
)public static void main"String *+args#
)
tr$)
InetAddress addr / InetAddress.get&$%ame"Jocal!ostL#(
S$stem.out.println"addr#(Soc,et connection/ new Soc,et"addrBO555#(
DataCutputStream out/ new DataCutputStream"connection.getCutputStream"##(
&ufferedInputStream in / new &ufferedInputStream"connection.getInputStream"##(
&ufferedInputStream inn/ new &ufferedInputStream"connection.getInputStream"##(&ufferedGeader ,i/ new &ufferedGeader"new InputStreamGeader"S$stem.in##(
int flag /5(
S$stem.out.println"JconnectedZL#(
S$stem.out.println"J-nter no. of frames to reFuested to server2L#(int c/ Integer.parseInt",i.readine"##(
out.write"c#(out.flush"#(
int iB jj/5(
while"jjNc#)
i/ in.read"#(
S$stem.out.println"JGeceived frame no2L3i#(
S$stem.out.println"JSending ac,nowledgement for frame no2L3i#(
out.write"i#(
out.flush"#(jj33(
6
out.flush"#(in.close"#2
inn.close"#(
out.close"#(
connection.close"#(S$stem.out.println"J[uiting..L#(
6
catch"-ception e#)
S$stem.out.println"e#( 6 6 6
O&TP&T:
SERER:'
-21cn7javac Server4.java
-
8/14/2019 Java Network programs
41/91
-21cn7 java Server4
Maiting for connection
Geceived reFuest for sending frames
Sending
Sending frame no24
Maiting for ac,nowledgeZ
Geceived ac,nowledge for frame no24 as 4
[uitting..
CLIENT:'
-21cn7javac =lient4.java
-21cn7 java =lient4
ocal!ost
=onnectedZ
-nter the no. of frames to reFuested to server2
4
Geceived frame no24
Sending ac,nowledgment for frame no24
[uittingZ.
RES<:
Thus the java program implementing the O*e 2.t s9.d.*/ +.*do+
-
8/14/2019 Java Network programs
42/91
To write a java program to implement FTP -s.*/ TCP.
ALGORITHM:
SERER:'
STEP1: Start the program.
STEP:Import net and other pac,ages.
STEP!:Declare object for ServerSoc,et and Soc,et to send a file.
STEP": Declare objects for ileCutputStream to write into a file.
STEP#:>et the filename from the user an dt$pe the file using ileCutputStream"#
conent.
CLIENT:'
STEP1:Start the program.
STEP:Import net and other pac,ages.
STEP!: Declare objects for Server and ileInputStream to write the server file into
server
STEP":Using the InputStream method receive the filename and print the received
file name
STEP#:=reate a new client file using ileInputStream and write the contents of the
server file into it using ileCutputSream.
STEP $:Mhile writingB read the source file as characters and write into the other file.
STEP %:-nd the program after writing into the client file.
PROGRAM:
FILE SERER:'
import java.net.'(
import java.io.'(
-
8/14/2019 Java Network programs
43/91
-
8/14/2019 Java Network programs
44/91
ileCutputStream fos / new ileCutputStream"fname3L.ttL#(
&ufferedCutputStream bos/ new &ufferedCutputStream"fosB85
-
8/14/2019 Java Network programs
45/91
-21cn7java ileServer
-nter the filename2
ile
=omputer networ,
\
FILE CLIENT:'
-21cn7javac ile=lient.java
-21cn7java ile=lient
Geceived file2 ile
-nter the new file name2
ile4
RES<:
Thus the java program implementing T@ using T=@ has been successfull$
coded and eecuted.
EXNO: 18a T(O (A) COMM&NICATION &SING TCP=IP
AIM:'
To write a java program to perform JT+o (ay Comm-*.,at.o* &s.*/ TCP=IPL.
ALGORITHM:
-
8/14/2019 Java Network programs
46/91
SERER:'
STEP12 Import the java pac,age and create class TwoServerT=@.
STEP2 String of argument is passed to the args *+.
STEP!2 =reate a new server soc,et and bind it to the port.
STEP"2 Accept the client connection at the reFuested port.
STEP#2 Cverride the methods of output stream
.
STEP$2 &$ using data input stream a message is received from clientserver andW
that message are echoed to client again b$ print stream.
STEP%2 Mrite message at the soc,et.
STEP52 If the read line is J-itL then close the client connection.
STEP72 -nd of main.
STEP182 -nd of TwoServerT=@ class.
-
8/14/2019 Java Network programs
47/91
CLIENT:'
STEP12 Import the java pac,age and create class Two=lientT=@.
STEP2 String of argument is passed to the args *+.
STEP!2 =reate a new soc,et reFuesting for communication with server.
STEP"2 =reate objects for buffered reader V inputs stream.
STEP#2 &$ using data input stream a message is received from clientserver andW
that message are echoed to client again b$ print stream.
STEP$2 Cverride the methods of input stream.
STEP%2 Gead the message from the soc,et.
STEP52 -nd of main
.
STEP72 -nd of Two=lientT=@ class.
-
8/14/2019 Java Network programs
48/91
SERER SIDE:'
import java.net.'(
import java.io.'(class TwoServerT=@
)public static void main"String args*+#throws IC-ception)
Tr$
)
ServerSoc,et ss/new ServerSoc,et";5#(Soc,et sc(
S$stem.out.println"0Maiting or =lient...0#(
sc/ss.accept"#(&ufferedGeader in/new &ufferedGeader"new W
InputStreamGeader"sc.getInputStream"###(
@rintMriter out/new @rintMriter"sc.getCutputStream"#Btrue#(&ufferedGeader stdin/new &ufferedGeader"new W
InputStreamGeader"S$stem.in##(
String aBb(
S$stem.out.println"0=onnected To =lient...03sc#(while"true#
)
S$stem.out.print"0-nter The essage or =lient20#(
a/stdin.readine"#(
if"a.eFuals"0-it0##
brea,(out.flush"#(
out.println"a#(b/in.readine"#(
S$stem.out.print"0=lient Sa$s203b#(
6
sc.close"#(6
catch"-ception e#
)S$stem.out.println"0-rror03e#(
66 6
-
8/14/2019 Java Network programs
49/91
CLIENT SIDE:'
import java.io.'(import java.net.'(
class Two=lientT=@
)public static void main"String args*+#throws IC-ception)
tr$
)Soc,et sc(
if"args.lengthP/4#
sc /new Soc,et"0ocal!ost0B;5#(else
sc/new Soc,et"0SEST-0B;5#(
&ufferedGeader in/new &ufferedGeader"new W
InputStreamGeader"sc.getInputStream"###(@rintMriter out/new @rintMriter"sc.getCutputStream"#Btrue#(
&ufferedGeader stdin/new &ufferedGeader"new
InputStreamGeader"S$stem.in##(String aBb(
S$stem.out.println"0Maiting or Server...0#(
S$stem.out.println"0=onnected To Server...03sc#(b/in.readine"#(
S$stem.out.println"0Server Sa$s203b#(
while"true#)
S$stem.out.println"0-nter The essage or Server20#(a/stdin.readine"#(
if"a.eFuals"0-it0##brea,(
out.println"a#(
b/in.readine"#(S$stem.out.print"0Server Sa$s203b#(
6
sc.close"#(6
catch"-ception e#
)S$stem.out.println"0-rror03e#(6
6
-
8/14/2019 Java Network programs
50/91
O&TP&T:'
SERER O&TP&T:'
=21jd,4.H1bin7javac [email protected]
=21jd,4.H1bin7java TwoServerT=@
Maiting or =lient...
=onnected To =lient...
Soc,et*addr/localhost948X.5.5.4Bport/HO5:Blocalport/;5+
-nter The essage or =lient2!ai
=lient Sa$s2!ai...!ow Are U
-nter The essage or =lient2Ea ine...
=lient Sa$s2null-nter The essage or =lient2-it
=21jd,4.H1bin7
CLIENT O&TP&T:'
=21jd,4.H1bin7javac [email protected]
=21jd,4.H1bin7java Two=lientT=@
Maiting or Server...
=onnected To Server...
Soc,et*addr/ocal!ost948X.5.5.4Bport/;5Blocalport/HO5:+
Server Sa$s2!ai
-nter The essage or Server2!ai...!ow Are U
Server Sa$s2Ea ine...
-nter The essage or Server2-it
=21jd,4.H1bin7
RES<:'
Thus the program has been eecuted and the output is verified successfull$.
-
8/14/2019 Java Network programs
51/91
EX: NO: 182 T(O (A) COMM&NICATION' &DP
AIM:
To write a java program to implement t+o +ay ,omm-*.,at.o* -s.*/ &DP0
ALGORITHM:
SERER:'
STEP1: Import the net and other pac,ages.
STEP:String of argument is passed to the args *+.
STEP!:=reate a Datagram soc,et and bind it to the special port specified.
STEP": >et the buffer si?e and I@ Address of local host and communication
with the client.
STEP#:Send the Datagram pac,et to the destination address of given port.
STEP$:Store the input in the buffer at the specific location.
CLIENT:'
STEP1:Import the net pac,age and other pac,ages.
STEP:String of argument is passed to the args *+.
STEP!:=reate a Datagram soc,et and bind it to the special port specified.
STEP":=reate pac,et for receving arra$ of b$tes with maimum b$tes.
STEP#:Specif$ the common port for connecting datagram soc,et.
-
8/14/2019 Java Network programs
52/91
PROGRAM:
SENDER:'
import java.io.'(
import java.net.'(
class Sender)
public static void main "String a*+#throws -ception
)while"true#
)
DatagramSoc,et ds / new DatagramSoc,et"#(&ufferedGeader br / new &ufferedGeader"new InputStreamGeader"S$stem.in##(
S$stem.out.println"0-nter the message20#(
String msg/br.readine"#(
b$te b*+/msg.get&$tes"#(InetAddress add/InetAddress.getocal!ost"#(
Datagram@ac,et dp/new Datagram@ac,et"bBb.lengthBaddB48H#(
ds.send"dp#(if"msg.eFuals"0-KIT0##
S$stem.eit"5#(
b$te b4*+/new b$te*8OO+(Datagram@ac,et dp4/new Datagram@ac,et"b4Bb4.length#(
ds.receive"dp4#(
String msg4/new String"dp4.getData"##(
S$stem.out.println"0Geceived message203msg4#(
666
RECEIER:'
import java.io.'(
import java.net.'(class Geceiver
)
public static void main"String a*+#throws IC-ception)
DatagramSoc,et ds/new DatagramSoc,et"48H#(
b$te b*+/new b$te*8OO+(
while"true#)
Datagram@ac,et dp/new Datagram@ac,et"bBb.length#(
ds.receive"dp#(
-
8/14/2019 Java Network programs
53/91
String msg/new String"dp.getData"##(
S$stem.out.println"0essage Geceived203msg#(
InetAddress add/dp.getAddress"#(int port/dp.get@ort"#(
S$stem.out.println"0-nter the message to send20#(
&ufferedGeader br/new &ufferedGeader"new InputStreamGeader"S$stem.in##(
String msg4/br.readine"#(
b$te b4*+/msg4.get&$tes"#(Datagram@ac,et dp4/new Datagram@ac,et"b4Bb4.lengthBaddBport#(
ds.send"dp4#(
if"msg4.eFuals"0-KIT0##
S$stem.eit"5#(6
6
6
-
8/14/2019 Java Network programs
54/91
O&TP&T:
SENDER:'
-21cn7javac Sender.java
-21cn7java Sender
-nter the message2 hi
Geceived message2 hi
-nter the message2
-KIT
RECEIER:'
-21cn7javac receiver.java
-21cn7java Geceiver
essage Geceived2 hi
-nter the message to send2 hi
essage Geceived2 -KIT
RES<:
Thus the java program implementing UD@> t+o +ay ,omm-*.,at.o* has been
successfull$ coded and eecuted.
-
8/14/2019 Java Network programs
55/91
EX0NO:11 REMOTE METHOD INOCATION
AIM:
To write a java program to implement remote method .*4o,at.o* ?rm.@0
ALGORITHM:
STEP 1:Start
STEP :-stablish the connection between the client and the server to calculate
factorial operation
STEP !:In imple"# it calls the inter"# which throws eception thrown b$ fact"#.
STEP ":In the client UG has been specified and declared as String.
STEP #:It calls the inter"# to connect with the imple"# and getthe result.
STEP $:Stop.
-
8/14/2019 Java Network programs
56/91
PROGRAM:
RMI SERER:'
import java.rmi.'(
import java.net.'(
import java.io.'(public class server
)
public static void main"String args*+#throws -ception
)imple c/new imple"#(
java.rmi.%aming.rebind"0server0Bc#(
S$stem.out.println"0S-GR-G G-ADE...0#(
66
RMI CLIENT:'
import java.rmi.'(
import java.net.'(import java.io.'(
public class client
)public static void main"String args*+#throws -ception
)&ufferedGeader br/new &ufferedGeader"new InputStreamGeader"S$stem.in##(inter s/"inter#java.rmi.%aming.loo,up"0rmi299ocal!ost9server0#(
S$stem.out.println"0-nter a %o 2 0#(
int n/Integer.parseInt"br.readine"##(
s.fact"n#(6
6
RMI IMPLEMENT:'
import java.rmi.'(import java.net.'(
import java.rmi.server.'(
public class imple etends java.rmi.server.UnicastGemoteCbject implements inter)
public imple"#throws java.rmi.Gemote-ception
)
-
8/14/2019 Java Network programs
57/91
6
public void fact"int n#throws java.rmi.Gemote-ception
)int f/4(
for"int i/4(iN/n(i33#
f/f'i(S$stem.out.println"0actorial of 03n30is 2 03f#(
6
6
RMI INTERFACE:'
import java.rmi.'(
public interface inter etends java.rmi.Gemote
)
public void fact"int n#throws java.rmi.Gemote-ception(6
-
8/14/2019 Java Network programs
58/91
-
8/14/2019 Java Network programs
59/91
EX: NO: 1 SHORTEST PATH RO&TING ALGORITHM
AIM:
To write a java program to implement shortest path routing algorithm.
ALGORITHM:
STEP1:Import the java pac,age and create class spath.
STEP:String of argument is passed to the args *+.
STEP!:Gead the distance b9w pair of nodes.
STEP":If eitsB then get the source and destination node.
STEP#:rom the source nodeB find the possible path and record.
STEP$:Ta,e the smallest path among these path repeat the process till the W
destination is reached.
STEP%:-nd of main.
STEP5:-nd spath class.
-
8/14/2019 Java Network programs
60/91
PROGRAM:
import java.net.'(import java.io.'(
class spath
)public static void main"String args*+#throws IC-ception
)
int nBsBdBiBjB$/5Bsd/455(int*+ in/new int*45+(
int*+*+ m/new int*O+*O+(
int*+ dis/new int*45+(
int*+ path/new int*45+(DataInputStream a/new DataInputStream"S$stem.in#(
S$stem.out.print"0-%T-G T!- %U&-G C R-GT-K 2 0#(
n/Integer.parseInt"a.readine"##(
S$stem.out.print"0-%T-G T!- SCUG=- R-GT-K 2 0#(s/Integer.parseInt"a.readine"##(
S$stem.out.print"0-%T-G T!- D-STI%ATIC% R-GT-K 2 0#(d/Integer.parseInt"a.readine"##(
for"i/4(iNn(i33#
)j/4(
while"jNn#
)
S$stem.out.print"0-%T-G T!- DISTA%=- &-TM--% 03i30 A%D 03"j34#302 0#(m*i+*j34+/Integer.parseInt"a.readine"##(
m*j34+*i+/m*i+*j34+(
j33(6
6
for"i/4(iN/n(i33#)
in*i+/5(
dis*i+/m*s+*i+(
if"m*s+*i+P/5#path*i+/s(
6
in*s+/4(dis*s+/5(
for"i/8(iNn(i33#
)for"j/4(jN/n(j33#
)
if"in*j+//5#
)
-
8/14/2019 Java Network programs
61/91
if"dis*j+Nsd#
)
sd/dis*j+($/j(
6
66
in*$+/4(
for"j/4(jN/n(j33#)
if""in*j+//5#VV"m*$+*j+P/5##
)
if""dis*$+3m*$+*j+#Ndis*j+#)
dis*j+/dis*$+3m*$+*j+(
path*j+/$(
66
66
S$stem.out.print"003d30N0#(
i/d(while"path*i+P/s#
)
S$stem.out.print"003path*i+30N0#(
i/path*i+(6
S$stem.out.print"003s#(
S$stem.out.print"01nDISTA%=- IS 203dis*d+#(6
6
-
8/14/2019 Java Network programs
62/91
O&TP&T:
-21cn7javac [email protected]
-21cn7java Short@ath
-nter The %o.Cf Rertices2
-nter The =ost Cf atri2
Mhether -dges -ist &9M 4 And 8 *4EesB5%o+.
4
-nter The Meight or -dge 4 To 8
O
Mhether -dges -ist &9M 4 And H *4EesB5%o+.
5
Mhether -dges -ist &9M 4 And *4EesB5%o+.
4
-nter The Meight or -dge 4 To
Mhether -dges -ist &9M 8 And 4 *4EesB5%o+.
4
-nter The Meight or -dge 8 To 4
X
Mhether -dges -ist &9M 8 And H *4EesB5%o+.
5
Mhether -dges -ist &9M 8 And *4EesB5%o+.
4
-nter The Meight or -dge 8 To
8
Mhether -dges -ist &9M H And 4 *4EesB5%o+.
5
Mhether -dges -ist &9M H And 8 *4EesB5%o+.
4
-
8/14/2019 Java Network programs
63/91
-nter The Meight or -dge H To 8
H
Mhether -dges -ist &9M H And *4EesB5%o+.
5
Mhether -dges -ist &9M And 4 *4EesB5%o+.
5
Mhether -dges -ist &9M And 8 *4EesB5%o+.
5
Mhether -dges -ist &9M And H *4EesB5%o+.
4
-nter The Meight or -dge To H
4
-nter The Source Rerte24
The Q Ralue Is2
5
4
4
The Q Ralue Is28
5
4
4
The Q Ralue Is2H
5
4
4
@ath*4+/5
Distance*4+/5
-
8/14/2019 Java Network programs
64/91
-
8/14/2019 Java Network programs
65/91
EX: NO: 1!a ;ROADCASTING
AIM:
To write a java program to implement the process of 2road,ast.*/.
ALGORITHM:
SERER:'
STEP1:Start the program.
STEP :Import the net and other pac,ages.
STEP !:The DatagramSoc,et and the DataInputstream are initiali?ed.
STEP ":The buffer is declared for message.
STEP #:The message is obtained from the user.
STEP $:The message is delivered to the client one b$ one with the help of the
Datagram@ac,et.
STEP %:The client details is obtained
STEP%:@ac,ets are then sent to the reciver b$ the help od Datagram soc,et.
STEP5:If message is b$e then the server Fuits.
CLIENT:'
STEP1:Start the program.
STEP:Import net and other pac,age.
STEP!: The DatagramSoc,et and Datagram@ac,et are initiali?ed.
STEP":The message from sender is ta,en as the pac,etfrom buffer.
STEP#: The message is displa$ed along with the I@ address of the server.
STEP$:If the message isb$e then the client Fuits.
-
8/14/2019 Java Network programs
66/91
PROGRAM:
SERER:'
import java.io.'(
import java.net.'(
public class &roadServer)
public static void main"String args*+# throws -ception
)DatagramSoc,et ds / new DatagramSoc,et"#(
DataInputStream dis / new DataInputStream"S$stem.in#(
String msg/ 00(String client / 00(
b$te*+ buf / new b$te*458+(
while"true#
)S$stem.out.println"0Server0#(
msg/ dis.readine"#(
buf/ msg.get&$tes"#(for"int i/4( iN/45( i33#
)
tr$)
client / "0s$sL3i#(
Datagram@ac,et dp/ new Datagram@ac,et "bufBbuf.lengthBInetAddress.get&$%ame"client#BO555#(
ds.send"dp#(
S$stem.out.println"0essage Sent To2 03client#(6catch"-ception e#
)
S$stem.out.println"0Unable To =onnect At...03client3e#(6
if"msg.eFualsIgnore=ase"0&$e0##
)S$stem.out.println"0Server [uits0#(
return(
6
66
6
6
-
8/14/2019 Java Network programs
67/91
CLIENT:'
import java.net.'(
import java.io.'(
public class &road=lient
)public static void main"String args*+#throws -ception
)
DatagramSoc,et ds/ new DatagramSoc,et"O555#(while"true#
)
tr$)
b$te*+ buf/new b$te*458+(
DataInputStream dis/ new DataInputStream"S$stem.in#(String msg/ null(
Datagram@ac,et dp/ new Datagram@ac,et"bufBbuf.length#(ds.receive"dp#(
msg/new String"dp.getData"#B5Bdp.getength"##(InetAddress ia/dp.getAddress"#(
S$stem.out.println"0rom2 03ia30 essage2 03msg#(
if"msg.eFualsIgnore=ase"0&$e0##)
S$stem.out.println"0=lient [uits0#(
return(6
6
catch"-ception e#)S$stem.out.println"J-rrorL3e#(
6
66
6
-
8/14/2019 Java Network programs
68/91
O&TP&T:
SERER:'
-21cn7javac &roadServer.java
-21cn7java &roadServer
Server2 hai
essage successfull$ sent to =lient2 s$s4
Unable To =onnect at2 s$s8java.net.Un,nown!ost-ception2 s$s82 s$s8
Unable To =onnect at2 s$sHjava.net.Un,nown!ost-ception2 s$sH2 s$sH
Unable To =onnect at2 s$sjava.net.Un,nown!ost-ception2 s$s2 s$s
Unable To =onnect at2 s$sOjava.net.Un,nown!ost-ception2 s$sO2 s$sO
Unable To =onnect at2 s$s;java.net.Un,nown!ost-ception2 s$s;2 s$s;
Unable To =onnect at2 s$sXjava.net.Un,nown!ost-ception2 s$sX2 s$sX
Unable To =onnect at2 s$s
-
8/14/2019 Java Network programs
69/91
OTHER:
-21cn7 javac &road=lient.java
-21cn7java &road=lient
rom2 94;:.8O.4.8 essage2 hai
-21cn7 javac &road=lient.java
-21cn7java &road=lient
Server2
-21cn7
RES<:
Thus the java program implementing the process of 2road,ast.*/has been successfull$
coded and eecuted.
-
8/14/2019 Java Network programs
70/91
EXNO: 1!2 M<ICASTING
AIM:'
To write a java program to perform JM-9t.,ast.*/L.
ALGORITHM:
SERER:'
STEP12 Import the java pac,age and create class ultiServer.
STEP2 String of argument is passed to the args *+.
STEP!2 irst we enable the soc,et object for multicasting soc,et.
STEP"2 Using the soc,et objectBwe join the multicast group using the host name.
STEP#2 Cnce we join the group the message is read from the server and it is send to
the client.
STEP$2 Similarl$ the soc,et object to recive the message from client side is enabled
and using the get data methodBwe recive the pac,ets through the given port.
STEP%2 Mhen it reaches J&$eL it Fuits from the group.
STEP52 -nd of main
.
STEP72 -nd of ultiServer class.
-
8/14/2019 Java Network programs
71/91
CLIENT:'
STEP12 Import the java pac,age and create class ulti=lient.
STEP2 String of argument is passed to the args *+.
STEP!2 !ere using the soc,et we join the multicast group.
STEP"2 Using the Datagram pac,ets object Bwe read the message b$ string in W
buffered arra$ .
STEP#2 Similarl$ b$ using readine method input got from client as b$te.
STEP$2 The message is passed using the soc,et object using read method in the W
enabled port.
STEP%2 -nd of main.
STEP52 -nd of &road=lient class.
-
8/14/2019 Java Network programs
72/91
SERER SIDE:'
import java.net.'(
import java.io.'(
class ultiServer
)public static void main"String args*+#throws -ception
)
ulticastSoc,et soc,et(
Datagram@ac,et pac,et(
String msgBmsg4Bmsg8(
b$te*+ buf/new b$te*O555+(
DataInputStream one/new DataInputStream"S$stem.in#(
soc,et/new ulticastSoc,et"#(
soc,et.join>roup"InetAddress.get&$%ame"088.5.5.40##(
S$stem.out.println"01n1nocalI@2 03InetAddress.getocal!ost"##(
while"true#
)
S$stem.out.println"0essage !ere20#(
msg4/one.readine"#(
msg8/ InetAddress.getocal!ost"#.toString"#(
msg/msg430030rom Server03msg8(
buf/msg.get&$tes"#(
pac,et/ new
Datagram@ac,et"bufBbuf.lengthBInetAddress.get&$%ame"04;:.8O.85.
-
8/14/2019 Java Network programs
73/91
CLIENT SIDE:'
import java.io.'(import java.net.'(
class ulti=lient
)public static void main"String args*+#throws -ception
)
ulticastSoc,et soc,et/new ulticastSoc,et"O#(soc,et.join>roup"InetAddress.get&$%ame"088.5.5.40##(
String msgBmsg4Bmsg8(
DataInputStream one/new DataInputStream"S$stem.in#(
b$te*+ buf/new b$te*4O55+(S$stem.out.println"0ocal I@2 03InetAddress.getocal!ost"##(
while"true#
)
Datagram@ac,et pac,et/new Datagram@ac,et"bufBbuf.length#(
soc,et.receive"pac,et#(msg/new String"pac,et.getData"#B5Bpac,et.getength"##(
S$stem.out.println"0essage2 03msg#(
int c/45(char c4*+/new char*c+(
msg.get=hars"5BHBc4B5#(
String temp/0 0(
for"int i/5(iNH(i33#)
temp/temp3c4*i+(
6if"temp.eFualsIgnore=ase"0&$e0##
)
S$stem.out.println"0=lient [uits0#(return(
6
msg/null(
buf/new b$te*4555+(S$stem.out.println"0essage2 0#(
msg4/one.readine"#(
msg8/InetAddress.getocal!ost"#.toString"#(msg/msg430 030rom =lient2 03msg8(
buf/msg.get&$tes"#(
InetAddress address/pac,et.getAddress"#(int port/pac,et.get@ort"#(
pac,et/new Datagram@ac,et"bufBbuf.lengthBaddressBport#(
soc,et.send"pac,et#(
6 6 6
-
8/14/2019 Java Network programs
74/91
-
8/14/2019 Java Network programs
75/91
AIM:
To write a java program to implement Address Reso9-t.o* Proto,o9.
ALGORITHM:
CLIENT SIDE:
STEP1:-stablish a connection between the =lient and Server.
Soc,et ss/new Soc,et"InetAddress.getocal!ost"#B4455#(
STEP:=reate instance output stream writer .@rintMriter ps/new @rintMriter"s.getCutputStream"#Btrue#(
STEP!:>et the I@ Address to resolve its ph$sical address.
STEP":Send the I@Address to its output Stream.ps.println"ip#(
STEP#:@rint the @h$sical Address received from the server.
SERER SIDE:
STEP12 Accept the connection reFuest b$ the client.
ServerSoc,et ss/new ServerSoc,et"8555#( Soc,et s/ss.accept"#(
STEP2 >et the I@address from its inputstream.&ufferedGeader br4/new
&ufferedGeader"new InputStreamGeader"s.getInputStream"###(
ip/br4.readine"#(
STEP!2 During runtime eecute the process Guntime r/Guntime.getGuntime"#(
@rocess p/r.eec"0arp a 03ip);
STEP":Send the @h$sical Address to the client.
PROGRAM:
-
8/14/2019 Java Network programs
76/91
-
8/14/2019 Java Network programs
77/91
&ufferedGeader br4/new &ufferedGeader"new InputStreamGeader"s.getInputStream"###(
String ip(
ip/br4.readine"#(Guntime r/Guntime.getGuntime"#(
@rocess p/r.eec"0arp a 03ip#(
&ufferedGeader br8/new &ufferedGeader"new InputStreamGeader"p.getInputStream"###(String str(
while""str/br8.readine"##P/null#
)ps.println"str#(
6
6
catch"IC-ception e#)
S$stem.out.println"0-rror03e#(
6
6 6
O&TP&T
-
8/14/2019 Java Network programs
78/91
=21%etwor,ing @rograms7java ArpServer
=21%etwor,ing @rograms7java Arp=lient
-nter the I@ADDG-SS2
4:8.4;
-
8/14/2019 Java Network programs
79/91
EX0NO:1# CR)PTOGRAPH)
AIM:
To write a java program to implement the ,9.e*t ser4er ,ry
-
8/14/2019 Java Network programs
80/91
STEP":Store the string and the ,e$ to different variabls.
STEP#:Gead the string character wise to its length.
STEP$:GeFuest the user to enter the ,e$ for decr$ption.
STEP%:=ompare the ,e$ with received ,e$.
STEP5:If both the ,e$s are eFual thenB
=onvert the each character of the string to integer and deduce the ,e$ value.
=onvert the integer value to character and form it into a string and displa$ the
decr$pted string.
-lse
@rint the message as cannot decr$ptB wrong message and Fuit.
STEP7:-nd the program.
PROGRAM:
-
8/14/2019 Java Network programs
81/91
SERER:
import java.io.'(import java.net.'(
class eser
)public static void main"String args*+# throws -ception)
tr$
)ServerSoc,et ss/new ServerSoc,et":555#(
Soc,et s/ss.accept"#(
@rintStream ps/new @rintStream"s.getCutputStream"##(&ufferedGeader br/new &ufferedGeader"new InputStreamGeader"S$stem.in##(
int i/5B,(
String s8/0 0(
S$stem.out.println"01n -%T-G A STGI%>20#(String s4/br.readine"#(
S$stem.out.print"01n -%T-G T!- Q-E20#(
,/Integer.parseInt"br.readine"##(while"iNs4.length"##
)
char c4/s4.charAt"i33#(int n/"int#c43,34(
c4/"char#n(
s83/c4(6
S$stem.out.println"0-%=GE@T-D STGI%> IS203s8#(ps.println"s8#(
ps.println",#(ps.close"#(
s.close"#(
ss.close"#(6
catch"-ception e#
)S$stem.out.println"01n -GGCG203e#(
6
66
CLIENT:
-
8/14/2019 Java Network programs
82/91
import java.io.'(
import java.net.'(class ecli
)
public static void main"String args*+# throws -ception)
Soc,et s/new Soc,et"0ocal!ost0B:555#(
&ufferedGeader br/new &ufferedGeader"new InputStreamGeader"s.getInputStream"###(int i/5B,4(
&ufferedGeader br4/new &ufferedGeader"new InputStreamGeader"S$stem.in##(
String sH/0 0(
String s8/br.readine"#(int ,/Integer.parseInt"br.readine"##(
S$stem.out.println"0G-=-IR-D S>203s8#(
S$stem.out.println"01n -%T-G T!- Q-E20#(
,4/Integer.parseInt"br4.readine"##(if",//,4#
)while"iNs8.length"##
)
char c4/s8.charAt"i33#(int n/"int#c4,44(
c4/"char#n(
sH3/c4(
6S$stem.out.println"0D-=GE@T-D STGI%> IS203sH#(
s.close"#(
66
6
-
8/14/2019 Java Network programs
83/91
O&TP&T:
SERER:
=21jd,4.H1bin7java eser
-%T-G A STGI%>2the server
-%T-G T!- Q-E2
-%=GE@T-D STGI%> IS2 $mjYjw)jw
CLIENT:
=21jd,4.H1bin7java ecli
G-=-IR-D S>2 $mjYjw)jw
-%T-G T!- Q-E2
D-=GE@T-D STGI%> IS2 ]the server
RES<:
Thus the java program implementing the process of ,ry
-
8/14/2019 Java Network programs
84/91
AIM:
To stud$ the IEEE 580! Proto,o9.
IEEE 580! Proto,o9:
The I---
-
8/14/2019 Java Network programs
85/91
-ach -therneteFuipped computer operates independentl$ of all other stations on the networ,2
there is no central controller. All stations attached to an -thernet are connected to a shared
signaling s$stemB also called the medium. To send data a station first listens to the channelB andwhen the channel is idle the station transmits its data in the form of an -thernet frameB or pac,et.
After each frame transmissionB all stations on the networ, must contend eFuall$ for the netframe transmission opportunit$. Access to the shared channel is determined b$ the medium
access control "A=# mechanism embedded in the -thernet interface located in each station.The medium access control mechanism is based on a s$stem called =arrier Sense ultiple
Access with =ollision Detection "=SA9=D#.
As each -thernet frame is sent onto the shared signal channelB all -thernet interfaces loo, at thedestination address. If the destination address of the frame matches with the interface addressB
the frame will be read entirel$ and be delivered to the networ,ing software running on that
computer. All other networ, interfaces will stop reading the frame when the$ discover that the
destination address does not match their own address.
Mhen it comes to how signals flow over the set of media segments that ma,e up an -thernet
s$stemB it helps to understand the topolog$ of the s$stem. The signal topolog$ of the -thernet is
also ,nown as the logical topolog$B to distinguish it from the actual ph$sical la$out of the media
cables. The logical topolog$ of an -thernet provides a single channel "or bus# that carries-thernet signals to all stations.
Ma6.m-m Tra*sfer Rate:
The aimum Data Transfer Gate for I--- bps -thernet "I---
-
8/14/2019 Java Network programs
86/91
@reamble "@G-# X b$tes. The @G- is an alternating pattern of ones and ?eros that tells
receiving stations that a frame is comingB and that provides a means to s$nchroni?e the
framereception portions of receiving ph$sical la$ers with the incoming bit stream. Startofframe delimiter "SD# 4 b$te. The SC is an alternating pattern of ones and
?erosB ending with two consecutive 4bits indicating that the net bit is the leftmost bit
in the leftmost b$te of the destination address.
Destination address "DA# ; b$tes. The DA field identifies which station"s# should
receive the frame.
Source addresses "SA# ; b$tes. The SA field identifies the sending station.
ength9T$pe 8 b$tes. This field indicates either the number of A=client data b$tes
that are contained in the data field of the frameB or the frame t$pe ID if the frame is
assembled using an optional format.
Data Is a seFuence of n b$tes ";/N n /N4O55# of an$ value. "The total frame minimum
is ;b$tes.#
rame chec, seFuence "=S# b$tes. This seFuence contains a H8bit c$clic redundanc$
chec, "=G=# valueB which is created b$ the sending A= and is recalculated b$ thereceiving A= to chec, for damaged frames.
Note:
ultiple -thernet segments can be lin,ed together to form a larger -thernet A% using a signal
amplif$ing and retiming device called a repeater. Through the use of repeatersB a given -thernets$stem of multiple segments can grow as a 0nonrooted branching tree.0 _`%onrooted0 means
that the resulting s$stem of lin,ed segments ma$ grow in an$ directionB and does not have a
specific root segment. ost importantl$B segments must never be connected in a loop. -ver$segment in the s$stem must have two endsB since the -thernet s$stem will not operate correctl$
in the presence of loop paths.
-ven though the media segments ma$ be ph$sicall$ connected in a star patternB with multiple
segments attached to a repeaterB the logical topolog$ is still that of a single -thernet channel that
carries signals to all stations
-
8/14/2019 Java Network programs
87/91
EX0NO:1%: ST&D) OF 3E) DISTRI;&TION AND CERTIFICATION SCHEMES
AIM:
To stud$ the ,e$ d.str.2-t.o* a*d ,ert.f.,at.o* s,hemes
3E) DISTRI;&TION AND CERTIFICATION2
@ublic ,e$ encr$ption has its own difficultiesB in particular theproblem of obtaining someone^s true public ,e$. &oth of these problemsdetermining a shared
,e$ for s$mmetric ,e$ cr$ptograph$ and securel$ obtaining the public ,e$ for public ,e$
cr$ptograph$can be solved using atr-sted .*termed.ary0or s$mmetric ,e$ cr$ptograph$B the
trusted intermediar$ is called a ey d.str.2-t.o* ,e*ter "QD=#B which is a singleB trusted
networ, entit$ with whom one has established a shared secret ,e$. Me will see that one can use
the QD= to obtain the shared ,e$s needed to communicate securel$ with allother networ,
entitiesB avoiding some of the pitfalls we uncovered in. or public ,e$ cr$ptograph$B the trusted
intermediar$ is called a ,ert.f.,at.o* a-thor.ty ?CA@0A certification authorit$ certifies that a
public ,e$ belongs to a particular entit$ "a person or a networ, entit$#. or a certified public ,e$B
if one can safel$ trust the =A that certified the ,e$B then one can be sure about to whom thepublic ,e$ belongs. Cnce a public ,e$ is certifiedB then it can be distributed from just about
an$whereB including a public ,e$ serverB a personal Meb pageB or a dis,ette.
THE 3E) DISTRI;&TION CENTER:
Suppose once again that &ob and Alice want to communicate using s$mmetric ,e$ cr$ptograph$.
The$ have never met "perhaps the$ just met in an online chat room# and thus have not
established a shared secret ,e$ in advance. !ow can the$ now agree on a secret ,e$B given that
the$ can communicate with each other onl$ over the networ, A solution often adopted inpractice is to use a trusted ,e$ distribution center.
The QD= is a server that shares a different secret s$mmetric ,e$ with each registered user. This
,e$ might be manuall$ installed at the server when a user first registers. The QD= ,nows thesecret ,e$ of each userB and each user can communicate securel$ with the QD= using this ,e$.
et^s see how ,nowledge of this one ,e$ allows a user to securel$ obtain a ,e$ for
communicating with an$ other registered user. Suppose that Alice and &ob are users of the QD=(
-
8/14/2019 Java Network programs
88/91
the$ onl$ ,now their individual ,e$BKA-KDCandKB-KDCB respectivel$B for communicating securel$
with the QD=.
4. UsingKA-KDCto encr$pt her communication with the QD=B Alice sends a message to the
QD= sa$ing she "A# wants to communicate with &ob "B#. Me denote this messageBKA-
KDC"ABB#.
8. The QD=B ,nowingKA-KDCB decr$ptsKA-KDC"ABB#. The QD= then generates a randomnumberBR1. This is the shared ,e$ value that Alice and &ob will use to perform
s$mmetric encr$ption when the$ communicate with each other. This ,e$ is referred to as
a o*e't.me sess.o* eyas Alice and &ob will use this ,e$ for onl$ this one session thatthe$ are currentl$ setting up. The QD= now needs to inform Alice and &ob of the value
ofR1. The QD= thus sends bac, a message to AliceB encr$pted using KAKDCB containing
the following2
R1B the onetime session ,e$ that Alice and &ob will use to communicate.
A pair of values2AB andR1B encr$pted b$ the QD= using &ob^s ,e$BKBKDC. Me
denote thisKBKDC"ABR1#. It is important to note that QD= is sending Alice not
onl$ the value ofR1for her own useB but also an encr$pted version of R1and
Alice^s nameB encr$pted using &ob^s ,e$. Alice can^t decr$pt this pair of values inthe message "she doesn^t ,now &ob^s encr$ption ,e$#B but then she doesn^t reall$
need to. Me^ll see shortl$ that Alice will simpl$ forward this encr$pted pair of
values to &ob "who can decr$pt them#.
These items are put into a message and encr$pted using Alice^s shared ,e$. The
message from the QD= to Alice is thusKAKDC"R1BKBKDC"R1##.
H. Alice receives the message from the QD=B etractsR1from the message and saves it.
Alice now ,nows the onetime session ,e$BR1. Alice also etractsKBKDC"ABR1# andforwards this to &ob.
. &ob decr$pts the received messageBKBKDC"ABR1#B usingKBKDCand etractsAandR1. &ob
now ,nows the onetime session ,e$BR1B and the person with whom he is sharing this,e$BA. Cf courseB he ta,es care to authenticate Alice usingR1before proceeding an$
further.
P&;LIC 3E) CERTIFICATION:
Cne of the principle features of public ,e$ encr$ption is that it is possible for two entities
to echange secret messages without having to echange secret ,e$s. or eampleB when Alicewants to send a secret message to &obB she simpl$ encr$pts the message with &ob^s public ,e$
and sends the encr$pted message to &ob( she doesn^t need to ,now &ob^s secret "that isB private#
-
8/14/2019 Java Network programs
89/91
,e$B nor does &ob need to ,now her secret ,e$. ThusB public ,e$ cr$ptograph$ obviates the need
for QD= infrastructureB such as Qerberos.
Cf courseB with public ,e$ encr$ptionB the communicating entities still have to echangepublic ,e$s. A user can ma,e its public ,e$ publicl$ available in man$ wa$sB for eampleB b$
posting the ,e$ on the user^s personal Meb pageB placing the ,e$ in a public ,e$ serverB or b$
sending the ,e$ to a correspondent b$ email. A Meb commerce site can place its public ,e$ onits server in a manner such that browsers automaticall$ download the public ,e$ when
connecting to the site. Gouters can place their public ,e$s on public ,e$ serversB thereb$
allowing other networ, entities to retrieve them.
&inding a public ,e$ to a particular entit$ is t$picall$ done b$ a ,ert.f.,at.o* a-thor.ty"=A#Bwhose job it is to validate identities and issue certificates. A =A has the following roles2
1. A =A verifies that an entit$ "a personB a routerB and so on# is who it sa$s it is. There are
no mandated procedures for how certification is done. Mhen dealing with a =AB one must
trust the =A to have performed a suitabl$ rigorous identit$ verification. or eampleB if
Trud$ were able to wal, into the l$b$%ight certificate authorit$ and simpl$ announce0I am Alice0 and receive certificates associated with the identit$ of 0AliceB0 then one
shouldn^t put much faith in public ,e$s certified b$ the l$b$%ight certificate authorit$.Cn the other handB one might "or might notP# be more willing to trust a =A that is part of
a federalor statesponsored program "for eampleB *Utah 4:::+#. Cne can trust the
0identit$0 associated with a public ,e$ onl$ to the etent that one can trust a =A and itsidentit$verification techniFues. Mhat a tangled web of trust we spinP
8. Cnce the =A verifies the identit$ of the entit$B the =A creates a ,ert.f.,atethat binds the
public ,e$ of the identit$ to the identit$. The certificate contains the public ,e$ and
globall$ uniFue identif$ing information about the owner of the public ,e$ "for eampleB ahuman name or an I@ address#. The certificate is digitall$ signed b$ the =A.
3. et us now see how certificates can be used to combat pi??aordering pran,stersB li,eTrud$B and other undesirables. Mhen Alice receives &ob^s orderB she gets &ob^s
certificateB which ma$ be on his Meb pageB in an email messageB or in a certificate
server. Alice uses the =A^s public ,e$ to verif$ that the public ,e$ in the certificate isindeed &ob^s. If we assume that the public ,e$ of the =A itself is ,nown to all "for
eampleB it could be published in a trustedB publicB and well,nown placeB such as The
New York Times,so that it is ,nown to all and cannot be spoofed#B then Alice can be surethat she is indeed dealing with &ob. Illustrates the steps involved in =Amediated public
,e$ encr$ption.
4. &oth the International Telecommunication Union "ITU# and the I-T have developed
standards for certification authorities. ITU K.O5: *ITU 4::H+ specifies an authenticationservice as well as a specific s$nta for certificates. G= 488 describes =Abased ,e$
management for use with secure Internet email. It is compatible with K.O5: but goesbe$ond K.O5: b$ establishing procedures and conventions for a ,e$ management
architecture. Table describes some of the important fields in a certificate.
http://occ.awlonline.com/bookbind/pubbooks/kurose-ross1/chapter8/custom10/deluxe-content.html#utah1999http://occ.awlonline.com/bookbind/pubbooks/kurose-ross1/chapter8/custom10/deluxe-content.html#itu1993http://occ.awlonline.com/bookbind/pubbooks/kurose-ross1/chapter8/custom10/deluxe-content.html#itu1993http://occ.awlonline.com/bookbind/pubbooks/kurose-ross1/chapter8/custom10/deluxe-content.html#utah1999 -
8/14/2019 Java Network programs
90/91
Selected fields in a K.O5: and G= 488 public ,e$ certificate
F.e9d Name Des,r.
-
8/14/2019 Java Network programs
91/91
RES<:
Thus ey d.str.2-t.o* a*d ,ert.f.,at.o* s,hemeswere studied successfull$.