sockets supp
TRANSCRIPT
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 1/45
Network Programming using
sockets
Distributed Software Systems
Prof. Sanjeev Setia
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 2/45
A P I s f o r T C P I P
T C P I P i s a p r o t o c o l d e s i g n e d t o o p e r a t e i n m u l t i -
v e n d o r e n v i r o n m e n t
i n t e r f a c e b e t w e e n T C P I P a n d a p p l i c a t i o n s l o o s e l y
s p e c i e d
a p p l i c a t i o n i n t e r f a c e s
B S D U N I X : s o c k e t i n t e r f a c e
A T & T : T L I i n t e r f a c e
T C P I P s o f t w a r e i n s i d e k e r n e l i n v o k e d b y s y s t e m
c a l l s
U N I X I O f a c i l i t i e s e x t e n d e d w i t h T C P I P s p e c i c
c a l l s
9
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 3/45
T h e S o c k e t I n t e r f a c e
p r o v i d e s f u n c t i o n s t h a t s u p p o r t n e t w o r k
c o m m u n i c a t i o n u s i n g m a n y p o s s i b l e p r o t o c o l s
P F I N E T i s o n e p r o t o c o l f a m i l y s u p p o r t e d b y
s o c k e t s
T C P a n d U D P a r e p r o t o c o l s i n P F I N E T f a m i l y
s o c k e t i s t h e a b s t r a c t i o n f o r n e t w o r k c o m m u n i c a t i o n
a s o c k e t i s i d e n t i e d b y s o c k e t d e s c r i p t o r
s y s t e m d a t a s t r u c t u r e f o r s o c k e t
f a m i l y e . g . , P F I N E T
s e r v i c e e . g . , S O C K S T R E A M
L o c a l I P a d d r e s s , L o c a l P o r t
R e m o t e I P a d d r e s s , R e m o t e P o r t
p a s s i v e s o c k e t : s o c k e t u s e d b y a s e r v e r t o w a i t f o r
i n c o m i n g c o n n e c t i o n s ; a c t i v e s o c k e t : s o c k e t u s e d
b y c l i e n t t o i n i t i a t e a c o n n e c t i o n
1 0
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 4/45
E n d p o i n t A d d r e s s e s
T C P I P p r o t o c o l s d e n e a c o m m u n i c a t i o n e n d p o i n t
t o c o n s i s t o f a n I P a d d r e s s a n d a p r o t o c o l p o r t
n u m b e r
o t h e r p r o t o c o l f a m i l i e s h a v e o t h e r d e n i t i o n s
s o c k e t a b s t r a c t i o n s s u p p o r t s t h e c o n c e p t o f a d d r e s s
f a m i l y w h i c h a l l o w s d i e r e n t p r o t o c o l s t o h a v e t h e i r
o w n a d d r e s s r e p r e s e n t a t i o n s
T C P I P p r o t o c o l s u s e a s i n g l e a d d r e s s
r e p r e s e n t a t i o n w i t h a d d r e s s f a m i l y d e n o t e d b y
A F I N E T
1 1
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 5/45
E
n
d
p
o
i
n
t
A
d
d
r
e
s
s
e
s
c
o
n
t
'
d
s
t
r
u
c
t
u
r
e
f
o
r
A
F
I
N
E
T
a
d
d
r
e
s
s
e
s
s
t
r
u
c
t
s
o
c
k
a
d
d
r
_
i
n
*
s
t
r
u
c
t
t
o
h
o
l
d
a
n
a
d
d
r
e
s
s
*
u
_
c
h
a
r
s
i
n
_
l
e
n
;
*
t
o
t
a
l
l
e
n
g
t
h
*
u
_
s
h
o
r
t
s
i
n
_
f
a
m
i
l
y
;
*
t
y
p
e
o
f
a
d
d
r
e
s
s
*
u
_
s
h
o
r
t
s
i
n
_
p
o
r
t
;
*
p
r
o
t
o
c
o
l
p
o
r
t
n
u
m
b
e
r
*
s
t
r
u
c
t
i
n
_
a
d
d
r
s
i
n
_
a
d
d
r
;
*
I
P
a
d
d
r
e
s
s
*
c
h
a
r
s
i
n
_
z
e
r
o
8
;
*
u
n
u
s
e
d
s
e
t
t
o
z
e
r
o
*
;
i
f
p
r
o
g
r
a
m
u
s
i
n
g
m
i
x
t
u
r
e
o
f
p
r
o
t
o
c
o
l
s
,
p
r
o
g
r
a
m
m
e
r
m
u
s
t
b
e
c
a
r
e
f
u
l
s
i
n
c
e
n
o
t
a
l
l
a
d
d
r
e
s
s
e
s
h
a
v
e
t
h
e
s
a
m
e
s
i
z
e
1
2
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 6/45
S y s t e m C a l l s
s o c k e t
u s e d t o c r e a t e n e w s o c k e t
a r g u m e n t s : p r o t o c o l f a m i l y e . g . P F I N E T ,
p r o t o c o l o r s e r v i c e i . e . , s t r e a m o r d a t a g r a m
r e t u r n s s o c k e t d e s c r i p t o r
c o n n e c t :
c l i e n t c a l l s c o n n e c t t o e s t a b l i s h a n a c t i v e
c o n n e c t i o n t o t h e s e r v e r
a r g u m e n t t o c o n n e c t s p e c i e s r e m o t e e n d p o i n t
w r i t e
s e r v e r s a n d c l i e n t s u s e w r i t e t o s e n d d a t a a c r o s s
a T C P c o n n e c t i o n
a r g u m e n t s : s o c k e t d e s c r i p t o r , a d d r e s s o f d a t a ,
l e n g t h o f d a t a
1 3
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 7/45
S y s t e m C a l l s c o n t ' d
r e a d
u s e d t o r e c e i v e d a t a f r o m a T C P c o n n e c t i o n
a r g u m e n t s : s o c k e t , b u e r , l e n g t h o f b u e r
r e a d b l o c k s i f n o d a t a ; i f m o r e d a t a t h a n t s i n
b u e r , i t o n l y e x t r a c t s e n o u g h t o l l t h e b u e r ;
i f l e s s t h a n b u e r l e n g t h , i t e x t r a c t s a l l t h e d a t a
a n d r e t u r n s n u m b e r o f b y t e s r e a d
r e a d a n d w r i t e c a n a l s o b e u s e d w i t h U D P b u t
d i e r e n t b e h a v i o r
c l o s e : u s e d t o d e a l l o c a t e s o c k e t ; d e l e t e d w h e n l a s t
p r o c e s s t h a t i s u s i n g s o c k e t d o e s a c l o s e
b i n d
u s e d t o s p e c i f y a l o c a l e n d p o i n t a d d r e s s f o r a
s o c k e t
u s e s s o c k a d d r i n s t r u c t u r e
1 4
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 8/45
S y s t e m C a l l s c o n t ' d
l i s t e n
u s e d b y c o n n e c t i o n - o r i e n t e d s e r v e r s t o p u t s o c k e t
i n p a s s i v e m o d e
a r g u m e n t s : s o c k e t , s i z e o f q u e u e f o r s o c k e t
c o n n e c t i o n r e q u e s t s
a c c e p t
c r e a t e s a n e w s o c k e t f o r e a c h c o n n e c t i o n r e q u e s t
r e t u r n s d e s c r i p t o r o f n e w s o c k e t t o i t s c a l l l e r
U D P c a l l s :
s e n d , s e n d t o , s e n d m s g
r e c v , r e c v f r o m , r e c v m s g
1 5
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 9/45
I n t e g e r C o n v e r s i o n
s t a n d a r d r e p r e s e n t a t i o n f o r b i n a r y i n t e g e r s u s e d i n
T C P I P p r o t o c o l h e a d e r s : n e t w o r k b y t e o r d e r , M S B
r s t
e . g . t h e p r o t o c o l p o r t e l d i n s t r u c t s o c k a d d r i n
u s e s n e t w o r k b y t e o r d e r
h o s t ' s i n t e g e r r e p r e s e n t a t i o n m a y b e d i e r e n t
c o n v e r s i o n r o u t i n e s : h t o n s , h t o n l , n t o h l , n t o h s
s h o u l d b e u s e d f o r p o r t a b i l i t y
1 6
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 10/45
C l i e n t S o f t w a r e
c o n c e p t u a l l y s i m p l e r t h a n s e r v e r s b e c a u s e
d o n o t h a v e t o h a n d l e c o n c u r r e n t i n t e r a c t i o n s w i t h
m u l t i p l e s e r v e r s
u s u a l l y n o t p r i v i l e g e d s o f t w a r e
d o n ' t h a v e t o b e
a s c a r e f u l
n o a u t h e n t i c a t i o n , p r o t e c t i o n , e t c .
1 7
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 11/45
L o c a t i n g t h e s e r v e r
s e r v e r ' s I P a d d r e s s a n d p o r t n u m b e r n e e d e d
c a n b e s p e c i e d a s a c o n s t a n t i n t h e p r o g r a m
h a v e t h e u s e r s p e c i f y i t a s a n a r g u m e n t w h e n
i n v o k i n g c l i e n t
r e a d f r o m a l e o n d i s k
u s e a p r o t o c o l t o n d t h e s e r v e r e . g . a b r o a d c a s t
m e s s a g e t o w h i c h s e r v e r s r e s p o n d
1 8
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 12/45
P a r s i n g a d d r e s s a r g u m e n t
a d d r e s s a r g u m e n t t y p i c a l l y i s a h o s t n a m e l i k e
c s . g m u . e d u o r I P a d d r e s s i n d o t t e d d e c i m a l n o t a t i o n
l i k e 1 2 9 . 1 7 4 . 2 9 . 3 4
n e e d t o s p e c i f y a d d r e s s u s i n g s t r u c t u r e s o c k a d d r i n
l i b r a r y r o u t i n e s i n e t a d d r a n d g e t h o s t b y n a m e u s e d
f o r c o n v e r s i o n s
s t r u c t h o s t e n t
c h a r * h n a m e ;
c h a r * * h _ a l i a s e s ;
i n t h _ a d d r t y p e ;
i n t h _ l e n g t h ;
c h a r * * h _ a d d r _ l i s t ;
;
d e f i n e h _ a d d r h _ a d d r _ l i s t 0 ;
1 9
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 13/45
E X A M P L E :
s t r u c t h o s t e n t * h p t r ;
c h a r * n a m e = ` ` c s . g m u . e d u ' ' ;
i f h p t r = g e t h o s t b y n a m e n a m e
* I P a d d r e s s i s i n h p t r - h _ a d d r *
e l s e
* h a n d l e e r r o r *
i n e t a d d r c o n v e r t s d o t t e d d e c i m a l I P a d d r e s s i n t o
b i n a r y
2 0
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 14/45
C l i e n t S o f t w a r e c o n t ' d
l o o k i n g u p a w e l l k n o w n p o r t b y n a m e
s t r u c t s e r v e n t d e n e d i n n e t d b . h i n t h e s a m e w a y a s s t r u c t
h o s t e n t
s t r u c t s e r v e n t * s p t r ;
i f s p t r = g e t s e r v b y n a m e ` ` s m t p ' ' , ` ` t c p ' '
* p o r t n u m b e r i s n o w i n s p t r - s _ p o r t *
e l s e
* h a n d l e e r r o r *
N O T E : g e t s e r v b y n a m e r e t u r n s p r o t o c o l p o r t i n
n e t w o r k b y t e o r d e r
2 1
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 15/45
C l i e n t S o f t w a r e c o n t ' d
l o o k i n g u p a p r o t o c o l b y n a m e
s t r u c t p r o t o e n t d e n e d i n n e t d b . h
s t r u c t p r o t o e n t * p p t r ;
i f p p t r = g e t p r o t o b y n a m e ` ` u d p ' '
* o f f i c i a l p r o t o c o l n u m b e r i s i n p p t r - p _ p r o t o
e l s e
* h a n d l e e r r o r *
2 2
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 16/45
T C P c l i e n t a l g o r i t h m
1 . F i n d I P a d d r e s s a n d p r o t o c o l n u m b e r o f s e r v e r
2 . a l l o c a t e a s o c k e t
3 . s p e c i f y t h a t t h e c o n n e c t i o n n e e d s a n a r b i t r a r y ,
u n u s e d p r o t o c o l p o r t o n l o c a l m a c h i n e a n d a l l o w
T C P t o s e l e c t o n e
4 . C o n n e c t t h e s o c k e t t o t h e s e r v e r
5 . C o m m u n i c a t e w i t h t h e s e r v e r u s i n g a p p l i c a t i o n - l e v e l
p r o t o c o l
6 . C l o s e t h e c o n n e c t i o n
2 3
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 17/45
T C P c l i e n t c o n t ' d
A l l o c a t i n g a s o c k e t
i n c l u d e s y s t y p e s . h
i n c l u d e s y s s o c k e t . h
i n t s ; * s o c k e t d e s c r i p t o *
s = s o c k e t P F _ I N E T , S O C K _ S T R E A M , 0 ;
C h o o s i n g a l o c a l p o r t n u m b e r
c o n i c t s h a v e t o b e a v o i d e d
h a p p e n s a s a s i d e - e e c t t o c o n n e c t c a l l
c h o o s i n g a l o c a l I P a d d r e s s
a p r o b l e m f o r h o s t s c o n n e c t e d t o m u l t i p l e
n e t w o r k s
c h o s e n a u t o m a t i c a l l y b y T C P I P a t t i m e o f
c o n n e c t i o n
2 4
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 18/45
C o n n e c t i n g a T C P s o c k e t t o a s e r v e r
r e t c o d e = c o n n e c t s , r e m a d d r , r e m a d d r l e n
c o n n e c t p e r f o r m s f o u r t a s k s
1 . t e s t s s p e c i e d s o c k e t i s v a l i d a n d n o t a l r e a d y
c o n n e c t e d
2 . l l s i n r e m o t e a d d r e s s i n s o c k e t f r o m s e c o n d
a r g u m e n t
3 . c h o o s e s a l o c a l e n d p o i n t a d d r e s s f o r s o c k e t i f i t
d o e s n o t h a v e o n e
4 . i n i t i a t e s a c o n n e c t i o n a n d r e t u r n s v a l u e t o t h e
c a l l e r
2 5
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 19/45
C o m m u n i c a t i n g w i t h t h e s e r v e r u s i n g
T C P : E x a m p l e
d e f i n e B L E N 1 2 0
c h a r * r e q = ` ` r e q u e s t o f s o m e s o r t ' ' ;
c h a r b u f B L E N ;
c h a r * b p t r ;
i n t n ;
i n t b u f l e n ;
b p t r = b u f ;
b u f l e n = B L E N ;
* s e n d r e q u e s t *
w r i t e s , r e q , s t r l e n r e q ;
* r e a d r e s p o n s e m a y c o m e i n s e v e r a l p i e c e s *
w h i l e n = r e a d s , b p t r , b u f l e n 0
b p t r + = n ;
b u f l e n - = n ;
2 6
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 20/45
C l o s i n g a T C P c o n n e c t i o n
p a r t i a l c l o s e n e e d e d b e c a u s e c l i e n t m a y n o t k n o w
w h e n a l l t h e d a t a f r o m t h e s e r v e r h a s a r r i v e d a n d
s e r v e r m a y n o t k n o w i f c l i e n t w i l l s e n d a n o t h e r
r e q u e s t
s h u t d o w n c a l l
e r r c o d e = s h u t d o w n s , d i r e c t i o n ;
d i r e c t i o n = 0 : n o f u r t h e r i n p u t , 1 : n o f u r t h e r
o u t p u t , 2 : s h u t d o w n i n b o t h d i r e c t i o n s
2 7
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 21/45
P r o g r a m m i n g a U D P c l i e n t
1 . F i n d I P a d d r e s s a n d p r o t o c o l n u m b e r o f s e r v e r
2 . A l l o c a t e a s o c k e t
3 . S p e c i f y t h a t t h e c o n n e c t i o n n e e d s a n a r b i t r a r y ,
u n u s e d p r o t o c o l p o r t o n l o c a l m a c h i n e a n d a l l o w
U D P t o s e l e c t o n e
4 . S p e c i f y t h e s e r v e r t o w h i c h m e s s a g e s m u s t b e s e n t
5 . C o m m u n i c a t e w i t h t h e s e r v e r u s i n g a p p l i c a t i o n - l e v e l
p r o t o c o l
6 . C l o s e t h e s o c k e t
2 8
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 22/45
C o n n e c t e d a n d U n c o n n e c t e d U D P
s o c k e t s
w i t h U D P , c o n n e c t e d s o c k e t s d o n o t m e a n a
c o n n e c t i o n " w a s e s t a b l i s h e d
c o n n e c t e d s o c k e t s s e r v e r s p e c i e d o n c e
u n c o n n e c t e d s o c k e t s s e r v e r s p e c i e d e a c h t i m e
r e a d a n d w r i t e : m e s s a g e t r a n s f e r N O T s t r e a m s
c l o s e d o e s n o t i n f o r m r e m o t e e n d p o i n t o f a n y a c t i o n s
U D P i s u n r e l i a b l e
2 9
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 23/45
E x a m p l e s
T C P a n d U D P c l i e n t s f o r s e r v i c e s
D A Y T I M E
T I M E
E C H O
c o n n e c t T C P a n d c o n n e c t U D P p r o c e d u r e s i n v o k e
c o n n e c t s o c k
3 0
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 24/45
I s s u e s i n S e r v e r D e s i g n
C o n c u r r e n t v s i t e r a t i v e s e r v e r s : h a n d l e m u l t i p l e
r e q u e s t s c o n c u r r e n t l y o r o n e a f t e r t h e o t h e r ?
C o n n e c t i o n - o r i e n t e d v s c o n n e c t i o n - l e s s s e r v e r s :
T C P o r U D P ?
S t a t e f u l v s s t a t e l e s s s e r v e r s
1
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 25/45
I t e r a t i v e , c o n n e c t i o n - o r i e n t e d s e r v e r
A l g o r i t h m
1 . C r e a t e a s o c k e t a n d b i n d t o t h e w e l l - k n o w n
a d d r e s s f o r t h e s e r v i c e b e i n g o e r e d
2 . P l a c e t h e s o c k e t i n p a s s i v e m o d e
3 . A c c e p t t h e n e x t c o n n e c t i o n r e q u e s t f r o m t h e
s o c k e t , a n d o b t a i n a n e w s o c k e t f o r t h e
c o n n e c t i o n
4 . R e p e a t e d l y r e a d a r e q u e s t f r o m t h e c l i e n t ,
f o r m u l a t e a r e s p o n s e , a n d s e n d a r e p l y b a c k t o
t h e c l i e n t a c c o r d i n g t o t h e a p p l i c a t i o n p r o t o c o l
5 . W h e n n i s h e d w i t h a p a r t i c u l a r c l i e n t , c l o s e t h e
c o n n e c t i o n a n d r e t u r n t o s t e p 3 t o a c c e p t a n e w
c o n n e c t i o n
s e r v e r s s h o u l d s p e c i f y I N A D D R A N Y a s i n t e r n e t
a d d r e s s w h i l e b i n d i n g
n e e d e d f o r h o s t s w i t h m u l t i p l e I P a d d r e s s e s
2
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 26/45
I t e r a t i v e , c o n n e c t i o n - l e s s s e r v e r s
A l g o r i t h m
1 . C r e a t e a s o c k e t a n d b i n d t o t h e w e l l - k n o w n
a d d r e s s f o r t h e s e r v i c e b e i n g o e r e d
2 . R e p e a t e d l y r e a d t h e n e x t r e q u e s t f r o m a c l i e n t ,
f o r m u l a t e a r e s p o n s e , a n d s e n d a r e p l y b a c k t o
t h e c l i e n t a c c o r d i n g t o t h e a p p l i c a t i o n p r o t o c o l
c a n n o t u s e c o n n e c t u n l i k e c l i e n t s
u s e s e n d t o a n d r e c v f r o m
3
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 27/45
C o n c u r r e n t , C o n n e c t i o n - l e s s s e r v e r s
A l g o r i t h m
M a s t e r 1 . C r e a t e a s o c k e t a n d b i n d t o t h e w e l l -
k n o w n a d d r e s s f o r t h e s e r v i c e b e i n g o e r e d .
L e a v e t h e s o c k e t u n c o n n e c t e d .
M a s t e r 2 . R e p e a t e d l y c a l l r e c v f r o m t o r e c e i v e t h e
n e x t r e q u e s t f r o m a c l i e n t , a n d c r e a t e a n e w s l a v e
t h r e a d p r o c e s s t o h a n d l e t h e r e s p o n s e
S l a v e 1 . R e c e i v e a s p e c i c r e q u e s t u p o n c r e a t i o n
a s w e l l a s a c c e s s t o t h e s o c k e t
S l a v e 2 . F o r m a r e p l y a c c o r d i n g t o t h e a p p l i c a t i o n
p r o t o c o l a n d s e n d i t b a c k t o t h e c l i e n t u s i n g
s e n d t o
S l a v e 3 . E x i t
c o s t o f p r o c e s s t h r e a d c r e a t i o n f o r e a c h c l i e n t
r e q u e s t
w h i l e u s i n g t h r e a d s , u s e t h r e a d - s a f e f u n c t i o n s a n d
b e c a r e f u l w h i l e p a s s i n g a r g u m e n t s t o t h r e a d s
4
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 29/45
A p p a r e n t c o n c u r r e n c y u s i n g a s i n g l e
p r o c e s s
m u l t i p l e p r o c e s s e s n e e d t o u s e s h a r e d m e m o r y
I P C f a c i l i t i e s i f d a t a s t r u c t u r e s s h a r e d a m o n g
p r o c e s s e s
c r e a t i n g p r o c e s s e s c a n b e e x p e n s i v e
t h r e a d s m a k e t h i s e a s i e r
c a n a l s o a c h i e v e t h e s a m e g o a l u s i n g a s i n g l e p r o c e s s
a n d a s y n c h r o n o u s I O u s i n g s e l e c t
6
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 30/45
A p p a r e n t c o n c u r r e n c y u s i n g a s i n g l e
p r o c e s s
A l g o r i t h m
1 . C r e a t e a s o c k e t a n d b i n d t o t h e w e l l - k n o w n p o r t
f o r t h e s e r v i c e . A d d t h e s o c k e t t o t h e l i s t o f t h o s e
o n w h i c h I O i s p o s s i b l e
2 . U s e s e l e c t t o w a i t f o r I O o n e x i s t i n g s o c k e t s
3 . I f o r i g i n a l s o c k e t i s r e a d y , u s e a c c e p t t o o b t a i n
t h e n e x t c o n n e c t i o n , a n d a d d t h e n e w s o c k e t t o
t h e l i s t o f t h o s e o n w h i c h I O i s p o s s i b l e
4 . I f s o m e s o c k e t o t h e r t h a n t h e o r i g i n a l i s r e a d y , u s e
r e a d t o o b t a i n t h e n e x t r e q u e s t , f o r m a r e s p o n s e ,
a n d u s e w r i t e t o s e n d t h e r e s p o n s e b a c k t o t h e
c l i e n t
5 . C o n t i n u e p r o c e s s i n g w i t h s t e p 2 .
7
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 31/45
T h e P r o b l e m o f S e r v e r D e a d l o c k
i t e r a t i v e s e r v e r : s u p p o s e c l i e n t c r e a t e s a c o n n e c t i o n
b u t d o e s n o t s e n d a n y r e q u e s t s
s u p p o s e c l i e n t d o e s n o t c o n s u m e r e s p o n s e s
c o n n e c t i o n - o r i e n t e d s e r v e r s w i l l b l o c k o n w r i t e i f
l o c a l b u e r f u l l d e a d l o c k i n s i n g l e p r o c e s s s e r v e r s
8
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 32/45
M u l t i - p r o t o c o l S e r v e r D e s i g n
m u l t i p r o t o c o l s e r v e r h a n d l e s s e r v i c e r e q u e s t s o v e r
b o t h U D P a n d T C P
M o t i v a t i o n : a l l o w s t h e u s e o f s h a r e d c o d e f o r s e r v i c e
a s y n c h r o n o u s I O n e e d e d s e l e c t s y s t e m c a l l
d e s i g n c a n b e i t e r a t i v e o r c o n c u r r e n t m u l t i - p r o c e s s
o r s i n g l e - p r o c e s s
9
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 33/45
M u l t i - s e r v i c e S e r v e r D e s i g n
s i n g l e s e r v e r f o r m u l t i p l e s e r v i c e s
M o t i v a t i o n : c o n s e r v e s y s t e m r e s o u r c e s a n d m a k e
m a i n t e n a n c e e a s i e r
D e s i g n : I t e r a t i v e , c o n c u r r e n t , o r s i n g l e p r o c e s s
c o n c u r r e n t
C o n n e c t i o n - l e s s o r C o n n e c t i o n - o r i e n t e d
M u l t i - s e r v i c e , M u l t i - p r o t o c o l s u p e r s e r v e r s " , e . g .
U N I X i n e t d
S t a t i c o r d y n a m i c s e r v e r c o n g u r a t i o n
1 0
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 34/45
U N I X i n e t d s u p e r s e r v e r
c o n g u r a t i o n l e e t c i n e t d . c o n f
e n t r i e s : s e r v i c e n a m e f r o m e t c s e r v i c e s
,
s o c k e t t y p e , p r o t o c o l , w a i t s t a t u s , u s e r i d , s e r v e r
p r o g r a m , a r g u m e n t s
1 1
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 35/45
Java sockets API
• TCP socket classes
– Socket
– ServerSocket
– InetAddress
• UDP classes – DatagramPacket
– DatagramSocket
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 37/45
A TCP Client for the Echo service
impor t java.io.*;impor t java.net.*;
public class EchoClient {pub lic static void main(String[] args) throws IOExcept ion {
Socket echoSocket = null ;Print Writ er out = null;Buf feredReader in = null ;
try {echoSocket = new Socket("taranis", 7);out = new PrintWriter(echoSocket.getOutputStream(), true);in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));} catch (UnknownHostException e) {
System.err .print ln ("Don't know about host : taranis.");System.exit (1);
} catch (IOExcept ion e) {System.err .print ln ("Couldn't get I/ O for "
+ "the connection t o: taranis.");
System.exit (1);}
BufferedReader stdIn = new BufferedReader(new InputSt reamReader(System.in));
St ring userInput ;while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);System.out .pr int ln ("echo: " + in.readLine());
}out.close();in.close();stdIn.close();echoSocket.close();
}}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 38/45
A TCP Cli ent f or t he Dayt im e serv ice
import java.net.*;
import java.io.*;
public class DayClient1 {publ ic static final int DAYTIME_PORT = 13;String host ;Socket s;
public stat ic void main(String args[]) throws
IOException {DayClient1 that = new DayClient1(args[0]);that.go();
}
public DayClient1(String host ) {this.host = host;
}
public void go() throws IOException {s = new Socket(host, DAYTIME_PORT);BufferedReader i = new BufferedReader(
new InputStreamReader(s.get InputStream()));System.out.println(i.readLine());i.close();s.close();
}}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 39/45
A TCP Server f or t he Dayt im e servi ce
import java.io.*;
import java.net.*;import java.util.*;
public class DayServer1 {private ServerSocket ss;publ ic static final int DAYTIME_PORT = 13;
public stat ic void main(String args[]) throws
IOException {DayServer1 d = new DayServer1();d.go();
}
public void go() throws IOException {Socket s = null ;ss = new ServerSocket(DAYTIME_PORT, 5);
for (;;) {s = ss.accept();BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream(),"8859_1"))out .writ e("Java Daytime server: " +
(new Date()). toString() + "\ n");out.close();s.close();
}}
}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 40/45
A Mul t it hr eaded TCP server
public class Mult iServe implements Runnable {private ServerSocket ss;
public stat ic void main(String args[]) throws Except ion {Mult iServe m = new Mult iServe();m.go();
}
pub lic void go() throws Except ion {ss = new ServerSocket(DayClient 2.DAYTIME_PORT, 5);Thread t1 = new Thread(this, "1");
Thread t2 = new Thread(this, "2");Thread t3 = new Thread(this, "3");t1.start (); t 2.start (); t 3.start ();
}
pub lic void run() {Socket s = null;BufferedWrit er out = null ;St ring myname = Thread.cur rent Thread().getName();
for (;;) {try {
System.out.print ln ("thread " + myname + " about to accept ..");s = ss.accept();System.out.print ln ("thread " + myname +
" accept ed a connection");out = new BufferedWriter(
new Output StreamWriter(s.getOutputStream()));out .writ e(myname + " " + new Date());
Thread.sleep(10000);out .write("\ n");out.close();
}catch (Except ion e) {
e.printStackTrace();}
}}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 41/45
Anot her Mul t i-t hr eaded Serv er Exam ple
impor t java.net.*;impor t java.io.*;
publ ic class KKMult iServer {pub lic static void main(String[] args) throws IOExcept ion {
ServerSocket serverSocket = nul l;boolean listening = true;
try {serverSocket = new ServerSocket(4444);
} catch (IOExcept ion e) {
System.err .print ln ("Could not listen on port : 4444 .");System.ex it (-1);}
while (listening)new KKMult iServerThread( serverSocket.accept()).start ();
serverSocket.close();}
}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 42/45
impor t java.net.*;impor t java.io.*;
public class KKMultiServerThread extends Thread {
private Socket socket = null;
publ ic KKMult iServerThread(Socket socket ) {super("KKMultiServerThread");this.socket = socket ;
}
pub lic void run() {
try {PrintWrit er out = new PrintWrit er (socket .getOutputStream(), t rueBufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;KnockKnockProtocol kkp = new KnockKnockProt ocol ();out put Line = kkp.processInput(null);out.println(outputLine);
while (( inputLine = in.readLine()) != null) {outputLine = kkp.processInput(inputLine);out.println(outputLine);if (outputLine.equals("Bye"))
break;}out.close();in.close();
socket.close();
} catch (IOExcept ion e) {e.printStackTrace();
}}
}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 43/45
A UDP Client
impor t java.io.*;
impor t java.net.*;impor t java.util.*;
public class QuoteClient {pub lic static void main(String[] args) throws IOExcept ion {
if (args.lengt h != 1) {System.out .pr int ln ("Usage: java Quot eClient <hostname>");return;
}
/ / get a datagram socketDatagramSocket socket = new DatagramSocket ();
/ / send requestbyte[] buf = new byte[256 ];InetAddress address = InetAddress.getByName (args[0]);DatagramPacket packet =
new DatagramPacket(buf, buf .length, address, 44
socket.send(packet);
/ / get responsepacket = new DatagramPacket(buf, buf .length);socket.receive(packet);
/ / display responseStrin g received = new String(packet .getData(), 0);System.out.pr int ln ("Quot e of the Moment : " + received);
socket.close();}
}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 44/45
A UDP Quo te Serv er
impor t java.io.*;
public class QuoteServer {pub lic static void main(String[] args) throws IOExcept ion {
new QuoteServerThread().start();}
}
impor t java.io.*;impor t java.net.*;
impor t java.util.*;
public class QuoteServerThread extends Thread {
protected DatagramSocket socket = null;protected BufferedReader in = null;prot ected boolean moreQuot es = t rue;
publ ic Quot eServerThread() throws IOExcept ion {this("QuoteServerThread");
}
publ ic Quot eServerThread(String name) throws IOExcept ion {super(name);socket = new DatagramSocket (4445);
try {in = new BufferedReader(new FileReader("one-liners.txt"));
} catch (FileNot FoundExcept ion e) {
System.err .print ln ("Could not open quote file. Serving t imeinstead.");
}}
8/3/2019 Sockets Supp
http://slidepdf.com/reader/full/sockets-supp 45/45
public void run() {
while (moreQuotes) {try {
byte[] buf = new byte[256];
/ / receive requestDatagramPacket packet = new DatagramPacket (buf, buf .length);socket.receive(packet);
/ / figure out responseStr ing dStr ing = null ;if (in == null)
dString = new Date().toString();else
dStr ing = getNext Quote();buf = dString.getBytes();
/ / send t he response to the client at "address" and "port "InetAddress address = packet .getAddress();int port = packet.getPort();packet = new DatagramPacket(buf, buf.length, address, port);socket.send(packet);
} catch (IOException e) {e.printStackTrace();
moreQuotes = false;}
}socket.close();
}
protected String getNextQuote() {String returnValue = null;try {
if ((returnValue = in.readLine()) == null) {in.close();
moreQuotes = false;returnValue = "No more quotes. Goodbye.";}
} catch (IOException e) {returnValue = "IOExcept ion occurred in server.";
}