tcp-ip sockets in java - practical guide for programmers

Upload: jorge-centeno-borge

Post on 03-Jun-2018

246 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    1/120

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    2/120

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    3/120

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    4/120

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    5/120

    reface

    For years, college courses in com pute r ne tworking were taugh t with little or no hands on expe-rience. For various reasons, including some good ones, instructors approached the principlesof computer networking primarily through equations, analyses, and abstract descriptions ofprotocol stacks. Textbooks might have included code, but it would have been unconnected toanything students could get their hands on. We believe, however, that students learn betterwhen they can see and then build) concrete exam ples of the princi ples at work. And, for-tunately, things have changed. The Internet has become a part of everyday life, and accessto its services is readily available to mos t st ude nts and their programs ). Moreover, copiousexa mpl esu goo d and bad- -of nontrivial software are freely available.

    We wrote this book for the same reason we wrote TCP IP Sockets in C: we needed aresource to support learning networking through programming exercises in our courses. Ourgoal is to provide a sufficient introductio n so that st udent s can get their hands on real networkservices without too much hand-holding. After grasping the basics, students can then move onto more advanced as signments, which support learning about routing algorithms, multimediaprotocols, medium access control, and so on. We have tried to make this book equivalent toour earlier book to enable instructors to allow students to choose the language they use andstill ensure that all studen ts will come away with the same skills and underst andin g. Of course,it is not clear that this goal is achievable, but in any case the scope, price, and presentationlevel of the book are intended to be similar.

    I n te n d e d u d i e n c eThis book is aimed primarily at studen ts in upper-division underg radua te or graduate coursesin compu ter netw orks. It is inte nded as a sup plem ent to a traditional textbook t hat explains theproblems and principles of computer networks. At the same time, we have tried to make the

    x

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    6/120

    Preface m

    b o o k r e a s o n a b l y s e l f - c o n t a i n e d ( ex c e p t f o r t h e a s s u m e d p r o g r a m m i n g b a c k g r o u n d ) , s o t h a t itc a n a l so b e u s e d , f o r e x a m p l e , in c o u r s e s o n o p e r a t i n g s y s t e m s o r d i s t r i b u t e d c o m p u t i n g . F o ru s e s o u t s i d e t h e c o n t e x t o f a n e t w o r k i n g c o u r s e , i t w i ll b e h e l p f u l if t h e s t u d e n t s h a v e s o m ea c q u a i n t a n c e w i t h t h e b a s ic c o n c e p t s o f n e t w o r k i n g a n d T C P /I P.T h i s b o o k s o t h e r t a r g e t a u d i e n c e c o n s i s t s o f p r a c t i t i o n e r s w h o k n o w J a v a a n d w a n t t ol e a r n a b o u t w r i t i n g J a v a a p p l i c a t i o n s t h a t u s e T C P / I P . T h i s b o o k s h o u l d t a k e s u c h u s e r s f a re n o u g h t h a t t h e y c a n s t a r t e x p e r i m e n t i n g a n d l e a r n i n g o n t h e i r o w n . R e a d e r s a r e a s s u m e dt o h a v e a c c e s s t o a c o m p u t e r e q u i p p e d w i t h J av a. T h is b o o k i s b a s e d o n V e r s i o n 1 .3 o f J a v aa n d t h e J a v a V i r t u a l M a c h i n e ( JV M ); h o w e v e r , t h e c o d e s h o u l d w o r k w i t h e a r l i e r v e r s i o n s o fJ a v a , w i t h t h e e x c e p t i o n o f a f e w n e w J a v a m e t h o d s . J a v a i s a b o u t p o r t a b i l i t y , s o t h e p a r t i c u l a rh a r d w a r e a n d o p e r a t i n g s y s t e m (O S) o n w h i c h y o u r u n s h o u l d n o t m a t t e r .

    pproachC h a p t e r 1 p r o v i d e s a g e n e ra l o v e r v i ew o f n e t w o r k i n g c o n c e p t s . I t i s n o t , b y a n y m e a n s , a c o m -p l e t e i n t ro d u c t i o n , b u t r a t h e r i s i n t e n d e d t o al lo w r e a d e r s t o s y n c h r o n i z e w i t h t h e c o n c e p t s a n dt e r m i n o l o g y u s e d t h r o u g h o u t t h e b o o k . C h a p t e r 2 i n t r o d u c e s t h e m e c h a n i c s o f s im p l e c l ie n t sa n d s e r v e r s ; t h e c o d e i n t h i s c h a p t e r c a n s e r v e a s a s t a r t i n g p o i n t f o r a v a r i e t y o f e x e r c i s e s .C h a p t e r 3 co v e r s t h e b a s i c s o f m e s s a g e c o n s t r u c t i o n a n d p a r s i n g . T h e r e a d e r w h o d i g e s t s t h ef i r st t h r e e c h a p t e r s s h o u l d i n p r i n c i p l e b e a b l e t o i m p l e m e n t a c l i e n t a n d s e r v e r f o r a g i v e n( s im p l e ) a p p l i c a t i o n p r o t o c o l. C h a p t e r 4 t h e n d e a l s w i t h t e c h n i q u e s t h a t a r e n e c e s s a r y w h e nb u i l d i n g m o r e s o p h i s t i c a t e d a n d r o b u s t c l ie n t s a n d s e r v er s . F in a ll y, i n k e e p i n g w i t h o u r g o a lo f i l l u s t ra t i n g p r in c i p l e s t h r o u g h p r o g r a m m i n g , C h a p t e r 5 d i s c u s s e s t h e r e l a t io n s h i p b e t w e e nt h e p r o g r a m m i n g c o n s t r u c t s a n d t h e u n d e r l y i n g p r o t o c o l i m p l e m e n t a t i o n s i n s o m e w h a t m o r ed e t a i l .

    O u r ge n e r al a p p r o a c h i n t r o d u c e s p r o g r a m m i n g c o n c e p t s t h r o u g h s i m p le p r o g r a m e x a m -p l e s a c c o m p a n i e d b y li n e- b y -l in e c o m m e n t a r y t h a t d e s c r i b e s t h e p u r p o s e o f e v e r y p a r t o f t h ep r o g r a m . T h i s le t s y o u s e e th e i m p o r t a n t o b j e c t s a n d m e t h o d s a s t h e y a re u s e d i n c o n te x t . A sy o u l o o k a t th e c o d e , y o u s h o u l d b e a b l e to u n d e r s t a n d t h e p u r p o s e o f e a c h a n d e v e r y l in e .

    J a v a m a k e s m a n y t h i n g s e a s i er , b u t i t d o e s n o t s u p p o r t s o m e f u n c t i o n a l i t y t h a t isc o m m o n l y a s s o c i a t e d w i t h t h e C /U N I X s o c k e t s i n t e r f a c e ( a s y n c h r o n o u s I / O , s e le c t ( ) - st y lem u l t i p l e x i n g ) . I n C a n d C + +, t h e s o c k e t i n t e r f a c e is a g e n e r i c a p p l i c a t i o n p r o g r a m m i n g i n t e r f a c e(A P I) f o r a l l t y p e s o f p r o t o c o l s , n o t j u s t T C P / I P . J a v a s s o c k e t c l a s s e s , o n t h e o t h e r h a n d , b yd e f a u l t w o r k e x c l u s i v e l y w i t h T C P a n d U D P o v e r I Pv 4 . I r o n ic a l ly , t h e r e d o e s n o t s e e m t o b ea n y t h i n g i n t h e Ja v a s p e c if i c a ti o n o r d o c u m e n t a t i o n t h a t r e q u i r e s t h a t a n i n s t a n c e o f t h e S o c k etc l a s s u s e T C P , o r t h a t a D a t ag r am S o e k e t i n s t a n c e u s e U D P. N e v e r t h e l e s s , t h i s b o o k a s s u m e s t h i st o b e t h e c a s e , a s i s t r u e o f c u r r e n t i m p l e m e n t a t i o n s .

    O u r e x a m p l e s d o n o t t a k e a d v a n t a g e o f a ll l i b r a r y f ac i l it i e s in Ja v a . S o m e o f t h e s e f a c il i ti e s ,i n p a r t i c u l a r s e r i al i z a ti o n , e f f ec t iv e l y r e q u i r e t h a t a ll c o m m u n i c a t i n g p e e r s b e i m p l e m e n t e d i nJ a v a. A l s o, t o i n t r o d u c e e x a m p l e s a s s o o n a s p o s s i b l e , w e w a n t e d t o a v o i d b r i n g i n g i n a t h i c k e t o fm e t h o d s a n d c l a s s e s t h a t h a v e t o b e s o r t e d o u t la t e r . W e h a v e t r i e d to k e e p i t s i m p l e , e s p e c i a l l yi n t h e e a r l y c h a p t e r s .

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    7/120

    Wha t Th is Book I s No t x

    W h a t T h i s o o k Is N o tT o k e e p t h e p r ic e o f th i s b o o k w i t h i n a r e a s o n a b l e r a n g e f o r a s u p p l e m e n t a r y t e xt , w e h a v eh a d t o l im i t it s s c o p e a n d m a i n t a i n a ti g h t f o c u s o n t h e g o a l s o u t l in e d a b o v e. W e o m i t t e d m a n yt o p i cs a n d d i r ec t io n s , s o it i s p r o b a b l y w o r t h m e n t i o n i n g s o m e o f th e t h i n g s t h i s b o o k i s n o t :

    I t i s n o t a n i n t r o d u c t i o n t o Ja v a . W e fo c u s s p e c i f ic a l ly o n T C P / I P s o c k e t p r o g r a m m i n gu s i n g t h e J a v a l a n g u a g e . W e e x p e c t t h a t t h e r e a d e r i s a l r e a d y a c q u a i n t e d w i t h t h e l a n g u a g ea n d b a s i c J a v a l ib r a r ie s ( e s p e c i a ll y I /O ) , a n d k n o w s h o w t o d e v e l o p p r o g r a m s i n J a va .

    I t i s n o t a b o o k o n p r o t o c o l s . R e a d i n g t h i s b o o k w i ll n o t m a k e y o u a n e x p e r t o n IP , T C P ,F T P , H T T P , o r a n y o t h e r e x i s t i n g p r o t o c o l ( e x c e p t m a y b e t h e e c h o p r o t o c o l ) . O u r f o c u s i so n t h e i n t e r f a c e t o t h e T C P / I P s e r v i c e s p r o v i d e d b y t h e s o c k e t a b s t r a c t i o n . ( It w i ll h e l p i fy o u s t a r t w i t h s o m e i d e a a b o u t tl~e g e n e r a l w o r k i n g s o f T C P a n d IP , b u t C h a p t e r 1 m a yb e a n a d e q u a t e s u b s t it u t e .)

    I t i s n o t a g u i d e t o a ll o f J a v a ' s r ic h c o l l e c t i o n o f l ib r a r i e s t h a t a r e d e s i g n e d t o h i d ec o m m u n i c a t i o n d e t a i l s ( e. g., H T T PC o nn ec ti on ) a n d m a k e t h e p r o g r a m m e r ' s l i fe e a s i e r.S in c e w e a r e te a c h i n g t h e f u n d a m e n t a l s o f h o w t o d o, n o t h o w t o a v o i d d o i n g, p r o t o c o ld e v e l o p m e n t , w e d o n o t c o v e r t h e s e p a r t s o f t h e A PI. W e w a n t r e a d e r s t o u n d e r s t a n dp r o t o c o l s i n t e r m s o f w h a t g o e s o n t h e w ir e, s o w e m o s t l y u s e s i m p l e b y t e s t r e a m s a n dd e a l w i t h c h a r a c t e r e n c o d i n g s e x p l ic i tl y . A s a c o n s e q u e n c e , t h i s t e x t d o e s n o t d e a l w i t hU R L U R L C o n n e c t i o n a n d s o o n. W e b e l ie v e t h a t o n c e y o u u n d e r s t a n d t h e p r in c i p l e s, u s i n gt h e s e c o n v e n i e n c e c l a s s e s w il l b e s t r a i g h t f o r w a r d . T h e n e t w o r k - r e l e v a n t c l a s s e s t h a t w ed o c o v e r i n c l u d e I n e t A d d r e s s , S o c k e t, S e r v e r S o c k e t , D a t a g r am P a c k e t , D a t a g ra m S o e k e t, a n dU u l t i c a s t S o c k e t .

    I t i s n o t a b o o k o n o b j e c t - o r i e n t e d d e s i g n. O u r f o c u s is o n t h e i m p o r t a n t p r i n c i p l e s o fT C P / I P s o c k e t p r o g r a m m i n g , a n d o u r e x a m p l e s a r e i n t e n d e d t o i l lu s t r a t e t h e m c o n c i se l y .A s fa r a s p o s s ib l e , w e t r y t o a d h e r e t o o b j e c t - o r i e n t e d d e s i g n p r i n c ip l e s ; h o w e v e r , w h e nd o i n g s o a d d s c o m p l e x i t y th a t o b f u s c a t e s t h e s o c k e t p r i n c i p l e s o r b l o a t s t h e c o d e, w es a c ri f ic e d e s i g n f o r c la r i ty . T h i s t e x t d o e s n o t c o v e r d e s i g n p a t t e r n s f o r n e t w o r k i n g .( T h o u g h w e w o u l d l i k e t o t h i n k t h a t it p r o v i d e s s o m e o f th e b a c k g r o u n d n e c e s s a r y f o ru n d e r s t a n d i n g s u c h p a t t e r n s )I t i s n o t a b o o k o n w r i t i n g p r o d u c t i o n - q u a l i t y c o d e . A g a i n, t h o u g h w e s t r i v e f o r r o b u s t n e s s ,t h e p r i m a r y g o a l o f o u r c o d e e x a m p l e s i s e d u c a t i o n . I n o r d e r t o a v o i d o b s c u r i n g t h ep r i n c i p le s w i t h l a r g e a m o u n t s o f e r r o r - h a n d l i n g c o d e , w e h a v e s a c ri f ic e d s o m e r o b u s t n e s sf o r b r e v i t y a n d c l a ri t y .

    I t i s n o t a b o o k o n d o i n g y o u r o w n n a t i v e s o c k e t s i m p l e m e n t a t i o n i n Ja v a. W e f o c u se x c lu s i v el y o n T C P / I P s o c k e t s a s p r o v i d e d b y t h e s t a n d a r d J a v a d i s t r i b u t i o n a n d d o n o tc o v e r th e v a r i o u s s o c k e t i m p l e m e n t a t i o n w r a p p e r c l a s s e s ( e.g ., S o ck e tl m p l) .

    T o a v o i d c l u t te r i n g t h e e x a m p l e s w i t h e x t r a n e o u s ( n o n s o c k e t - r e l a t e d p r o g r a m m i n g ) c o d e,w e h a v e m a d e t h e m c o m m a n d - l i n e b a s e d . W h i l e t h e b o o k ' s W e b s it e, www.mkp.com/practical/javasockets c o n t a i n s a f e w e x a m p l e s o f G U I - e n h a n c e d n e t w o r k a p p l i c a t io n s ,w e d o n o t i n c l u d e o r e x p l a i n t h e m i n t h i s t e x t .

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    8/120

    l l Preface u

    I t i s n o t a b o o k o n J a v a a p p l e t s . A p p l e t s u s e t h e s a m e J a v a n e t w o r k i n g A P I s o t h e c o m m u -n i c a t i o n c o d e s h o u l d b e v e r y s i m i la r ; h o w e v e r , t h e r e a r e s e v e r e s e c u r i t y re s t r i c t i o n s o nt h e k i n d s o f c o m m u n i c a t i o n a n a p p l e t c a n p e r f o r m . W e p r o v i d e a v e ry l i m i te d d i s c u s s io no f th e s e r e s t r i c t i o n s a n d a s in g l e a p p l e t / a p p l i c a t i o n e x a m p l e o n t h e W e b s it e; h o w e v e r ,a c o m p l e t e d e s c r i p t i o n o f a p p l e t n e t w o r k i n g i s b e y o n d t h e s c o p e o f t hi s t ex t .T h i s b o o k w il l n o t m a k e y o u a n e x p e r t - - t h a t t a k e s y e a r s o f e x p e ri e n c e . H o w e v e r , w e h o p e

    i t w i ll b e u s e f u l a s a r e so u r c e , e v e n t o t h o s e w h o a l r e a d y k n o w q u i te a bi t a b o u t u s i n g s o c k e t si n J a v a . B o t h o f u s e n j o y e d w r i t i n g i t a n d l e a r n e d q u i t e a b i t a l o n g t h e w a y .

    cknowledgmen tsW e w o u l d l i ke t o t h a n k a ll t h e p e o p l e w h o h e l p e d m a k e t h is b o o k a re a li ty . D e s p i t e t h e b o o k ' sb r e v i ty , m a n y h o u r s w e n t i n t o r e v i e w i n g th e o r ig i n a l p r o p o s a l a n d t h e d r a f t, a n d t h e r e v i e w e r s 'i n p u t h a s s i g n i f i c a n tl y s h a p e d t h e f i n a l r e s u l t.

    F ir st , t h a n k s t o th o s e w h o m e t i c u l o u s l y r e v i e w e d t h e d r a f t o f t h e t e x t a n d m a d e s u g g e s -t i o n s f o r i m p r o v e m e n t . T h e s e i n c l u d e M i ch el B a rb e a u , C a r l t o n U n i v er s it y ; C h r i s E d m o n d s o n -Y u r k a n a n , U n i v e r s i t y o f T e x a s a t A u s t i n , T e d H e r m a n , U n i v e r s i t y o f I o w a ; D a v e H o l l i n g e r,R e n s s e l a e r P o l y t e c n i c I n s t i t u t e ; J i m L e o n e , R o c h e s t e r I n s t i t u t e o f T e c h n o l o g y ; D a n S c h m i d t ,T e x a s A M U n i v e r s i ty ; E ri c k W a g n e r , E D S; a n d C S I 4 3 21 , S p r i n g 2 0 0 1 . A n y e r r o r s t h a t r e m a i na r e, o f c o u r se , o u r r e s p o n s i b i l it y . W e a r e v e r y i n t e r e s t e d i n w e e d i n g o u t s u c h e r r o r s i n f u t u r ep r i n t i n g s s o if y o u f i n d o n e , p l e a s e e m a i l e i t h e r o f u s . W e w i l l m a i n t a i n a n e r r a t a l i s t o n t h eb o o k ' s W e b p a g e .

    F in a ll y, w e a r e g r a t e f u l t o t h e f o l k s a t M o r g a n K a u f m a r m . T h e y c a r e a b o u t q u a l i ty a n dw e a p p r e c i a t e t h a t . W e e s p e c i a ll y a p p r e c i a t e t h e e f f o r t s o f K a r y n J o h n s o n , o u r e d i t o r , a n d M e iL e v e n s o n , o u r p r o d u c t i o n c o o r d i n a t o r .

    FeedbackW e i n vi te y o u r s u g g e s t i o n s f o r th e i m p r o v e m e n t o f a n y a s p e c t o f t h i s b o o k . Y o u c a n s e n df e e d b a c k v ia t h e b o o k ' s W e b p a g e , www.mkp.com/practical/javasockets o r y o u c a n e m a i l u s a tt h e a d d r e s s e s b e l o w :K e n n e t h L . C a l v e r t [email protected] i c h a e l J . D o n a h o o [email protected]

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    9/120

    c h a p t e rIntroduction

    M i l l i o n s o f c o m p u t e r s a ll o v e r t h e w o r l d a re n o w c o n n e c t e d t o t h e w o r l d w i d e n e t w o r kk n o w n a s t h e In t e rn e t . T he I n te r n e t e n a b le s p r o g r a m s r u n n i n g o n c o m p u t e r s t h o u s a n d s o fm i l es a p a r t t o c o m m u n i c a t e a n d e x c h a n g e i n f o r m a t i o n . I f y o u h a v e a c o m p u t e r c o n n e c t e d t o an e t w o rk , y o u m a y h a v e u s e d a W e b b r o w s e r - - a t y pi ca l p r o g r a m t h a t m a k e s u s e o f th e I n t er n e t.W h a t d o e s s u c h a p r o g r a m d o t o c o m m u n i c a t e w i t h o t h e r s o v e r a n e t w o r k ? T h e a n s w e r v a ri e sw i t h t h e a p p l i c a t i o n a n d t h e o p e r a t i n g s y s t e m (O S), b u t a g r e a t m a n y p r o g r a m s g e t a c ce s s t on e t w o r k c o m m u n i c a t i o n s e r v ic e s t h r o u g h t h e s o c k e t s a p p l i c a ti o n p r o g r a m m i n g i n t e r f a c e (A PI).T h e g o a l o f t h i s b o o k i s t o g e t y o u s t a r t e d w r i t i n g J a v a p r o g r a m s t h a t u s e t h e s o c k e t s A P I.

    B e f o r e d e l v i n g i n t o t h e d e t a i l s o f t h e A PI, i t i s w o r t h t a k i n g a b r i e f l o o k a t t h e b i g p i c t u r eo f n e t w o r k s a n d p r o t o c o l s t o s e e h o w a n AP I f o r T r a n s m i s s i o n C o n t r o l P r o t o c o l / I n t e r n e tP r o t o c o l fi ts in . O u r g o a l h e r e is n o t t o t e a c h y o u h o w n e t w o r k s a n d T C P / I P w o r k - - m a n y f in et e x t s a r e a v a i la b l e fo r t h a t p u r p o s e [2 , 4 , 11 , 1 6, 2 2 J - - b u t r a t h e r t o i n t r o d u c e s o m e b a s i cc o n c e p t s a n d t e r m i n o l o g y .

    1.1 N e t w o r k s P a c k e t s a n d P r o t o c o l sA c o m p u t e r n e t w o r k c o n s i s t s o f m a c h i n e s i n t e r c o n n e c t e d b y c o m m u n i c a t i o n c h a n n el s . W ec al l t h e s e m a c h i n e s hosts a n d routers. H o s t s a re c o m p u t e r s t h a t r u n a p p l i c a ti o n s s u c h a s y o u rW e b b r o w s e r . T h e a p p l i c a t i o n p r o g r a m s r u n n i n g o n h o s t s a r e r e a ll y t h e u s e r s o f t h e n e t w o r k .R o u t e r s a r e m a c h i n e s w h o s e j o b i s t o r e la y , o r forward, i n f o r m a t io n f r o m o n e c o m m u n i c a t io nc h a n n e l t o a n o t h e r . T h e y m a y r u n p r o g r a m s b u t t y p i c a ll y d o n o t r u n a p p l i c a t i o n p r o g r a m s . F o ro u r p u r p o s e s , a communication channel is a m e a n s o f c o n v e y i n g s e q u e n c e s o f b y t e s f r o m o n eh o s t t o a n o t h e r ; i t m a y b e a b r o a d c a s t t e c h n o l o g y l ik e E t h e r n e t , a d i a l- u p m o d e m c o n n e c t i o n ,o r s o m e t h i n g m o r e s o p h i s t i c a te d .

    R o u t e r s a r e i m p o r t a n t s i m p l y b e c a u s e i t i s n o t p r a c t i c a l t o c o n n e c t e v e r y h o s t d i r e c t lyt o e v er y o t h e r h o s t . I n s t e a d , a fe w h o s t s c o n n e c t t o a r o u t e r , w h i c h c o n n e c t s t o o t h e r r o u t e r s ,a n d s o o n to f o r m t h e n e t w o r k . T h i s a r r a n g e m e n t l e ts e a c h m a c h i n e g e t b y w i t h a r e la t iv e l y

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    10/120

    2 Ch apter 1: Intro duc tion []

    I L ] AW Channel(e.g., Ethe rnet) " I1 ( IP ] ' ~ C h a n n e l "~L d p ]Host Router HostF i g u r e 1 1 : A T CP/IP network.

    s m a ll n u m b e r o f c o m m u n i c a t i o n c h a n n e ls ; m o s t h o s t s n e e d o n l y o n e. P r o g r a m s t h a t e x c h a n g ei n f o r m a t i o n o v e r th e n e t w o r k , h o w e v e r , d o n o t i n te r a c t d i r e c t ly w i t h r o u t e r s a n d g e n e r a l l yr e m a i n b l i s s f u l ly u n a w a r e o f t h e ir e x i s t en c e .

    By information w e m e a n s e q u e n c e s o f b y t e s t h a t a re c o n s t r u c t e d a n d i n t e r p r e t e d b y p r o -g r a m s . I n t h e c o n t e x t o f c o m p u t e r n e t w o r k s , t h e s e b y t e s e q u e n c e s a r e g e n e ra l l y c a ll e d packets.A p a c k e t c o n t a i n s c o n t r o l i n f o r m a t i o n t h a t t h e n e t w o r k u s e s t o d o i ts j o b a n d s o m e t i m e s a l s oi n c l u d e s u s e r d a t a . A n e x a m p l e i s i n f o r m a t i o n i d e n t i f y i n g t h e p a c k e t ' s d e s t i n a t i o n . R o u t e r su s e s u c h c o n t r o l i n f o r m a t i o n t o f i g u r e o u t h o w t o f o r w a r d e a c h p a c k e t .A protocol i s a n a g r e e m e n t a b o u t t h e p a c k e t s e x c h a n g e d b y c o m m u n i c a t i n g p r o g r a m sa n d w h a t t h e y m e a n . A p r o t o c o l te ll s h o w p a c k e t s a r e s t r u c t u r e d - - f o r e x a m p l e , w h e r e t h ed e s t i n a t i o n i n f o r m a t i o n i s l o c a te d i n t h e p a c k e t a n d h o w b i g it i s m a s w e l l as h o w t h e i n fo r -m a t i o n i s t o b e i n t e r p r e t e d . A p r o t o c o l i s u s u a l l y d e s i g n e d t o s o lv e a s pe c if ic p r o b l e m u s i n gg i v e n c a p a b i l it i e s . F o r e x a m p l e , t h e Hyp erText Transfer Protocol HTTP) s o l ve s t h e p r o b l e m o ft r a n s f e r r i n g h y p e r t e x t o b j e c t s b e t w e e n s e r v e r s , w h e r e t h e y a r e s to r e d , a n d W e b b r o w s e r s t h a tm a k e t h e m a v a i l a b l e t o h u m a n u s e r s .

    I m p l e m e n t i n g a u s e f u l n e t w o r k r e q u i r e s t h a t a l a rg e n u m b e r o f d i f f e re n t p r o b l e m s b es o l v e d . T o k e e p t h i n g s m a n a g e a b l e a n d m o d u l a r , d i f f e r e n t p r o t o c o l s a r e d e s i g n e d t o s o l v ed i f f e r e n t s e t s o f p r o b l e m s . T C P / I P i s o n e s u c h c o l l e c t i o n o f s o l u t i o n s , s o m e t i m e s c a l l e d aprotocol suite. I t h a p p e n s t o b e t h e s u i te o f p r o t o c o l s u s e d i n th e I n t e r n e t, b u t i t c a n b e u s e d i ns t a n d - a l o n e p r i v a t e n e t w o r k s a s w e ll. H e n c e f o r t h w h e n w e t a l k a b o u t t h e n e t w o r k , w e m e a na n y n e t w o r k t h a t u s e s t h e T C P /I P p r o t o c o l s u i te . T h e m a i n p r o t o c o l s i n t h e T C P / IP s u i t e a r et h e I n t e r n e t P r o t o c o l ( IP ), t h e T r a n s m i s s i o n C o n t r o l P r o t o c o l ( T C P ), a n d t h e U s e r D a t a g r a mProtocol (UDP) .

    I t t u r n s o u t t o b e u s e f u l t o o r g a n i z e p r o t o c o l s i n t o layers; T C P / I P a n d v i r t u a l l y a l lo t h e r p r o t o c o l s u i t e s a r e o r g a n i z e d t h i s w a y . F i g u re 1 .1 s h o w s t h e r e l a t i o n s h i p s a m o n g t h ep r o t o c o l s , a p p l i c a t i o n s , a n d t h e s o c k e t s AP I i n t h e h o s t s a n d r o u t e r s , a s w e l l a s th e f l o wo f d a t a f r o m o n e a p p l i c a t i o n ( u s i n g T C P ) t o a n o t h e r . T h e b o x e s l a b e l e d T C P , U D P , a n d I Pr e p r e s e n t i m p l e m e n t a t i o n s o f t h o s e p r o t o c o l s . S u c h i m p l e m e n t a t i o n s t y p ic a l ly r e s i d e i n t h e

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    11/120

    [] 1.2 About Addresses

    operating syste m of a host. Applications access the services provid ed by UDP and TCP throug hthe sock ets API. The arro w depicts the flow of data fr om the application , t hr ou gh t he TCP and IPimplementations, through the network, and back up through the IP and TCP implementationsat the other end.In TCP/IP, the bottom layer consists of the underlying communication channelsnforexample, Ethernet or dial-up modem connections. Those channels are used by the ne tworklayer, which deals with the prob lem of forwardin g packets toward their desti natio n (i.e., whatrouters do). The single network layer protocol in the TCP/IP suite is the Internet Protocol; itsolves the problem of making the sequence of channels and routers between any two hostslook like a single host-to-host channel.

    The Internet Protocol provides a d a t a g r a m service: every packet is handled and deliveredby the network independently, like letters or parcels sent via the postal system. To make thiswork, each IP packet has to contain the address of its destination, just as every package thatyou mail is address ed to somebody. (We ll say more about addres ses shortly.) Althoug h mostdelivery comp anie s gu aran tee delivery of a package, IP is only a best-eff ort proto col: it att emp tsto deliver each packet, but it can (and occasionally does) lose, reorder, or duplicate packets intransit throu gh the network.

    The layer above IP is called the transpo rt layer . It offers a choice between two protocols:TCP and UDP. Each builds on the service provided by IP, but they do so in different ways toprovide different kinds of transport, which are used by application protocols with differentneeds. TCP and UDP have one function in common: addressing. Recall that IP delivers packetsto hosts; clearly, a finer granularity of addressing is needed to get a packet to a particularapplication, perhaps one of many using the network on the same host. Both TCP and UDPuse addresses, called por t numbers , to identify applications within hosts. They are called end-to-end tr anspo rt protocols because they carry data all the way from one program to another(whereas IP only carries data from one host to another).

    TCP is designed to detect and recover from the losses, duplications, and other errorsthat may occur in the h ost-to -host channel pr ovided by IP. TCP provides a reliable byte-s treamchannel, so that applications do not have to deal with these problems. It is a connection-oriented protocol: before using it to communicate, two programs must first establish a TCPconnection, which involves completing an exchange of handshake messages between the TCPimpl ementa tion s on the two commu nicat ing co mputers . Using TCP is also similar in many waysto file in pu t/ ou tp ut (I/O). In fact, a file that is written by one pro gra m and read by anot her is areasonable model of communi cati on over a TCP connection. UDP, on the ot her hand, doesnot atte mpt to recover from err ors experienced by IP; it simply extends the IP best-effortdatagram service so that it works between application programs instead of between hosts.Thus, applications that use UDP must be prepared to deal with losses, reordering, and so on.

    1 .2 A b o u t A d d r e s s e sWhen you mail a letter, you provide the address of the recipient in a form that the postalservice can understand. Before you can talk to someone on the phone, you must supply theirnumber to the telephone system. In a similar way, before a program can communicate with

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    12/120

    Cha pter 1: Introd uctio n I

    a n o t h e r p r o g r a m , i t m u s t t e ll t h e n e t w o r k w h e r e t o f in d t h e o t h e r p r o g r a m . I n T C P /I P , i t t a k e st w o p i e c e s o f i n f o r m a t i o n t o id e n t i f y a p a r t i c u l a r p r o g r a m : a n Internet address, u s e d b y IP, a n da port number, t h e a d d i t i o n a l a d d r e s s i n t e r p r e t e d b y t h e t r a n s p o r t p r o t o c o l ( T CP o r U D P).

    I n t e r n e t a d d r e s s e s a r e 3 2 - bi t b i n a r y n u m b e r s . 1 I n w r i t i n g d o w n I n t e r n e t a d d r e s s e s f o rh u m a n c o n s u m p t i o n ( as o p p o s e d t o u s i n g t h e m i n s id e a p p l ic a t io n s ) , w e ty p i ca l ly s h o w t h e ma s a s tr i n g o f f o u r d e c i m a l n u m b e r s s e p a r a t e d b y p e r i o d s ( e.g ., 1 0 .1 .2 .3 ); t h i s is c a ll e d t h edotted-quad n o t a t i o n . T h e fo u r n u m b e r s i n a d o t t e d - q u a d s t r in g r e p r e s e n t t h e c o n t e n t s o f t h ef o u r b y t e s o f t h e I n t e r n e t a d d r e s s - - t h u s , e a c h is a n u m b e r b e t w e e n 0 a n d 2 5 5.

    O n e s p e c ia l IP a d d r e s s w o r t h k n o w i n g i s t h e loopback address, 1 2 7 . 0 . 0 . 1 . T h i s a d d r e s si s a l w a y s a s s i g n e d t o a s p e c i a l loopback interface, w h i c h s i m p l y e ch o e s t r a n s m i t t e d p a c k e t sr i g h t b a c k t o t h e s e n d e r . T h e l o o p b a c k i n t e r f a c e i s v e r y u s e f u l f o r t e s ti n g ; i t c a n b e u s e d e v e nw h e n a c o m p u t e r i s n o t c o n n e c t e d t o t he n e t w o r k .

    T e c h n ic a ll y , e a c h I n t e r n e t a d d r e s s r e f e r s t o t h e c o n n e c t i o n b e t w e e n a h o s t a n d a nu n d e r l y i n g c o m m u n i c a t i o n c h a n n e l , s u c h a s a d i al - up m o d e m o r E t h e r n e t c a rd . B e c au s e e a c hs u c h n e t w o r k c o n n e c t i o n b e l o n g s t o a s in g le h o s t , a n I n t e r n e t a d d r e s s i d e n ti f ie s a h o s t a sw e l l a s i t s c o n n e c t i o n t o t h e n e t w o r k . H o w e v e r , b e c a u s e a h o s t c a n h a v e m u l t i p l e p h y s i c a lc o n n e c t i o n s t o th e n e t w o r k , o n e h o s t c a n h a v e m u l t i p l e I n t e r n e t a d d r e s s e s .

    T h e p o r t n u m b e r i n TC P o r U D P i s a l w a y s i n t e r p r e t e d r e l a t i v e t o a n I n t e r n e t a d d r e s s .R e t u r n i n g t o o u r ea r li e r a n a lo g i e s, a p o r t n u m b e r c o r r e s p o n d s t o a r o o m n u m b e r a t a g i v ens t r e e t a d d r e s s , s a y, t h a t o f a l a r g e b u i l d i n g . T h e p o s t a l s e r v ic e u s e s t h e s t r e e t a d d r e s s t o g e t t h el e t t e r t o a m a i l b o x ; w h o e v e r e m p t i e s t h e m a i l b o x is t h e n r e s p o n s i b l e f o r g e t t i n g t h e l e t t e r to t h ep r o p e r r o o m w i t h i n th e b ui l di n g . O r c o n s i d e r a c o m p a n y w i t h a n in t e r n a l t e l e p h o n e s y s t e m :t o s p e a k t o a n in d i v i d u a l in th e c o m p a n y , y o u f i rs t di al th e c o m p a n y ' s m a i n p h o n e n u m b e r t oc o n n e c t t o th e i n t e r n a l t e l e p h o n e s y s t e m a n d t h e n d ia l t h e e x t e n s i o n o f t h e p a r ti c u l a r t e l e p h o n eo f t h e i n d i v i d u a l t h a t y o u w i s h t o s p e a k w i th . I n t h e s e a n a l o g i e s , t h e I n t e r n e t a d d r e s s i s t h es t re e t a d d r e s s o r t h e c o m p a n y ' s m a i n n u m b e r , w h e r e a s t h e p o r t c o r r e s p o n d s t o t h e r o o mn u m b e r o r t e l e p h o n e e x t e n si o n . P or t n u m b e r s a r e 1 6- bi t u n s i g n e d b i n a r y n u m b e r s , s o e a c ho n e i s i n t h e r a n g e 1 t o 6 5 , 5 3 5 ( 0 i s r e s e r v e d ) .

    1 .3 A b o u t N a m e sM o s t l ik e ly y o u a r e a c c u s t o m e d t o r e fe r r i n g t o h o s t s b y name ( e . g . , h o s t . e x a m p l e . c o m ) . H o w -e v er , t h e I n t e r n e t p r o to c o l s d e a l w i t h n u m e r i c a l a d d r e s s e s , n o t n a m e s . Y o u s h o u l d u n d e r s t a n dt h a t t h e u s e o f n a m e s i n s t e a d o f a d d r e s s e s i s a c o n v e n i e n c e f e a t u r e t h a t i s i n d e p e n d e n t o ft h e b a s i c s e rv i c e p r o v i d e d b y T C P / I P - - y o u c a n w r i t e a n d u s e T C P /I P a p p l i c a t i o n s w i t h o u t e v e r

    1Throughout th is book the te rm Internet address re fe rs to the addresses used wi th the cur ren t ve rs ion o fIP, which is version 4 [12] . Because i t is expected t hat a 32-bit add ress space wil l be inad equ ate for futu reneeds , a new vers ion o f IP has bee n def ined [5 ]; i t p rov ides the same se rvice bu t has m uch b igger In te rne tadd ress es (128 bits). IPv6, as the new vers ion is known, has not be en widely deployed; the socke ts API wil lrequ i re some changes to dea l wi th i t s much la rger addres ses [6] .

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    13/120

    [] 1.4 Clients an d Servers

    u s i n g a n a m e . W h e n y o u u s e a n a m e t o i d e n t i f y a c o m m u n i c a t i o n e n d p o i n t , t h e s y s t e m h a s t od o s o m e e x t r a w o r k t o resolve t h e n a m e i n t o a n a d d r e s s .

    T h i s e x t r a s t e p i s o f t e n w o r t h i t, f o r a c o u p l e o f r e a s o n s . F i r s t, n a m e s a r e g e n e r a l l ye a s ie r f o r h u m a n s t o r e m e m b e r t h a n d o t t e d - q u a d s . S e c o n d , n a m e s p r o v i d e a l ev e l o f i n di -r e ct io n , w h i c h i n s u l a t e s u s e r s f r o m IP a d d r e s s c h a n g e s . D u r i n g t h e w r i t in g o f t h is b o o k , t h eW e b s e r v e r f o r t h e p u b l i s h e r o f t h i s t ex t, M o r g a n K a u f m a n n , c h a n g e d I n t e r n e t a d d r e s s e sf r o m 2 0 8 . 1 6 4 . 1 2 1 . 4 8 t o 2 1 6 . 2 0 0 . 1 4 3 . 1 2 4 . H o w e v e r , b e c a u s e w e r e f e r t o th a t W e b s e r v e r a swww.mkp.com ( cl ea rl y m u c h e a s i e r t o r e m e m b e r t h a n 2 0 8 . 1 6 4 . 1 2 1 . 4 8 ) a n d b e c a u s e t h e c h a n g eis r e f le c t e d i n t h e s y s t e m t h a t m a p s n a m e s t o a d d r e s s e s www.mkp.com n o w r e s o l v e s t o t h en e w I n t e r n e t a d d r e s s i n s t e a d o f 2 0 8 .1 6 4 . 1 2 1 . 4 8 ), t h e c h a n g e i s t r a n s p a r e n t t o p r o g r a m s t h a tu s e t h e n a m e t o a c c e s s t h e W e b s e r v e r .

    T h e n a m e - r e s o l u t i o n s e rv i c e ca n a c c e ss i n f o r m a t i o n f r o m a w i d e v a r ie t y o f s o u r c e s . T w oo f t h e p r i m a r y s o u r c e s a r e th e Domain Nam e System DNS) a n d l o c a l c o n f i g u r a t i o n d a t a b a s e s .T h e D N S [ 9 ] i s a d i s t r i b u t e d d a t a b a s e t h a t m a p s domain names s u c h a s www.mkp.com toI n t e r n e t a d d r e s s e s a n d o t h e r i n f o r m a t i o n ; t h e DN S p r o t o c o l [ 10 ] a l lo w s h o s t s c o n n e c t e d t ot h e I n t e r n e t t o r e tr i e v e i n f o r m a t i o n f r o m t h a t d a t a b a s e u s i n g T C P o r U D P. L o c al c o n f i g u r a t i o nd a t a b a s e s a r e g e n e r a l l y O S - sp e ci fi c m e c h a n i s m s f o r l o ca l n a m e - t o - I n t e r n e t a d d r e s s m a p p i n g s .

    1 .4 C l i e n t s a n d S e r v e r sI n o u r p o s t a l a n d t e l e p h o n e a n a l o g ie s , e a c h c o m m u n i c a t i o n i s i n i t ia t e d b y o n e p a r t y , w h o s e n d sa l e t te r o r m a k e s t h e t e l e p h o n e c all , w h i le t h e o t h e r p a r t y r e s p o n d s t o t h e i n it i a to r ' s c o n t a c t b ys e n d i n g a r e t u r n l e t t e r o r p i c k i n g u p t h e p h o n e a n d t al k in g . I n t e r n e t c o m m u n i c a t i o n i s si m il ar .T h e t e r m s client a n d server r e f e r t o t h e s e r o l e s : T h e c l i e n t p r o g r a m i n i t i a t e s c o m m u n i c a t i o n ,w h i le t h e s e r v e r p r o g r a m w a i t s p a s s i v e ly f o r a n d t h e n r e s p o n d s t o c li e n ts t h a t c o n t a c t it.T o g e t h e r , t h e c l ie n t a n d s e r v e r c o m p o s e t h e application. T h e t e r m s client a n d server a r ed e s c r i p t iv e o f th e t y p i c a l s i t u a t io n i n w h i c h t h e s e r v e r m a k e s a p a r t i c u l a r c a p a b i l i t y - - f o re x a m p l e , a d a t a b a s e s e r v i c e - - a v a i l a b l e t o a n y c li e n t t h a t i s a b l e t o c o m m u n i c a t e w i t h it .

    W h e t h e r a p r o g r a m is a c ti n g a s a c l ie n t o r s e r v e r d e t e r m i n e s t h e g e n e r a l f o r m o f i tsu s e o f t h e s o c k e t s A PI t o e s t a b l is h c o m m u n i c a t i o n w i t h i ts peer. ( T h e c l i e n t i s t h e p e e r o f t h es e r v e r a n d v i ce v e r s a. ) B e y o n d t h a t , t h e c l i e n t -s e r v e r d i s t i n c t i o n i s i m p o r t a n t b e c a u s e t h e c l i e n tn e e d s t o k n o w t h e s e r v e r ' s a d d r e s s a n d p o r t i n i ti a ll y , b u t n o t v ic e v e r s a . W i t h t h e s o c k e t s A P I,t h e s e r v e r c a n , i f n e c e s s a r y , l e a r n t h e c l i e n t ' s a d d r e s s i n f o r m a t i o n w h e n i t r e c e i v e s t h e i n it i alc o m m u n i c a t i o n f r o m t h e c li en t . T hi s is a n a l o g o u s t o a t e l e p h o n e c a l l - -i n o r d e r t o b e c al le d , ap e r s o n d o e s n o t n e e d t o k n o w t h e t e l e p h o n e n u m b e r o f t h e c al le r. A s w i t h a t e l e p h o n e c all ,o n c e t h e c o n n e c t i o n i s e s t a b l i s h e d , t h e d i s t i n c t i o n b e t w e e n s e r v e r a n d c l ie n t d i s a p p e a r s .

    H o w d o e s a c l ie n t f in d o u t a s e r v e r ' s IP a d d r e s s a n d p o r t n u m b e r ? U s u a ll y , t h e c li e n tk n o w s t h e n a m e o f t h e s e r v e r i t w a n t s m f o r e x a m p l e , f r o m a Universal Resource Locator URL)s u c h a s http://www.mkp.com--and u s e s t h e n a m e - r e s o l u t i o n s e r v i ce to l e a r n th e c o r r e s p o n d i n gI n t e r n e t a d d r e s s .

    F i n d i n g a s e r v e r ' s p o r t n u m b e r i s a d i f f e r e n t s t o r y . In p r i n c i p l e , s e r v e r s c a n u s e a n y p o r t ,b u t t h e c l i e n t m u s t b e a b l e t o l e a r n w h a t i t i s. I n t h e I n t e r n e t , t h e r e i s a c o n v e n t i o n o f a s s i g n i n gw e l l - k n o w n p o r t n u m b e r s t o c e rt a i n ap p l i c a t io n s . T h e In t e r n e t A s s i g n e d N u m b e r A u t h o r i t y

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    14/120

    6 Chapter 1: Introduction u

    (IANA) oversees this assignment. For example, port number 21 has been assigned to the FileTra ns fer Protocol FTP). When you run an FTP client application, it tries to contact the FTPserver on that port by default. A list of all the assigned port numbers is maintained by thenumbering authority of the Internet (see h t t p : / / w w w . i a n a . o r g / a s s i g n m e n t s / p o r t - n u m b e r s ) .

    1 . 5 W h a t Is a S o c k e t ?A socke t is an abstraction through which an application may send and receive data, in muchthe same way as an open file handle allows an application to read and write data to stablestorage. A socket allows an application to plug in to the network and commun icate wi th otherapplications that are plugged in to the same network. Information written to the socket byan application on one machine can be read by an application on a different machine and viceversa.

    Different types of sockets cor respo nd to different underlyin g protocol suites and differentstacks of protocols within a suite. This book deals only with the TCP/IP protocol suite. Themai n types of sockets in TCP/IP tod ay are s t r eam socke ts and data gra m socke ts . Stream socketsuse TCP as the end-to-end protocol (with IP underneath) and thus provide a reliable byte-stream service. A TCP/IP stream socket represents one end of a TCP connection. Datagramsockets use UDP (again, with IP und ern eath ) and th us pr ovide a best-effo rt da tag ram service thatapplications can use to send individual messages up to about 65,500 bytes in length. Streamand datagram sockets are also supported by other protocol suites, but this book deals onlywith TCP stream sockets and UDP datagram sockets. A TCP/IP socket is uniquely identified byan Inter net addres s, an end-to -end pro toc ol (TCP or UDP), and a por t num ber. As you proce ed,you will encounter several ways for a socket to become bound to an address.

    Figure 1.2 depicts the logical relationships among applications, socket abstractions,protocols, and port numbers within a single host. Note that a single socket abstraction canbe referenced by multiple application programs. Each program that has a reference to aparticular socket can communicate through that socket. Earlier we said that a port identifiesan application on a host. Actually, a port identifies a socket on a host. From Figure 1.2, we seethat multiple programs on a host can access the same socket. In practice, separate programsthat access the same socket would usually belong to the same application (e.g., multiple copiesof a Web server program), althou gh in principle they could belong to different applications.

    1 . 6 E x e r c i s e s1. Can you thi nk of a real-life example of commu nic ati on that do es not fit the client-server

    model?2. To how many different kinds of network s is your home connected? How many sup por t

    two-way transport ?

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    15/120

    [] 1.6 Exe rcises

    Appl icat ions

    TCP sockets

    TCP po r ts 1

    , - ... .. '~ . . . . .~. uDpSOCketsocketsreferences

    . . . . . . . Sockets b o u n d to p o r ts

    5535 U D Ppor t sUDP

    ( IP )F i g u r e 1 2 : Sockets , protocols , and por ts .

    3 . IP i s a b e s t - e f f o r t p r o t o c o l , r e q u i r i n g t h a t i n f o r m a t i o n b e b r o k e n d o w n i n t o d a t a g r a m s ,w h i c h m a y b e l o s t , d u p l i c a t e d , o r r e o r d e r e d . T C P h i d e s a ll o f t h is , p r o v i d i n g a r e li a b les e rv i ce t h a t t a k e s a n d d e l iv e r s a n u n b r o k e n s t r e a m o f b y t e s. H o w m i g h t y o u g o a b o u tp r o v i d i n g T C P s e r v ic e o n t o p o f IP ? W h y w o u l d a n y b o d y u s e U D P w h e n T C P i s a v a i la b l e ?

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    16/120

    c h a p t e r 2Basic S ock ets

    Y o u are now ready to learn about writing your own socket applications. We begin bydemonstrating how Java applications identify network hosts. Then, we describe the creationof TCP and UDP clients and servers. Java provides a clear distinction between using TCP andUDP, defining a separate set of classes for bot h protoco ls, so we treat each separately.

    2.1 Socket A ddres sesIP uses 32-bit binary addresses to identify communicating hosts. A client must specify theIP address of the host running the server program when it initiates communication; thenetwork infrastructure uses the 32-bit des t ina t ion addres s to route the client's informationto the proper machine. Addresses can be specified in Java using a string that contains ei-ther the dotted-quad representation of the numeric address (e.g., 169.1.1.1) or a name (e.g.,server .example .corn) . Java encapsulates the IP addresses abstraction in the InetAddress classwhich provides three static methods for creating lnetAddress instances, getByName() andgetAllByName () take a name or IP add res s and r et urn the c orr esp ond ing InetAddress instance(s).For example, InetAddress.getByName("192.168.75.13") returns an instance identifying the IPaddres s 192.168.75.13. The third m ethod , getLocalHost (), re turn s an InetAddres s instance con-taining the local hos t add ress. Our first pr og ra m example, InetAddressExample. java, demon -strates the use of InetAddress. The progr am takes a list of name s or IP addre sses as command-line parameters and prints the name and an IP address of the local host, followed by namesand IP addresses of the hosts specified on the command line.

    I n e t A d d r e s s E x a m p i e . ja v a0 i m p o r t j a v a . n e t . * ; / / f o r I n e t A d d r e s s2 p u b l i c c l a s s I n e t A d d r e s s E x a m p l e {3

    9

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    17/120

    ! O Ch apt er 2: Basic Sockets []

    789

    10111213141516171819202122232425262728293O

    p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) {/ / G e t n a m e a n d I P a d d r e s s o f t h e l o c a l h o s tt r y {

    I n e t A d d r e s s a d d r e s s = I n e t A d d r e s s . g e t L o c a l H o s t ( ) ;S y s t e m . o u t . p r i n t l n ( " L o c a l H o s t : " ) ;S y s t e m . o u t . p r i n t l n ( " \ t " + a d d r e s s . g e t H o s t N a m e ( ) ) ;S y s t e m . o u t . p r i n t l n ( " \ t " + a d d r e s s . g e t H o s t A d d r e s s ( ) ) ;

    } c a t c h ( U n k n o w n H o s t E x c e p t i o n e ) {S y s t e m . o u t . p r i n t l n ( " U n a b l e t o d e t e r m i n e t h i s h o s t ' s a d d r e s s " ) ;}

    f o r ( i n t i = O; i < a r g s . l e n g t h ; i + + ) {/ / G e t n a m e ( s ) / a d d r e s s ( e s ) o f h o s t s g i v e n o n c o m m a n d l i n et r y {

    I n e t A d d r e s s [ ] a d d r e s s L i s t = I n e t A d d r e s s . g e t A l l B y N a m e ( a r g s [ i ] ) ;S y s t e m . o u t . p r i n t l n ( a r g s [ i ] + " : " ) ;/ / P r i n t t h e f i r s t n a m e . A s s u m e a r r a y c o n t a i n s a t l e a s t o n e e n t r y .S y s t e m . o u t . p r i n t l n ( " \ t " + a d d r e s s L i s t [ O ] . g e t H o s t N a m e ( ) ) ;f o r ( i n t j = O; j < a d d r e s s L i s t . l e n g t h ; j + + )

    S y s t e m . o u t . p r i n t l n ( " \ t " + a d d r e s s L i s t [ j ] . g e t H o s t A d d r e s s ( ) ) ;} c a t c h ( U n k n o w n H o s t E x c e p t i o n e ) {

    S y s t e m . o u t . p r i n t l n ( " U n a b l e t o f i n d a d d r e s s f o r " + a r g s [ i ] ) ;}

    InetAddressExample.java1. P r i n t i n f o r m a t i o n a b o u t t h e l o c a l h o s t : li n es 6 - 1 4

    9 C r e a t e a n I n e t A d d r e s s i n s t a n c e f o r t h e l o c a l h o s t : l in e 89 P r i n t t h e l o c a l h o s t i n f o r m a t i o n : l in e s 9 -1 1

    g e tH 0 s tN a m e ( ) a n d g e t H 0 s t A d d r e s s ( ) r e t u r n a s t r in g f o r t h e h o s t n a m e a n d IP a d d r e s s ,r e s p e c t i v e l y .

    2. R e q u e s t i n f o r m a t i o n f o r e a c h h o s t s p e c i f ie d o n t h e c o m m a n d l in e: l in e s 1 6 -2 89 C r e a t e a n a r r a y o f I n e tA d d r e s s i n s t a n c e s f o r t h e s p e c i f i e d h o s t : li ne 1 9

    T n e t A d d r e s s .g e t A l l B y N a m e ( ) r e t u r n s a n a r r a y o f I n e t A d d r e s s i n s t a n c e s , o n e f o r e a c ho f t h e s p e c i f i e d h o s t ' s a d d r e s s e s .

    9 P r i n t th e h o s t i n f o r m a t i o n : l i n es 22-24

    T o u s e t h i s a p p l i c a t i o n t o f i nd i n f o r m a t i o n a b o u t t h e l oc a l h o s t a n d t h e p u b l i s h e r ' s W e b s e r v e rwww.mkp.com), d o t h e f o l l o w i n g :

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    18/120

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    19/120

    ! 2 Chapter 2: Basic Sockets []static InetAddress getByName(String host)static Inet Addre ss getLocalHost0

    Returns an IP address for the specified/local host.host Host name or IP address

    A c c e s s o r sbyte[ ] getAddress0

    Returns the 4 bytes of the 32-bit IP address in big-endian order.String getHostAddress()

    Returns the IP address in dotted-quad notation (e.g., "169.1.1.2").String getHostName()

    Returns the canonical name of the host associated with the address.b o o l e a n isMulticastAddress()

    Returns true if the address is a multicast address (see Section 4.3.2).O p e r a t o r sb o o l e a n equals(Object address)

    Returns true if address is non-null and rep rese nts the same addres s as this $netAddressinstance.address Address to compare

    2 . 2 T C P S o c k e t sJava provide s two classes for TCP: Socket and ServerSocket. An insta nce of Socket rep re sen tsone end of a TCP connection. A TCP connection is an abstract two-way channel whose endsare each identified by an IP address and port number. Before being used for communication,a TCP connection must go through a setup phase, which starts with the client's TCP sending aconnec tion r eque st to the server's TCP. An instance of ServerSocket listens for TCP connec tionrequests and creates a new Socket instance to handle each incoming connection.

    2 . 2 . 1 T C P C l i e n tThe client initiates commu nica tion with a server that is passively waiting to be contacted. Thetypical TCP client goes through three steps:

    1. Const ruct an instance of Socket: The const ruct or estab lishes a TCP connec tion to thespecified remote host and port.

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    20/120

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    21/120

    14 Cha pter 2: Basic Sockets m

    3233343536373839

    }}

    t o t a l B y t e s R c v d + = b y t e s R c v d ;}

    S y s t e m . o u t . p r i n t l n ( " R e c e i v e d : " + n e w S t r i n g ( b y t e B u f f e r ) ) ;s o c k e t . c l o s e ( ) ; / / C l o s e t h e s o c k e t a n d i t s s t r e a m s

    T C P E c h o C I i e n t .j a v a

    1. A p p l i c a t io n s e t u p a n d p a r a m e t e r p a r s i n g : l i n e s 0 - 1 4[] C o n v e r t t h e e c h o s t r i n g : l ine 12T C P s o c k e t s s e n d a n d r e c e iv e s e q u e n c e s o f b y t e s . T h e g e t B y t e s ( ) m e t h o d o f S t r i n g

    r e t u r n s a b y t e a r r a y r e p r e s e n t a t i o n o f t h e s t r i n g . (S ee S e c t i o n 3.1 f o r a d i s c u s s i o n o fc h a r a c t e r e n c o d i n g s. )

    [] D e t e r m i n e t h e p o r t o f t h e e c h o s e r v e r : l ine 14T h e d e f a u l t e c h o p o r t is 7. I f w e s p e c if y a t h i r d p a r a m e t e r , I n t e g e r . p a r s e I n t ( ) t a k e st h e s t r i n g a n d r e t u r n s t h e e q u i v a l e n t i n t e g e r v a lu e .

    2 . T C P s o c k e t c r e a t i o n : l ine 17T h e S o c k e t c o n s t r u c t o r c r e a t e s a s o c k e t a n d e s t a b l i s h e s a c o n n e c t i o n t o t h e s p e c i f i e ds e r v e r , i d e n t i f i e d e i t h e r b y n a m e o r IP a d d r e s s . N o t e t h a t t h e u n d e r l y i n g T C P d e a l s o n l yw i t h I P a d d r e s s e s . I f a n a m e i s g iv e n , t h e i m p l e m e n t a t i o n r e s o l v e s i t to t h e c o r r e s p o n d -i n g a d d r e s s . I f t h e c o n n e c t i o n a t t e m p t f a i ls fo r a n y r e a s o n , t h e c o n s t r u c t o r t h r o w s a nl O B x c e p t i o n .

    3. G e t s o c k e t i n p u t a n d o u t p u t s t r e a m s : l i n e s 2 0 - 2 1A s s o c i a t e d w i t h e a c h c o n n e c t e d S o ck e t i n s t a n c e i s a n I n p u t S t re a m a n d 0 u t p u tS t r ea m . W es e n d d a t a o v e r t h e s o c k e t b y w r i t i n g b y t e s t o t h e 0 u t p u t S t r ea m j u s t a s w e w o u l d a n y o t h e rs t r e a m , a n d w e r e ce i v e b y r e a d i n g f r o m t h e I n p u t S tr e am .

    4 . S e n d t h e s t r i n g t o e c h o s e r v e r : l ine 23T h e w r i t e ( ) m e t h o d o f 0 u t p u t S tr e a m t r a n s m i t s t h e g i v en b y t e a r r a y o v e r t h e c o n n e c t i o nt o t h e s e r v e r .

    5 . R e c e i v e t h e r e p l y f r o m t h e e c h o s e r v e r: l i n e s 2 5 - 3 3S in c e w e k n o w t h e n u m b e r o f b y t e s t o e x p e c t f r o m t h e e c h o s e r v e r, w e c a n r e p e a t e d l yr e c ei v e b y t e s u n t i l w e h a v e r e c e i v ed t h e s a m e n u m b e r o f b y t e s w e s e n t . T h i s p a r t i c u l a rf o r m o f r e a d ( ) t a k e s t h r e e p a r a m e t e r s : 1) b u f f e r t o r e ce i v e i n t o , 2) b y t e o f f s e t i n t o t h eb u f f e r w h e r e t h e f i r st b y t e r e c e iv e d s h o u l d b e p la c e d , a n d 3) t h e m a x i m u m n u m b e r o fb y t e s t o b e p l a c e d i n t h e b u f f e r , r e a d ( ) b l o c k s u n t i l s o m e d a t a i s a v a i l a b l e , r e a d s u pt o th e s p e c i f ie d m a x i m u m n u m b e r o f b y t e s , a n d r e t u r n s t h e n u m b e r o f b y t e s a c t u a l l yp l a c e d i n t h e b u f f e r ( w h i ch m a y b e l e s s t h a n t h e g i v e n m a x i m u m ) . T h e l o o p s i m p l y f il ls

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    22/120

    [] 2.2 TCP Sockets 1 5

    up byteBuf fer until we receive as man y bytes as we sent. If the TCP connec tio n is closed bythe other end, read() returns -1. For the client, this indicates that the server prematurelyclosed the socket.

    Why not just a single read? TCP does not preserve read() and write() messageboundaries. That is, even though we sent the echo string with a single write(), the echoserver may receive it in multiple chunks. Even if the echo string is handle d in one chunkby the echo server, the reply may still be broken into pieces by TCP. One of the mostcommon errors for beginners is the assumption that data sent by a single write() willalways be received in a single read ().

    6. Print echo ed string: line 35To print the server's respo nse, we must convert the byte array to a string using the defaultcharacter encoding.

    7. Close socket : line 37When the client has finished receiving all of the echoed data, it closes the socket.We can communicate with an echo server named server .example.com with IP address

    169.1.1.1 in either of the following ways:% j a v a TCPE c h o Cl i e n t s e r v e r . e x a m p l e . c o m " E c h o t h i s "Received: Echo this% java TCPE choClie nt 169. . i. 1 "Ec ho this "Received: Echo this

    S e e T C P E c h o C l ie n t G U I . j a v a o n t h e b o o k ' s W e b s it e f or a n i m p l e m e n t a t i o n o f t h e T C P e c h o c li en twith a graphical interface.

    S o c k e tC o n s t r u c t o r sSocket(InetAddress remoteAddr, in t remotePort)Socket(String remoteHost, int remotePort)Socket(InetAddress remoteAddr, in t remotePort , InetAddress localAddr, int localPort)Socket(String remoteHost, int remotePort, InetAddress localAddr, int localPort)

    Constru cts a TCP socket connec ted to the specified remote addre ss and port. The firsttwo forms of the constructor do not specify the local address and port, so a defaultlocal addres s and some available port are chosen. Specifying the local address may beuseful on a host with multiple interfaces.r e mo t e Ad d r Remote host addressremoteHost Remote host name or IP address (in dotted-quad form)remotePort Remote port

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    23/120

    1 6 Cha pter 2: Basic Sockets []

    localAddrlocalPort

    L o c a l a d d r e s s ; u s e n u l l t o s p e c i f y u s i n g t h e d e f a u l t l o c a la d d r e s sL o c a l p o r t ; a lo c a l P o r t o f 0 a l l o w s t h e c o n s t r u c t o r t o p i c k a n ya v a i l a b le p o r t

    O p e r a t o r sv o i d c l o s e ( )

    C l o s e s t h e T C P s o c k e t a n d i t s I / O s t r e a m s .v o i d shutdownTnput()

    C l o s e s t h e i n p u t s i d e o f a T C P s t r e a m . A n y u n r e a d d a t a i s si l e n t l y d i s c a r d e d , i n c l u d i n gd a t a b u f f e r e d b y t h e s o c k e t , d a t a i n t r a n s i t , a n d d a t a a r r i v i n g i n t h e f u t u r e . A n y s u b s e -q u e n t a t t e m p t t o r e a d f r o m t h e s o c k e t w i ll r e t u r n e n d - o f - s t r e a m ( -1 ); a n y s u b s e q u e n tc a ll t o g e t l n p u t S t r e a m ( ) w i ll c a u s e a n l O E x c e p t i o n t o b e t h r o w n ( s e e S e c t i o n 4 . 5 ) .

    v o i d s h u t d o w n 0 u t p u t ( )C l o s es t h e o u t p u t s id e o f a T C P s t r e a m . T h e i m p l e m e n t a t i o n w i ll a t t e m p t t o d e l iv e r a n yd a t a a l r e a d y w r i t t e n t o t h e s o c k e t ' s o u t p u t s t r e a m t o t h e o t h e r e n d . A n y s u b s e q u e n ta t t e m p t t o w r i t e to t h e s o c k e t ' s o u t p u t s t r e a m o r to c a ll g e t 0 u t p u t S t r e a m ( ) w i ll c a u s ea n I O E x c e p t i o n t o b e t h r o w n ( s e e S e c t i o n 4 . 5 ) .

    A c c e s s o r s / M u t a t o r sI n e t A d d r e s s g e t l n e t A d d r e s s ( )int g e t P o r t ( )

    R e t u r n s t h e r e m o t e s o c k e t a d d r e s s / p o r t .InputStr e a m g e t l n p u t S t r e a m 0O u t p u t S t r e a m g e t 0 u t p u t S t r e a m 0

    R e t u r n s a s t r e a m f o r r e a d i n g / w r i t i n g b y t e s f r o m / t o t h e s oc k e t .b o o l e a n g e t K e e p A l i v e ( )v o i d s e tK e e p A l i v e (b o o l e an on)

    R e t u r n s / s e t s k e e p a l iv e m e s s a g e b e h a v i o r . If k e e p a l iv e i s e n a b l e d , T CP s e n d s a p r o b et o th e o t h e r e n d o f th e c o n n e c t i o n w h e n n o d a t a h a s b e e n e x c h a n g e d f o r a s y s t e m -d e p e n d e n t a m o u n t o f t i m e ( u s u a l l y tw o h o u r s ) . I f t h e r e m o t e s o c k e t i s st i ll al iv e , i tw i ll a c k n o w l e d g e t h e p r o b e ( i nv i s ib l e to t h e a p p l i c a t i o n ) . H o w e v e r , i f t h e o t h e r e n df a il s to a c k n o w l e d g e s e v e r a l p r o b e s i n a r o w , t h e l o ca l T C P c l o s e s t h e c o n n e c t i o n , a n ds u b s e q u e n t o p e r a t i o n s o n i t w i ll th r o w a n e x c e p t i o n . K e e p a li v e is d i s a b l e d b y d e f a u lt .on I f t r u e ( f a l s e ) , e n a b l e ( d i s a b l e ) k e e p a l i v e .

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    24/120

    [] 2.2 TCP So cke ts |

    Inet ddress g e t L o c a l A d d r e s s ( )i n t g e t L o c a l P o r t ( )

    R e t u r n s t h e l o c a l s o c k e t a d d r e s s / p o r t .i n t g e t R e c e i v e B u f f e r S i z e ( )i n t g e t S e n d B u f f e r S i z e ( )v o i d s e t R e c e i v e B u f f e r S i z e ( i n t s i z ev o i d s e t S e n d B u f f e r S i z e (i n t s i z e

    R e t u r n s / s e t s t h e s i z e o f t h e s e n d / r e c e i v e b u f f e r f o r t h e s o c k e t ( s e e S e c t i o n 4 . 4) .s i z e N u m b e r o f b y t e s t o a l lo c a t e fo r t h e s o c k e t s e n d / r e c e i v e

    b u f f e r

    i n t g e t S o L i n g e r ( )v o i d s e t S o L i n g e r ( b o o l e a n o n , i n t l i n g e r )R e t u r n s / s e t s t h e m a x i m u m a m o u n t o f t im e ( in m i l li s ec o n d s ) t h a t c l o s e ( ) w ill b l o c kw a i t i n g f o r a ll d a t a t o b e d e l iv e r e d , g e t S o L i n g e r ( ) r e t u r n s - 1 i f l i n g e r i n g is d i s a b l e d( s e e S e c t i o n 5 .4 ). L i n g e r i n g i s o f f b y d e f a u l t .o n I f t r u e , t h e s o c k e t li n g e r s o n c l o s e ( ) , u p t o t h e m a x i m u m

    s p e c i f i e d t i m e .l i n g e r T h e m a x i m u m a m o u n t o f t i m e ( m i l li s e co n d s ) a s o c k e t l in g e r s

    o n c l o s e ()int getSoTimeout()v o i d s e t S o T i m e o u t ( i n t t i m e o u t )

    R e t u r n s / s e t s t h e m a x i m u m a m o u n t o f t i m e t h a t a r e a d ( ) o n th i s s o c k e t w i ll b l o ck . Ift h e s p e c i f ie d n u m b e r o f m i l l i s e c o n d s e l a p s e s b e f o r e a n y d a t a i s a v a il ab l e, a n I n t e r -r u p t e d I O E x c e p t i o n i s t h r o w n ( s e e S e c t i o n 4 . 2 ) .t i m e o u t T h e m a x i m u m t i m e ( m i l l i s e c o n d s ) t o w a i t f o r d a t a o n a

    r e a d ( ) . T h e v a l u e 0 ( t h e d e f a u l t ) i n d i c a t e s t h a t t h e r e i s n ot i m e l i m it , m e a n i n g t h a t a r e a d w i l l n o t r e t u r n u n t i l d a t a i sa v a i l a b l e .

    b o o l e a n g e t T c p N o D e l a y ( )v o i d s e t T c p N o D e l a y ( b o o l e a n o n

    R e t u r n s / s e t s w h e t h e r t h e N a g l e a l g o r i t h m t o c o a l e s c e T C P p a c k e t s i s d i s a b l e d . T o a v o i ds m a l l TC P p a c k e t s , w h i c h m a k e i n e ff ic i e nt u s e o f n e t w o r k r e s o u r c e s , N a g l e 's a l g o r i t h m( e n a b l e d b y d e f au l t ) d e la y s p a c k e t t r a n s m i s s i o n u n d e r c e r t a i n c o n d i t i o n s t o i m p r o v et h e o p p o r t u n i t i e s t o c o a l e s c e b y t e s f r o m s e v e r a l w r i t e s i n t o a s in g l e TC P p a c k e t . T h i sd e l a y is u n a c c e p t a b l e t o s o m e t y p e s o f i n t e ra c t i v e a p p l i c a t io n s .o n I f t r u e ( f a l s e ) , d i s a b l e ( e n ab l e ) N a g l e ' s a l g o r i t h m .

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    25/120

    1 8 Chapter 2: Basic Sockets m

    Caveat: By default, Socket is implemented on top of a TCP connection; however, in Java,you can actually change the underlying implementation of Socket. This book is about TCP/IP,so for simplicity we assume t hat the under lying imp leme nta tion for all of the these ne tworki ngclasses is the default.2 2 2 TCP ServerWe now turn our attention to constructing a server. The server's job is to set up a communi-cation endpoint and passively wait for connections from clients. The typical TCP server goesthrou gh two steps:

    1. Construct a ServerSocket instance, specifying the local port. This socket listens forincoming connections to the specified port.

    2. Repeatedly:9 Call the accept () m et hod of ServerSocket to get the next incoming client connection.Upon establishment of a new client connection, an instance of Socket for the newconnection is created and re turned by accept ().

    9 Communicate with the client using the returned Socket's InputStream and Output-Stream.

    9 Close the new client socket connection using the cl ose( ) met hod of Socket.Our next example, TCPEchoServer. java, im pleme nt s t he e cho service use d by o ur client

    progra m. The server is very simple. It runs forever, repea tedly accepting a connection, receivingand echoing bytes until the connec tion is closed by the client, and th en closing the client socket.TCPEchoServer.java

    0 import ja va .net .* ; / / for Socket, ServerSocket, and InetAddress1 import ja va .i o. *; / / for IOException and Input/0utputSt ream23 pub lic class TCPEchoServer {456789101112131415161718

    p r i v a t e s t a t i c f i n a l i n t B U F S I Z E = 3 2 ; / / S i z e o f r e c e i v e b u f f e rp u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) t h r o w s l O E x c e p t i o n {

    i f ( a r g s . l e n g t h ! = i ) / / T e s t f o r c o r r e c t # o f a r g st h r o w n e w l l l e g a l A r g u m e n t E x c e p t i o n ( " P a r a m e t e r ( s ) : < P o r t > " ) ;

    i n t s e r v P o r t = I n t e g e r . p a r s e l n t ( a r g s [ 0 ] ;/ / C r e a t e a s e r v e r s o c k e t t o a c c e p t c l i e n t c o n n e c t i o n r e q u e s t sS e r v e r S o c k e t s e r v S o c k = n e w S e r v e r S o c k e t ( s e r v P o r t ) ;i n t r e c v M s g S i z e ; / / S i z e o f r e c e i v e d m e s s a g eb y t e [ ] b y t e B u f f e r = n e w b y t e [ B U F S l Z E ] ; / / R e c e i v e b u f f e r

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    26/120

    m 2.2 TCP Sockets 1 9

    1920212223242526272829303132333435363738}

    }

    f o r ( ; ; ) { / / R u n f o r e v e r , a c c e p t i n g a n d s e r v i c i n g c o n n e c t i o n sS o c k e t c l n t S o c k = s e r v S o c k . a c c e p t ( ) ; / / G e t c l i e n t c o n n e c t i o nS y s t e m . o u t . p r i n t l n ( " H a n d l i n g c l i e n t a t " +

    c l n t S o c k . g e t I n e t A d d r e s s ( ) . g e t H o s t A d d r e s s ( ) + " o n p o r t " +c l n t S o c k , g e t P o r t ( ) ) ;

    I n p u t S t r e a m i n = c l n t S o c k , g e t l n p u t S t r e a m ( ) ;O u t p u t S t r e a m o u t = c l n t S o c k . g e t O u t p u t S t r e a m ( ) ;

    / / R e c e i v e u n t i l c l i e n t c l o s e s c o n n e c t i o n , i n d i c a t e d b y - i r e t u r nw h i l e ( ( r e c v M s g S i z e = i n . r e a d ( b y t e B u f f e r ) ) ! = - I )

    o u t . w r i t e ( b y t e B u f f e r , O , r e c v M s g S i z e ) ;c l n t S o c k , c l o s e ( ) ;

    }/ * N O T R E A C H E D * /

    / / C l o s e t h e s o c k e t . W e a r e d o n e w i t h t h i s c l i e n t !

    TCPEchoServer.java

    1. Application s e t u p a n d parameter parsing: lines 0-122. Serve r sock et creation: line 15servSock listens for client connection requests on the port specified in the constructor.

    3. Loop forever, iterati vely handling i ncomin g connections" lines 20-359 Accept an incoming connection: line 21

    The sole purpose of a ServerSocket instance is to supply a new, connected Socketinstance for each new TCP connection. When the server is ready to handle a client, itcalls accept (), which blocks until an incoming connection is made to the ServerSocket'sport. accept() then returns an instance of Socket that is already connected to theremote socket and ready for reading and writing.9 Report connected client: lines 23-25We can query the newly created Socket instance for the address and port of theconnecting client. The getlnetAddress() method of Socket returns an instance ofInetAddress containing the address of the client. We call getHostAddress() to returnthe IP address as a dotted-quad String. The getPort() method of Socket returns theport of the client.

    9 Get soc ket i n p u t a n d o u t p u t streams: lines 27-28Bytes written to this socket's 0utputStream will be read from the client's socket'sInputStream, and bytes written to the client's 0utputStream will be read from thissocket's InputStream.

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    27/120

    2 0 Cha pter 2: Basic Sockets []9 R e c e i v e a n d r e p e a t d a t a u n t i l t h e c l i en t c l o s e s : l in e s 3 0 - 3 2

    T h e w h i l e l o o p r e p e a t e d l y r e a d s b y t e s ( w h e n a v a i l a b l e ) f r o m t h e i n p u t s t r e a m a n di m m e d i a t e l y w r i t e s t h e s a m e b y t e s b a c k t o th e o u t p u t s t r e a m u n t i l th e c l i e n t c l o s e st h e c o n n e c t i o n . T h e r e a d ( ) m e t h o d o f I n p u t S tr e a m r e a d s up to th e m a x i m u m n u m b e ro f b y t e s t h e a r r a y c a n h o l d ( in t h i s c a s e , B UF SIZ E b y t e s ) i n t o t h e b y t e a r r a y (byteBuffer)a n d r e t u r n s t h e n u m b e r o f b y t e s r e a d . r e a d ( ) b l o c k s u n t il d a t a i s a v a il a b le a n d r e t u r n s- 1 i f t h e r e i s n o d a t a a v a i la b l e , i n d i c a t i n g t h a t t h e c l i e n t c l o s e d i ts s o c k e t . I n th e e c h op r o t o c o l , t h e c l i e n t c l o s es t h e c o n n e c t i o n w h e n i t h a s r e c e i v e d t h e n u m b e r o f b y t e sb a c k t h a t i t s e n t , s o i n t h e s e r v e r w e e x p e c t t o r e c e i v e a - 1 f r o m r e a d ( ) . R e c a l l t h a t i nt h e c l ie n t , r e c e i v i n g a - 1 f r o m r e a d ( ) i n d i c a t e s a n e r r o r b e c a u s e i t i n d i c a t e s t h a t t h es e r v e r p r e m a t u r e l y c l o s e d t h e c o n n e c t i o n .

    A s p r e v i o u s l y m e n t i o n e d , r e a d ( ) d o e s n o t h a v e t o fi ll t h e e n t i r e b y t e a r r a y tor e t u r n . I n f ac t, i t c a n r e t u r n a f t e r h a v in g r e a d o n l y a s in g le b y te . T h e w r i t e ( ) m e t h o do f 0 u t p u t S t r e a m w r i t e s recvMsgSize b y t e s f r o m byteBuf fer t o t h e s o c k e t . T h e s e c o n dp a r a m e t e r i n d i c a t e s t h e o f f s e t i n t o t h e b y t e a r r a y o f th e f i r st b y t e t o s e n d . I n t h is c a s e ,0 i n d i c a t e s t o t a k e b y t e s s t a r t i n g f r o m t h e f r o n t o f byteBuffer. I f w e h a d u s e d t h ef o r m o f w r i t e ( ) t h a t t a k e s o n l y t h e b u f f e r a r g u m e n t , all t h e b y t e s i n t h e b u f fe r a r r a yw o u l d h a v e b e e n t r a n s m i t t e d , p o s s i b l y i n c l u d i n g b y t e s t h a t w e r e n o t r e c e i v e d f r o mthe c l ien t !

    9 C l o s e c l i e n t s o c k e t : l ine 34

    S e r v e r S o c k e tC o n s t r u c t o r sS e r v e r S o c k e t ( i n t localPort)S e r v e r S o c k e t ( i n t localPort, int queueLimit)S e r v e r S o c k e t ( i n t localPort, i n t queueL imi t n e t A d d r e s s localAddr)

    C o n s t r u c t a T C P s o c k e t t h a t i s r e a d y t o a c c e p t i n c o m i n g c o n n e c t i o n s t o t h e s p e c i f i e dl o c a l p o r t . O p t i o n a l l y , t h e s i z e o f t h e c o n n e c t i o n q u e u e a n d t h e l o c al a d d r e s s c a n b ese t .localPort L o c a l p o r t . A p o r t o f 0 a ll o w s t h e c o n s t r u c t o r t o p i c k a n y

    a v a i l a b l e p o r t .queueL imi t T h e m a x i m u m s iz e o f t h e q u e u e o f i n c o m p l e t e c o n n e c t i o n s

    a n d s o c k e t s w a i t i n g t o b e a c c e p t ( ) e d . I f a cl ie n t c o n n e c t i o nr e q u e s t a r r i v e s w h e n t h e q u e u e i s f u l l , t h e c o n n e c t i o n i sr e f u s e d . N o t e t h a t t h i s m a y n o t n e c e s s a r i l y b e a h a r d l i rr dt .F o r m o s t p l a t f o r m s , i t c a n n o t b e u s e d t o p r e c i s e l y c o n t r o lc l i e n t p o p u l a t i o n .

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    28/120

    m 2 2 TCP Sockets 21l o c a l A d d r The IP addre ss to which connections to this socket sh ould be

    addr esse d (must be one of the local interface addresses). If theaddre ss is not specified, the socket will accept con nection s toany of the host's IP addresses. This may be useful for hostswith multiple interfaces where the server socket should onlyaccept connections on one of its interfaces.

    OperatorsSocket accept()

    R e t u r n s a c o n n e c t e d S o c k et i ns t a n ce fo r t h e n e x t n e w i n c o m i n g c o n n e c t i o n t o t h eserver socket. If no established co nnec tion is waiting, accept() blocks until one isestablished or a time out occurs (see setSoTimeout()).

    vo id close()C l o s e s t h e u n d e r l y i n g T C P s o c k et . A f t e r i n v o k i n g t h i s m e t h o d , i n c o m i n g c li en t c o n -nect ion requ ests for this socke t are rejected.

    Accessors /Muta torsInetAddress getlnetAddress ()int getLocalPort()

    Returns the local address/port of the server socket.int g e t S o T i m e o u t Ovoid setSoTimeout(int t i m e o u t )

    Returns/sets the maximum amount of time (in milliseconds) that an accept() willblock for this socket. If the timer expires before a connection request arrives, anInterruptedlOException is thrown. A timeout value of 0 indicates no timeout: callsto accept () will not ret urn until a new connect ion is available, regardless of how muc htime passes (see Section 4.2).

    2 2 3 I n p u t a n d O u t p u t S tr ea m sAs illustrated by the examples above, the primary paradigm for I/O in Java is the s t r e a mabstraction. A stream is simply an ordered sequence of bytes. Java i n p u t s t r e a m s supportreading bytes, and o u t p u t s t r e a m s support writing bytes. In our TCP client and server, eachSocket instance holds an InputStream and an 0utputStream instance. When we write to theoutput stream of a Socket, the bytes can (eventually) be read from the input stream of theSocket at the other end of the connection.

    0utputStream is the abstract superclass of all output streams in Java. Using an Output-Stream, we can write bytes to, flush, and close the output stream.

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    29/120

    2 2 Ch apter 2: Basic Sockets II

    OutputSt ream

    dataoffsetlength

    v o i d f l u s h ( )

    a b s t r a c t v o i d w r i t e ( i n t data)W r i t e s a s i n g l e b y t e t o t h e o u t p u t s t r e a m .data B y t e ( l o w - o r d e r 8 b i t s ) t o w r i t e t o o u t p u t s t r e a m

    v o i d w r i t e ( b y t e [ ] data)W r i t e s e n ti r e a r r a y o f b y t e s t o t h e o u t p u t s t r e a m .data B y t e s t o w r i t e t o o u t p u t s t r e a m

    v o i d w r i t e ( b y t e [ ] data, int offset , int length)W r i t e s length b y t e s f r o m data s t a r t i n g f r o m b y t e offset.

    B y t e s f r o m w h i c h t o w r i t e t o o u t p u t s t r e a mS t a r t i n g b y t e t o s e n d i n dataN u m b e r o f b y t e s t o s e n d

    P u s h e s a n y b u f f e r e d d a t a o u t t o t he s t r e a m .v o i d c l o s e ( )

    T e r m i n a t e s t h e s t r e a m .

    I n p u t S t r e a m i s t h e a b s t r a c t s u p e r c l a s s o f a ll i n p u t s t r e a m s . U s i n g a n I n p u t S t r e a m , w e c a nr e a d b y t e s f r o m a n d c l o s e t h e i n p u t s t r e a m .

    InputStreama b s t r a c t i n t r e a d ( )

    R e a d a n d r e t u r n a s i n g l e b y t e f r o m t h e i n p u t s t r e a m . T h e b y t e r e a d i s i n t h e l e a s ts i g n if i c an t b y t e o f t h e r e t u r n e d i n t eg e r . T h i s m e t h o d r e t u r n s - 1 o n e n d - o f - s t r e a m .

    i n t r e a d ( b y t e [ ] data)R e a d s u p t o data.length b y t e s ( o r u n t i l t h e e n d - o f - s tr e a m ) f r o m t h e i n p u t s t r e a m i n t odata a n d r e t u r n s t h e n u m b e r o f b y t e s r e a d . I f n o d a t a i s a v a i la b l e , r e a d ( ) b l o c k s u n t i la t l e a s t I b y t e c a n b e r e a d o r th e e n d - o f - s t r e a m i s d e t e c t e d , i n d i c a t e d b y a r e t u r n o f - 1 .data B u ff er to r e c ei v e d a t a f r o m i n p u t s t r e a m

    i n t r e a d ( b y t e [ ] data, int offset, i n t length)R e a d s u p t o length b y t e s ( o r u n t i l t h e e n d - o f - s t r e a m ) f r o m t h e i n p u t s t r e a m i n t odata, s t a r t i n g a t p o s i t i o n offset, a n d r e t u r n s t h e n u m b e r o f b y t e s r e a d . If n o d a t a

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    30/120

    [] 2.3 UDP Sockets 23

    is available, read() blocks until at least 1 byte can be read or the end-of-stream isdetected, indicated by a return of -1.d a t a Buffer to receive data from input streamof fse t Starting byte of d a t a in which to writel eng th Maximum number of bytes to read

    i n t available()Return s the nu m b e r of bytes available for input.

    vo id close()Term inate s the stream.

    2 . 3 U D P S o c k e t sUDP provides an end-to-end service different from that of TCP. In fact, UDP performs onlytwo functions: 1) it adds another layer of addressing (ports) to that of IP, and 2) it detectsdata corruption that may occur in transit and discards any corrupted messages. Because ofthis simplicity, UDP sockets have some different characteristics from the TCP sockets we sawearlier. For example, UDP sockets do not have to be connected before being used. Where TCPis analogous to telephone communication, UDP is analogous to communicating by mail: youdo not have to "connect" before you send a package or letter, but you do have to specifythe destination address for each one. Similarly, each message--called a d a t a g r a m - - c a r r i e s itsown address information and is independent of all others. In receiving, a UDP socket is likea mailbox into which letters or packages from many different sources can be placed. As soonas it is created, a UDP socket can be used to send/receive messages to/from any address andto/ fro m many different a ddresses in succession.

    Another difference bet ween UDP sockets and TCP sockets is the way that they deal withmessage boundaries: UDP sockets prese rve them. This makes receiving an application messagesimpler, in some ways, t han it is with TCP sockets. (This is dis cus sed fur the r in Section 2.3.4.) Afinal difference is that the end-to-end transport service UDP provides is best-effort: there is noguarant ee that a message sent via a UDP socket will arrive at its destination, and mess ages canbe delivered in a different order than they were sent Oust like letters sent through the mail).A program using UDP sockets must therefore be prepared to deal with loss and reordering.(We'll provide an example of this later.)

    Given this additional burden, why would an application use UDP instead of TCP? Onereason is efficiency: if the application exchanges only a small amount of data--say, a singlerequest message from client to server and a single response message in the other direction--TCP's connection establishment phase at least doubles the number of messages (and thenumber of round-trip delays) required for the communication. Another reason is flexibility:when someth ing other than a reliable byte- strea m service is required, UDP provides a minimal-overhead platform on which to implement whatever is needed.

    Java programmers use UDP sockets via the classes DatagramPacket and DatagramSocket.Both clients and servers use DatagramSockets to send and receive DatagramPackets.

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    31/120

    2 4 Ch apter 2: Basic Sockets []2 .3 .1 DatagramPacketI n s t e a d o f s e n d i n g a n d r e c e i v in g s t r e a m s o f b y t e s a s w i t h T CP , U D P e n d p o i n t s e x c h a n g es e l f - c o n t a i n e d m e s s a g e s , c a ll e d d a t a g r a m s , w h i c h a r e r e p r e s e n t e d i n Ja v a a s i n s t a n c e s o fD a t a g ra m P a c k e t. T o s e n d , a J a v a p r o g r a m c o n s t r u c t s a D a t a gr a m P ac k e t i n s t a n c e a n d p a s s e s i t a sa n a r g u m e n t t o t h e s e n d ( ) m e t h o d o f a D a ta g ra m S oc k et . T o re c ei ve , a J a v a p r o g r a m c o n s t r u c t sa D a t a g ra m P a c k e t i n s t a n c e w i t h p r e a l l o c a t e d s p a c e ( a b y t e [ ]), i n t o w h i c h t h e c o n t e n t s o f ar e c e i v e d m e s s a g e c a n b e c o p i e d ( i f / w h e n o n e a r r iv e s) , a n d t h e n p a s s e s t h e i n s t a n c e t o t h er e c e i v e ( ) m e t h o d o f a D a t ag r am S o c k et .

    I n a d d i t i o n t o t h e d a t a , e a c h i n s t a n c e o f D a t ag r am P a c k e t a l s o c o n t a i n s a d d r e s s a n d p o r ti n f o r m a t i o n , t h e s e m a n t i c s o f w h i c h d e p e n d o n w h e t h e r t h e d a t a g r a m i s b e i n g s e n t o r re c e iv e d .W h e n a D a t a g r a m P a c k e t i s s e n t , t h e a d d r e s s a n d p o r t i d e n t i f y t h e d e s t i n a t i o n ; f o r a r e c e i v e dD a t a g ra m P a c k et , t h e y i d e n t i f y t h e s o u r c e o f t h e r e c e i v e d m e s s a g e . T h u s , a s e r v e r c a n r e c e i v ei n t o a D a t a g ra m P a c k e t i n s t a n c e , m o d i f y it s b u f f e r c o n t e n t s , t h e n s e n d t h e s a m e i n s t a n c e , a n dt h e m o d i f i e d m e s s a g e w i ll g o b a c k t o i ts o r i g i n . I n t e r n a ll y , a D a t ag r am P a c k e t a l s o h a s lengtha n d offset f ie ld s , w h i c h d e s c r i b e t h e l o c a t i o n a n d n u m b e r o f b y t e s o f m e s s a g e d a t a i n s i d e t h ea s s o c i a t e d b u f f e r . S ee t h e f o l l o w i n g r e f e r e n c e a n d S e c t i o n 2 .3 .4 f o r s o m e p i t f a l l s t o a v o i d w h e nu s i n g D a t a g ra m P a c k e ts .

    D a t a g r a m P a c k e tC o n s t r u c t o r sDa t a g r a m P a c k e t ( b y t e [ ] buffer, i n t length)Da t a g r a m P a c k e t ( b y t e [ ] buffer, i n t offset, int length)Da t a g r a m P a c k e t ( b y t e [ ] buffer, i n t length, I n e t A d d r e s s remoteAddr, i n t remotePort)D a t a g r a m P a c k e t ( b y t e [ ] buffer, int offset, i n t length, I n e t A d d r e s s remoteAddr , i n t re-motePort)

    C o n s t r u c t s a d a t a g r a m a n d m a k e s t h e g i v e n b y t e a r r a y it s d a t a b u ff e r. T h e f ir s t t w of o r m s a r e t y p i c a l l y u s e d t o c o n s t r u c t D a t a g ra m P a c k e ts f o r r e c e i v i n g b e c a u s e t h e d e s t i -n a t i o n a d d r e s s i s n o t s p e c if i ed ( a l t h o u g h i t c o u l d b e s p e c i fi e d l a te r w i t h s e t A d d r e s s ( )a n d s e t P o r t () ). T h e s e c o n d t w o f o r m s a r e t y p ic a l ly u s e d t o c o n s t r u c t D a t ag r am P a c k et sf o r s e n d i n g .buf fer D a t a g r a m p a y l o a dlength N u m b e r o f b y t e s o f th e b u f f e r t h a t w i ll a c tu a l l y b e u s e d .

    I f t h e d a t a g r a m i s s e n t , length b y t e s w i l l b e t r a n s m i t t e d . I fr e c e i v i n g i n t o t h i s d a t a g r a m , length s p e c i f i e s t h e m a x i m u mn u m b e r o f b y t e s t o b e p l a c e d i n th e b u f f er .

    offset L o c a t i o n i n t h e b u f f e r a r r a y o f th e f i r s t b y t e o f m e s s a g e d a t at o b e s e n t / r e c e i v e d ; d e f a u l t s t o 0 i f u n s p e c i f i e d .

  • 8/12/2019 TCP-IP Sockets in Java - Practical Guide for Programmers

    32/120

    [] 2.3 UDP Sockets 2 5r e m o te A d d rremotePor t

    Address (typically destination) of the datagramPort (typically destination) of the datagram

    A c c e s s o r s / M u t a t o r sI n e t A d d r e s s getAddress()v o i d setAddress(InetAddress address)

    Returns/sets the datagram address. There are other ways to set the address: 1) theaddress of a DatagramPacket instance can also be set by the constructor, and 2)the receive() method of DatagramSocket sets the address to the datagram sender'saddress.address Datagram address

    i n t getPort()v o i d setPort(int port)

    Returns/sets the datagram port. There are other ways to set the address: 1) the portcan be explicitly set by the constructor or the setPort() method, and 2) the receive()method of DatagramSocket sets the port to the datagram sender's port.port Datagram port

    i n t getT.ength()v o i d setLength(int length)Retur ns/set s the internal length of the datagram. The internal datagram length can be

    set explicitly by the constructor or by the setLength() method. Attempting to make itlarger than the length of the associ ated buffer results in an IllegalArgumentException.The receive() method of DatagramSocket uses the internal length in two ways: 1) oninput, it specifies the maximum number of bytes of a received message that will becopied into the buffer, and 2) on return, it indicates the number of bytes actually placedin the buffer.l eng th Length in bytes of the usable portion of the buffer

    i n t get0ffset()Returns the location in the buffer of the first byte of data to be sent/received. Thereis no s et 0f fs et () method; however, it ca