Professional Documents
Culture Documents
Mei-Ling Liu
a s o c k et
s o ck e t s o ck e t
A P I r untime Pro ce s s A Proce s s B A P I r untime
suppo r t suppo r t
r e c e i ve r 's
a dd r e s s
a D a ta g ra m Pa ck e t obje ct
a D a t a gra m Pa ck e t o bje ct
se nd
re ce iv e
a D a t a g ra m S o ck e t a D a ta g ra m S o ck e t
o bje ct o bje ct
o bje ct re f e re n ce
da t a flo w
s e nde r pr o g r am r e c e i ve r pr o g r am
create a datagram socket and
bind it to any local port; create a datagram socket and
place data in a byte array; bind it to a specific local port;
create a datagram packet, specifying create a byte array for receiving the data;
the data array and the receiver's create a datagram packet, specifying
address; the data array;
invoke the send m ethod of the invoke the receive m ethod of the
socket with a reference to the socket with a reference to the
datagram packet; datagram packet;
r e c e i ve
bl o c ki ng r e c e i ve ,
no nbl o c ki ng s e nd
requ es t i n a r e que s t-r e s po nse
se nd
pr o to c o l
r e c e i ve
bloc k ed
respo ns e
se nd
if da ta is s e n t be fo re a co rre s po n din g
re ce iv e o pe ra t io n is is s u e d, th e da ta will
be dis ca rde d by th e ru n tim e s u ppo rt
a n d will n o t be re ce iv e d.
Pro ce s s A P r o ce s s A
P ro ce s s C P r o ce s s C
F ig u re 3 a a c o n n e c t io n le ss
F ig u re 3 b
da t a gr a m so c k e t
public void disconnect( ) Cancel the current connection, if any, from this
socket.
a s tream -m o d e d ata s o c k et
p ro c es s
P1
P2
w r ite o p eratio n
read o p er atio n
... ...
a data str e am
clie n t 1
s e rv e r
co n n e c ti o n
s o ck e t
c o n n ec tio n o p er atio n
d a t a s o ck e t
clie n t 2 s en d /rec eiv e o p er ato n
void setSoTimeout(int timeout) Set a timeout period for blocking so that a read( )
throws SocketException call on the InputStream associated with this Socket
will block for only this amount of time. If the
timeout expires, a java.io.InterruptedIOException
is raised
4 . S e r ve r s e nds r e s po ns e
us ing s d2 . s d1
s d2
5 . W he n the pro to c o l
has c o m ple te d, s e rve r s d1 C lie nt c lo s e s its
c lo s e s s d2 ; s d1 is s o c ke t whe n the
us e d to ac c e pt the pr o to c o l has
ne xt c o nne c tio n c o m ple te d
P1 e s ta blis h e s P2 e s tablis h e s
a loca l s ock e t a loca l s o ck e t
P1 is s u e s a P2 s e n ds a da ta gram
re ce ive o pe ration addre s s e d to P1
to re ce iv e th e
da tag ram .
a cce pt co n n e ct re qu e s t
(f r o m S o c k e t c o n s t r u c t o r )
re a d
w ri te
m e ssage
a n o p e r a ti o n
cl o s e
da ta s o ck e t
pro ce s s e x e cu ti n g
cl o s e
co n n e cti o n s o ck e t
cl o s e s o ck e t pro ce s s s u s pe n de d
• Import javax.net.ssl;
• Class SSLServerSocket is a subclass of ServerSocket,
and inherits all its methods.
• Class SSLSocket is a subclass of Socket, and inherits
all its methods.
• There are also classes for
– Certification
– Handshaking
– KeyManager
– SSLsession