You are on page 1of 19

JavaSocketProgramming Examples

Althoughmostprogrammersprobablydonetworkprogrammingusinganicelibrarywithhighlevelapplicationprotocol(such asHTTP)supportbuiltin,it'sstillusefultohaveanunderstandingofhowtocodeatthesocketlevel.Hereareafewcomplete examplesyoucancompileandrun.

Overview
Wewilllookatfournetworkapplications,writtencompletelyfromscratchinJava.Wewillseethatwecanwrite theseprogramswithoutanyknowledgeofthetechnologiesunderthehood(whichincludeoperatingsystem resources,routingbetweennetworks,addresslookup,physicaltransmissionmedia,etc.) Eachoftheseapplicationsusetheclientserverparadigm,whichisroughly 1. Oneprogram,calledtheserverblockswaitingforaclienttoconnecttoit 2. Aclientconnects 3. Theserverandtheclientexchangeinformationuntilthey'redone 4. Theclientandtheserverbothclosetheirconnection Theonlypiecesofbackgroundinformationyouneedare: Hostshaveports,numberedfrom065535.Serverslistenonaport.Someportnumbersarereservedsoyou can'tusethemwhenyouwriteyourownserver. Multipleclientscanbecommunicatingwithaserveronagivenport.Eachclientconnectionisassigneda separatesocketonthatport. Clientapplicationsgetaportandasocketontheclientmachinewhentheyconnectsuccessfullywitha server. Thefourapplicationsare Atrivialdateserverandclient,illustratingsimpleonewaycommunication.Theserversendsdatatothe clientonly. Acapitalizeserverandclient,illustratingtwowaycommunication.Sincethedialogbetweentheclientand servercancompriseanunboundednumberofmessagesbackandforth,theserveristhreadedtoservice multipleclientsefficiently. Atwoplayertictactoegame,illustratingaserverthatneedstokeeptrackofthestateofagame,andinform eachclientofit,sotheycaneachupdatetheirowndisplays. Amultiuserchatapplication,inwhichaservermustbroadcastmessagestoallofitsclients.

ADateServerandClient
Theserver
DateServer.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . i o . I O E x c e p t i o n i m p o r t j a v a . i o . P r i n t W r i t e r i m p o r t j a v a . n e t . S e r v e r S o c k e t i m p o r t j a v a . n e t . S o c k e t i m p o r t j a v a . u t i l . D a t e / * * * A T C P s e r v e r t h a t r u n s o n p o r t 9 0 9 0 . W h e n a c l i e n t c o n n e c t s , i t * s e n d s t h e c l i e n t t h e c u r r e n t d a t e a n d t i m e , t h e n c l o s e s t h e * c o n n e c t i o n w i t h t h a t c l i e n t . A r g u a b l y j u s t a b o u t t h e s i m p l e s t * s e r v e r y o u c a n w r i t e . * / p u b l i c c l a s s D a t e S e r v e r { / * * * R u n s t h e s e r v e r . * / 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 ) t h r o w s I O E x c e p t i o n { S e r v e r S o c k e t l i s t e n e r = n e w S e r v e r S o c k e t ( 9 0 9 0 ) t r y { w h i l e ( t r u e ) { S o c k e t s o c k e t = l i s t e n e r . a c c e p t ( ) t r y { P r i n t W r i t e r o u t = n e w P r i n t W r i t e r ( s o c k e t . g e t O u t p u t S t r e a m ( ) , t r u e ) o u t . p r i n t l n ( n e w D a t e ( ) . t o S t r i n g ( ) ) } f i n a l l y { s o c k e t . c l o s e ( ) } } } f i n a l l y { l i s t e n e r . c l o s e ( ) } } }

Theclient
DateClient.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . i o . B u f f e r e d R e a d e r i m p o r t j a v a . i o . I O E x c e p t i o n i m p o r t j a v a . i o . I n p u t S t r e a m R e a d e r i m p o r t j a v a . n e t . S o c k e t

i m p o r t j a v a x . s w i n g . J O p t i o n P a n e / * * * T r i v i a l c l i e n t f o r t h e d a t e s e r v e r . * / p u b l i c c l a s s D a t e C l i e n t { / * * * R u n s t h e c l i e n t a s a n a p p l i c a t i o n . F i r s t i t d i s p l a y s a d i a l o g * b o x a s k i n g f o r t h e I P a d d r e s s o r h o s t n a m e o f a h o s t r u n n i n g * t h e d a t e s e r v e r , t h e n c o n n e c t s t o i t a n d d i s p l a y s t h e d a t e t h a t * i t s e r v e s . * / 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 ) t h r o w s I O E x c e p t i o n { S t r i n g s e r v e r A d d r e s s = J O p t i o n P a n e . s h o w I n p u t D i a l o g ( " E n t e r I P A d d r e s s o f a m a c h i n e t h a t i s \ n " + " r u n n i n g t h e d a t e s e r v i c e o n p o r t 9 0 9 0 : " ) S o c k e t s = n e w S o c k e t ( s e r v e r A d d r e s s , 9 0 9 0 ) B u f f e r e d R e a d e r i n p u t = n e w B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( s . g e t I n p u t S t r e a m ( ) ) ) S t r i n g a n s w e r = i n p u t . r e a d L i n e ( ) J O p t i o n P a n e . s h o w M e s s a g e D i a l o g ( n u l l , a n s w e r ) S y s t e m . e x i t ( 0 ) } }

Youcanalsotesttheserverwitht e l n e t .

ACapitalizationServerandClient
Theserver
CapitalizeServer.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . i o . B u f f e r e d R e a d e r i m p o r t j a v a . i o . I O E x c e p t i o n i m p o r t j a v a . i o . I n p u t S t r e a m R e a d e r i m p o r t j a v a . i o . P r i n t W r i t e r i m p o r t j a v a . n e t . S e r v e r S o c k e t i m p o r t j a v a . n e t . S o c k e t / * * * A s e r v e r p r o g r a m w h i c h a c c e p t s r e q u e s t s f r o m c l i e n t s t o * c a p i t a l i z e s t r i n g s . W h e n c l i e n t s c o n n e c t , a n e w t h r e a d i s * s t a r t e d t o h a n d l e a n i n t e r a c t i v e d i a l o g i n w h i c h t h e c l i e n t * s e n d s i n a s t r i n g a n d t h e s e r v e r t h r e a d s e n d s b a c k t h e * c a p i t a l i z e d v e r s i o n o f t h e s t r i n g . * * T h e p r o g r a m i s r u n s i n a n i n f i n i t e l o o p , s o s h u t d o w n i n p l a t f o r m * d e p e n d e n t . I f y o u r a n i t f r o m a c o n s o l e w i n d o w w i t h t h e " j a v a " * i n t e r p r e t e r , C t r l + C g e n e r a l l y w i l l s h u t i t d o w n . * /

p u b l i c c l a s s C a p i t a l i z e S e r v e r { / * * * A p p l i c a t i o n m e t h o d t o r u n t h e s e r v e r r u n s i n a n i n f i n i t e l o o p * l i s t e n i n g o n p o r t 9 8 9 8 . W h e n a c o n n e c t i o n i s r e q u e s t e d , i t * s p a w n s a n e w t h r e a d t o d o t h e s e r v i c i n g a n d i m m e d i a t e l y r e t u r n s * t o l i s t e n i n g . T h e s e r v e r k e e p s a u n i q u e c l i e n t n u m b e r f o r e a c h * c l i e n t t h a t c o n n e c t s j u s t t o s h o w i n t e r e s t i n g l o g g i n g * m e s s a g e s . I t i s c e r t a i n l y n o t n e c e s s a r y t o d o t h i s . * / 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 ) t h r o w s E x c e p t i o n { S y s t e m . o u t . p r i n t l n ( " T h e c a p i t a l i z a t i o n s e r v e r i s r u n n i n g . " ) i n t c l i e n t N u m b e r = 0 S e r v e r S o c k e t l i s t e n e r = n e w S e r v e r S o c k e t ( 9 8 9 8 ) t r y { w h i l e ( t r u e ) { n e w C a p i t a l i z e r ( l i s t e n e r . a c c e p t ( ) , c l i e n t N u m b e r + + ) . s t a r t ( ) } } f i n a l l y { l i s t e n e r . c l o s e ( ) } } / * * * A p r i v a t e t h r e a d t o h a n d l e c a p i t a l i z a t i o n r e q u e s t s o n a p a r t i c u l a r * s o c k e t . T h e c l i e n t t e r m i n a t e s t h e d i a l o g u e b y s e n d i n g a s i n g l e l i n e * c o n t a i n i n g o n l y a p e r i o d . * / p r i v a t e s t a t i c c l a s s C a p i t a l i z e r e x t e n d s T h r e a d { p r i v a t e S o c k e t s o c k e t p r i v a t e i n t c l i e n t N u m b e r p u b l i c C a p i t a l i z e r ( S o c k e t s o c k e t , i n t c l i e n t N u m b e r ) { t h i s . s o c k e t = s o c k e t t h i s . c l i e n t N u m b e r = c l i e n t N u m b e r l o g ( " N e w c o n n e c t i o n w i t h c l i e n t # " + c l i e n t N u m b e r + " a t " + s o c k e t ) } / * * * S e r v i c e s t h i s t h r e a d ' s c l i e n t b y f i r s t s e n d i n g t h e * c l i e n t a w e l c o m e m e s s a g e t h e n r e p e a t e d l y r e a d i n g s t r i n g s * a n d s e n d i n g b a c k t h e c a p i t a l i z e d v e r s i o n o f t h e s t r i n g . * / p u b l i c v o i d r u n ( ) { t r y { / / D e c o r a t e t h e s t r e a m s s o w e c a n s e n d c h a r a c t e r s / / a n d n o t j u s t b y t e s . E n s u r e o u t p u t i s f l u s h e d / / a f t e r e v e r y n e w l i n e . B u f f e r e d R e a d e r i n = n e w B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( s o c k e t . g e t I n p u t S t r e a m ( ) ) ) P r i n t W r i t e r o u t = n e w P r i n t W r i t e r ( s o c k e t . g e t O u t p u t S t r e a m ( ) , t r u e ) / / S e n d a w e l c o m e m e s s a g e t o t h e c l i e n t . o u t . p r i n t l n ( " H e l l o , y o u a r e c l i e n t # " + c l i e n t N u m b e r + " . " ) o u t . p r i n t l n ( " E n t e r a l i n e w i t h o n l y a p e r i o d t o q u i t \ n " ) / / G e t m e s s a g e s f r o m t h e c l i e n t , l i n e b y l i n e r e t u r n t h e m / / c a p i t a l i z e d

w h i l e ( t r u e ) { S t r i n g i n p u t = i n . r e a d L i n e ( ) i f ( i n p u t = = n u l l | | i n p u t . e q u a l s ( " . " ) ) { b r e a k } o u t . p r i n t l n ( i n p u t . t o U p p e r C a s e ( ) ) } } c a t c h ( I O E x c e p t i o n e ) { l o g ( " E r r o r h a n d l i n g c l i e n t # " + c l i e n t N u m b e r + " : " + e ) } f i n a l l y { t r y { s o c k e t . c l o s e ( ) } c a t c h ( I O E x c e p t i o n e ) { l o g ( " C o u l d n ' t c l o s e a s o c k e t , w h a t ' s g o i n g o n ? " ) } l o g ( " C o n n e c t i o n w i t h c l i e n t # " + c l i e n t N u m b e r + " c l o s e d " ) } } / * * * L o g s a s i m p l e m e s s a g e . I n t h i s c a s e w e j u s t w r i t e t h e * m e s s a g e t o t h e s e r v e r a p p l i c a t i o n s s t a n d a r d o u t p u t . * / p r i v a t e v o i d l o g ( S t r i n g m e s s a g e ) { S y s t e m . o u t . p r i n t l n ( m e s s a g e ) } } }

Theclient
CapitalizeClient.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . a w t . e v e n t . A c t i o n E v e n t i m p o r t j a v a . a w t . e v e n t . A c t i o n L i s t e n e r i m p o r t j a v a . i o . B u f f e r e d R e a d e r i m p o r t j a v a . i o . I O E x c e p t i o n i m p o r t j a v a . i o . I n p u t S t r e a m R e a d e r i m p o r t j a v a . i o . P r i n t W r i t e r i m p o r t j a v a . n e t . S o c k e t i m p o r t j a v a x . s w i n g . J F r a m e i m p o r t j a v a x . s w i n g . J O p t i o n P a n e i m p o r t j a v a x . s w i n g . J S c r o l l P a n e i m p o r t j a v a x . s w i n g . J T e x t A r e a i m p o r t j a v a x . s w i n g . J T e x t F i e l d / * * * A s i m p l e S w i n g b a s e d c l i e n t f o r t h e c a p i t a l i z a t i o n s e r v e r . * I t h a s a m a i n f r a m e w i n d o w w i t h a t e x t f i e l d f o r e n t e r i n g * s t r i n g s a n d a t e x t a r e a t o s e e t h e r e s u l t s o f c a p i t a l i z i n g * t h e m . * / p u b l i c c l a s s C a p i t a l i z e C l i e n t { p r i v a t e B u f f e r e d R e a d e r i n p r i v a t e P r i n t W r i t e r o u t

p r i v a t e J F r a m e f r a m e = n e w J F r a m e ( " C a p i t a l i z e C l i e n t " ) p r i v a t e J T e x t F i e l d d a t a F i e l d = n e w J T e x t F i e l d ( 4 0 ) p r i v a t e J T e x t A r e a m e s s a g e A r e a = n e w J T e x t A r e a ( 8 , 6 0 ) / * * * C o n s t r u c t s t h e c l i e n t b y l a y i n g o u t t h e G U I a n d r e g i s t e r i n g a * l i s t e n e r w i t h t h e t e x t f i e l d s o t h a t p r e s s i n g E n t e r i n t h e * l i s t e n e r s e n d s t h e t e x t f i e l d c o n t e n t s t o t h e s e r v e r . * / p u b l i c C a p i t a l i z e C l i e n t ( ) { / / L a y o u t G U I m e s s a g e A r e a . s e t E d i t a b l e ( f a l s e ) f r a m e . g e t C o n t e n t P a n e ( ) . a d d ( d a t a F i e l d , " N o r t h " ) f r a m e . g e t C o n t e n t P a n e ( ) . a d d ( n e w J S c r o l l P a n e ( m e s s a g e A r e a ) , " C e n t e r " ) / / A d d L i s t e n e r s d a t a F i e l d . a d d A c t i o n L i s t e n e r ( n e w A c t i o n L i s t e n e r ( ) { / * * * R e s p o n d s t o p r e s s i n g t h e e n t e r k e y i n t h e t e x t f i e l d * b y s e n d i n g t h e c o n t e n t s o f t h e t e x t f i e l d t o t h e * s e r v e r a n d d i s p l a y i n g t h e r e s p o n s e f r o m t h e s e r v e r * i n t h e t e x t a r e a . I f t h e r e s p o n s e i s " . " w e e x i t * t h e w h o l e a p p l i c a t i o n , w h i c h c l o s e s a l l s o c k e t s , * s t r e a m s a n d w i n d o w s . * / p u b l i c v o i d a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) { o u t . p r i n t l n ( d a t a F i e l d . g e t T e x t ( ) ) S t r i n g r e s p o n s e t r y { r e s p o n s e = i n . r e a d L i n e ( ) i f ( r e s p o n s e = = n u l l | | r e s p o n s e . e q u a l s ( " " ) ) { S y s t e m . e x i t ( 0 ) } } c a t c h ( I O E x c e p t i o n e x ) { r e s p o n s e = " E r r o r : " + e x } m e s s a g e A r e a . a p p e n d ( r e s p o n s e + " \ n " ) d a t a F i e l d . s e l e c t A l l ( ) } } ) } / * * * I m p l e m e n t s t h e c o n n e c t i o n l o g i c b y p r o m p t i n g t h e e n d u s e r f o r * t h e s e r v e r ' s I P a d d r e s s , c o n n e c t i n g , s e t t i n g u p s t r e a m s , a n d * c o n s u m i n g t h e w e l c o m e m e s s a g e s f r o m t h e s e r v e r . T h e C a p i t a l i z e r * p r o t o c o l s a y s t h a t t h e s e r v e r s e n d s t h r e e l i n e s o f t e x t t o t h e * c l i e n t i m m e d i a t e l y a f t e r e s t a b l i s h i n g a c o n n e c t i o n . * / p u b l i c v o i d c o n n e c t T o S e r v e r ( ) t h r o w s I O E x c e p t i o n { / / G e t t h e s e r v e r a d d r e s s f r o m a d i a l o g b o x . S t r i n g s e r v e r A d d r e s s = J O p t i o n P a n e . s h o w I n p u t D i a l o g ( f r a m e , " E n t e r I P A d d r e s s o f t h e S e r v e r : " , " W e l c o m e t o t h e C a p i t a l i z a t i o n P r o g r a m " , J O p t i o n P a n e . Q U E S T I O N _ M E S S A G E )

/ / M a k e c o n n e c t i o n a n d i n i t i a l i z e s t r e a m s S o c k e t s o c k e t = n e w S o c k e t ( s e r v e r A d d r e s s , 9 8 9 8 ) i n = n e w B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( s o c k e t . g e t I n p u t S t r e a m ( ) ) ) o u t = n e w P r i n t W r i t e r ( s o c k e t . g e t O u t p u t S t r e a m ( ) , t r u e ) / / C o n s u m e t h e i n i t i a l w e l c o m i n g m e s s a g e s f r o m t h e s e r v e r f o r ( i n t i = 0 i < 3 i + + ) { m e s s a g e A r e a . a p p e n d ( i n . r e a d L i n e ( ) + " \ n " ) } } / * * * R u n s t h e c l i e n t a p p l i c a t i o n . * / 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 ) t h r o w s E x c e p t i o n { C a p i t a l i z e C l i e n t c l i e n t = n e w C a p i t a l i z e C l i e n t ( ) c l i e n t . f r a m e . s e t D e f a u l t C l o s e O p e r a t i o n ( J F r a m e . E X I T _ O N _ C L O S E ) c l i e n t . f r a m e . p a c k ( ) c l i e n t . f r a m e . s e t V i s i b l e ( t r u e ) c l i e n t . c o n n e c t T o S e r v e r ( ) } }

ATwoPlayerNetworkedTicTacToeGame
Theserver
TicTacToeServer.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . i o . B u f f e r e d R e a d e r i m p o r t j a v a . i o . I O E x c e p t i o n i m p o r t j a v a . i o . I n p u t S t r e a m R e a d e r i m p o r t j a v a . i o . P r i n t W r i t e r i m p o r t j a v a . n e t . S e r v e r S o c k e t i m p o r t j a v a . n e t . S o c k e t / * * * A s e r v e r f o r a n e t w o r k m u l t i p l a y e r t i c t a c t o e g a m e . M o d i f i e d a n d * e x t e n d e d f r o m t h e c l a s s p r e s e n t e d i n D e i t e l a n d D e i t e l " J a v a H o w t o * P r o g r a m " b o o k . I m a d e a b u n c h o f e n h a n c e m e n t s a n d r e w r o t e l a r g e s e c t i o n s * o f t h e c o d e . T h e m a i n c h a n g e i s i n s t e a d o f p a s s i n g * d a t a * b e t w e e n t h e * c l i e n t a n d s e r v e r , I m a d e a T T T P ( t i c t a c t o e p r o t o c o l ) w h i c h i s t o t a l l y * p l a i n t e x t , s o y o u c a n t e s t t h e g a m e w i t h T e l n e t ( a l w a y s a g o o d i d e a . ) * T h e s t r i n g s t h a t a r e s e n t i n T T T P a r e : * * C l i e n t > S e r v e r S e r v e r > C l i e n t * * M O V E < n > ( 0 < = n < = 8 ) W E L C O M E < c h a r > ( c h a r i n { X , O } ) * Q U I T V A L I D _ M O V E * O T H E R _ P L A Y E R _ M O V E D < n > * V I C T O R Y * D E F E A T

* T I E * M E S S A G E < t e x t > * * A s e c o n d c h a n g e i s t h a t i t a l l o w s a n u n l i m i t e d n u m b e r o f p a i r s o f * p l a y e r s t o p l a y . * / p u b l i c c l a s s T i c T a c T o e S e r v e r { / * * * R u n s t h e a p p l i c a t i o n . P a i r s u p c l i e n t s t h a t c o n n e c t . * / 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 ) t h r o w s E x c e p t i o n { S e r v e r S o c k e t l i s t e n e r = n e w S e r v e r S o c k e t ( 8 9 0 1 ) S y s t e m . o u t . p r i n t l n ( " T i c T a c T o e S e r v e r i s R u n n i n g " ) t r y { w h i l e ( t r u e ) { G a m e g a m e = n e w G a m e ( ) G a m e . P l a y e r p l a y e r X = g a m e . n e w P l a y e r ( l i s t e n e r . a c c e p t ( ) , ' X ' ) G a m e . P l a y e r p l a y e r O = g a m e . n e w P l a y e r ( l i s t e n e r . a c c e p t ( ) , ' O ' ) p l a y e r X . s e t O p p o n e n t ( p l a y e r O ) p l a y e r O . s e t O p p o n e n t ( p l a y e r X ) g a m e . c u r r e n t P l a y e r = p l a y e r X p l a y e r X . s t a r t ( ) p l a y e r O . s t a r t ( ) } } f i n a l l y { l i s t e n e r . c l o s e ( ) } } } / * * * A t w o p l a y e r g a m e . * / c l a s s G a m e { / * * * A b o a r d h a s n i n e s q u a r e s . E a c h s q u a r e i s e i t h e r u n o w n e d o r * i t i s o w n e d b y a p l a y e r . S o w e u s e a s i m p l e a r r a y o f p l a y e r * r e f e r e n c e s . I f n u l l , t h e c o r r e s p o n d i n g s q u a r e i s u n o w n e d , * o t h e r w i s e t h e a r r a y c e l l s t o r e s a r e f e r e n c e t o t h e p l a y e r t h a t * o w n s i t . * / p r i v a t e P l a y e r [ ] b o a r d = { n u l l , n u l l , n u l l , n u l l , n u l l , n u l l , n u l l , n u l l , n u l l } / * * * T h e c u r r e n t p l a y e r . * / P l a y e r c u r r e n t P l a y e r / * * * R e t u r n s w h e t h e r t h e c u r r e n t s t a t e o f t h e b o a r d i s s u c h t h a t o n e * o f t h e p l a y e r s i s a w i n n e r . * / p u b l i c b o o l e a n h a s W i n n e r ( ) { r e t u r n

( b o a r d [ 0 ] ! = n u l l & & b o a r d [ 0 ] = = b o a r d [ 1 ] & & b o a r d [ 0 ] = = b o a r d [ 2 ] ) | | ( b o a r d [ 3 ] ! = n u l l & & b o a r d [ 3 ] = = b o a r d [ 4 ] & & b o a r d [ 3 ] = = b o a r d [ 5 ] ) | | ( b o a r d [ 6 ] ! = n u l l & & b o a r d [ 6 ] = = b o a r d [ 7 ] & & b o a r d [ 6 ] = = b o a r d [ 8 ] ) | | ( b o a r d [ 0 ] ! = n u l l & & b o a r d [ 0 ] = = b o a r d [ 3 ] & & b o a r d [ 0 ] = = b o a r d [ 6 ] ) | | ( b o a r d [ 1 ] ! = n u l l & & b o a r d [ 1 ] = = b o a r d [ 4 ] & & b o a r d [ 1 ] = = b o a r d [ 7 ] ) | | ( b o a r d [ 2 ] ! = n u l l & & b o a r d [ 2 ] = = b o a r d [ 5 ] & & b o a r d [ 2 ] = = b o a r d [ 8 ] ) | | ( b o a r d [ 0 ] ! = n u l l & & b o a r d [ 0 ] = = b o a r d [ 4 ] & & b o a r d [ 0 ] = = b o a r d [ 8 ] ) | | ( b o a r d [ 2 ] ! = n u l l & & b o a r d [ 2 ] = = b o a r d [ 4 ] & & b o a r d [ 2 ] = = b o a r d [ 6 ] ) } / * * * R e t u r n s w h e t h e r t h e r e a r e n o m o r e e m p t y s q u a r e s . * / p u b l i c b o o l e a n b o a r d F i l l e d U p ( ) { f o r ( i n t i = 0 i < b o a r d . l e n g t h i + + ) { i f ( b o a r d [ i ] = = n u l l ) { r e t u r n f a l s e } } r e t u r n t r u e } / * * * C a l l e d b y t h e p l a y e r t h r e a d s w h e n a p l a y e r t r i e s t o m a k e a * m o v e . T h i s m e t h o d c h e c k s t o s e e i f t h e m o v e i s l e g a l : t h a t * i s , t h e p l a y e r r e q u e s t i n g t h e m o v e m u s t b e t h e c u r r e n t p l a y e r * a n d t h e s q u a r e i n w h i c h s h e i s t r y i n g t o m o v e m u s t n o t a l r e a d y * b e o c c u p i e d . I f t h e m o v e i s l e g a l t h e g a m e s t a t e i s u p d a t e d * ( t h e s q u a r e i s s e t a n d t h e n e x t p l a y e r b e c o m e s c u r r e n t ) a n d * t h e o t h e r p l a y e r i s n o t i f i e d o f t h e m o v e s o i t c a n u p d a t e i t s * c l i e n t . * / p u b l i c s y n c h r o n i z e d b o o l e a n l e g a l M o v e ( i n t l o c a t i o n , P l a y e r p l a y e r ) { i f ( p l a y e r = = c u r r e n t P l a y e r & & b o a r d [ l o c a t i o n ] = = n u l l ) { b o a r d [ l o c a t i o n ] = c u r r e n t P l a y e r c u r r e n t P l a y e r = c u r r e n t P l a y e r . o p p o n e n t c u r r e n t P l a y e r . o t h e r P l a y e r M o v e d ( l o c a t i o n ) r e t u r n t r u e } r e t u r n f a l s e } / * * * T h e c l a s s f o r t h e h e l p e r t h r e a d s i n t h i s m u l t i t h r e a d e d s e r v e r * a p p l i c a t i o n . A P l a y e r i s i d e n t i f i e d b y a c h a r a c t e r m a r k * w h i c h i s e i t h e r ' X ' o r ' O ' . F o r c o m m u n i c a t i o n w i t h t h e * c l i e n t t h e p l a y e r h a s a s o c k e t w i t h i t s i n p u t a n d o u t p u t * s t r e a m s . S i n c e o n l y t e x t i s b e i n g c o m m u n i c a t e d w e u s e a * r e a d e r a n d a w r i t e r . * / c l a s s P l a y e r e x t e n d s T h r e a d { c h a r m a r k P l a y e r o p p o n e n t S o c k e t s o c k e t B u f f e r e d R e a d e r i n p u t P r i n t W r i t e r o u t p u t / * * * C o n s t r u c t s a h a n d l e r t h r e a d f o r a g i v e n s o c k e t a n d m a r k

* i n i t i a l i z e s t h e s t r e a m f i e l d s , d i s p l a y s t h e f i r s t t w o * w e l c o m i n g m e s s a g e s . * / p u b l i c P l a y e r ( S o c k e t s o c k e t , c h a r m a r k ) { t h i s . s o c k e t = s o c k e t t h i s . m a r k = m a r k t r y { i n p u t = n e w B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( s o c k e t . g e t I n p u t S t r e a m ( ) ) ) o u t p u t = n e w P r i n t W r i t e r ( s o c k e t . g e t O u t p u t S t r e a m ( ) , t r u e ) o u t p u t . p r i n t l n ( " W E L C O M E " + m a r k ) o u t p u t . p r i n t l n ( " M E S S A G E W a i t i n g f o r o p p o n e n t t o c o n n e c t " ) } c a t c h ( I O 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 ( " P l a y e r d i e d : " + e ) } } / * * * A c c e p t s n o t i f i c a t i o n o f w h o t h e o p p o n e n t i s . * / p u b l i c v o i d s e t O p p o n e n t ( P l a y e r o p p o n e n t ) { t h i s . o p p o n e n t = o p p o n e n t } / * * * H a n d l e s t h e o t h e r P l a y e r M o v e d m e s s a g e . * / p u b l i c v o i d o t h e r P l a y e r M o v e d ( i n t l o c a t i o n ) { o u t p u t . p r i n t l n ( " O P P O N E N T _ M O V E D " + l o c a t i o n ) o u t p u t . p r i n t l n ( h a s W i n n e r ( ) ? " D E F E A T " : b o a r d F i l l e d U p ( ) ? " T I E " : " " ) } / * * * T h e r u n m e t h o d o f t h i s t h r e a d . * / p u b l i c v o i d r u n ( ) { t r y { / / T h e t h r e a d i s o n l y s t a r t e d a f t e r e v e r y o n e c o n n e c t s . o u t p u t . p r i n t l n ( " M E S S A G E A l l p l a y e r s c o n n e c t e d " ) / / T e l l t h e f i r s t p l a y e r t h a t i t i s h e r t u r n . i f ( m a r k = = ' X ' ) { o u t p u t . p r i n t l n ( " M E S S A G E Y o u r m o v e " ) } / / R e p e a t e d l y g e t c o m m a n d s f r o m t h e c l i e n t a n d p r o c e s s t h e m . w h i l e ( t r u e ) { S t r i n g c o m m a n d = i n p u t . r e a d L i n e ( ) i f ( c o m m a n d . s t a r t s W i t h ( " M O V E " ) ) { i n t l o c a t i o n = I n t e g e r . p a r s e I n t ( c o m m a n d . s u b s t r i n g ( 5 ) ) i f ( l e g a l M o v e ( l o c a t i o n , t h i s ) ) { o u t p u t . p r i n t l n ( " V A L I D _ M O V E " ) o u t p u t . p r i n t l n ( h a s W i n n e r ( ) ? " V I C T O R Y " : b o a r d F i l l e d U p ( ) ? " T I E " : " " ) } e l s e { o u t p u t . p r i n t l n ( " M E S S A G E ? " ) }

} e l s e i f ( c o m m a n d . s t a r t s W i t h ( " Q U I T " ) ) { r e t u r n } } } c a t c h ( I O 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 ( " P l a y e r d i e d : " + e ) } f i n a l l y { t r y { s o c k e t . c l o s e ( ) } c a t c h ( I O E x c e p t i o n e ) { } } } } }

Theclient
TicTacToeClient.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . a w t . C o l o r i m p o r t j a v a . a w t . G r i d L a y o u t i m p o r t j a v a . a w t . e v e n t . M o u s e A d a p t e r i m p o r t j a v a . a w t . e v e n t . M o u s e E v e n t i m p o r t j a v a . i o . B u f f e r e d R e a d e r i m p o r t j a v a . i o . I n p u t S t r e a m R e a d e r i m p o r t j a v a . i o . P r i n t W r i t e r i m p o r t j a v a . n e t . S o c k e t i m p o r t j a v a x . s w i n g . I c o n i m p o r t j a v a x . s w i n g . I m a g e I c o n i m p o r t j a v a x . s w i n g . J F r a m e i m p o r t j a v a x . s w i n g . J L a b e l i m p o r t j a v a x . s w i n g . J O p t i o n P a n e i m p o r t j a v a x . s w i n g . J P a n e l / * * * A c l i e n t f o r t h e T i c T a c T o e g a m e , m o d i f i e d a n d e x t e n d e d f r o m t h e * c l a s s p r e s e n t e d i n D e i t e l a n d D e i t e l " J a v a H o w t o P r o g r a m " b o o k . * I m a d e a b u n c h o f e n h a n c e m e n t s a n d r e w r o t e l a r g e s e c t i o n s o f t h e * c o d e . I n p a r t i c u l a r I c r e a t e d t h e T T T P ( T i c T a c T o e P r o t o c o l ) * w h i c h i s e n t i r e l y t e x t b a s e d . H e r e a r e t h e s t r i n g s t h a t a r e s e n t : * * C l i e n t > S e r v e r S e r v e r > C l i e n t * * M O V E < n > ( 0 < = n < = 8 ) W E L C O M E < c h a r > ( c h a r i n { X , O } ) * Q U I T V A L I D _ M O V E * O T H E R _ P L A Y E R _ M O V E D < n > * V I C T O R Y * D E F E A T * T I E * M E S S A G E < t e x t > * * / p u b l i c c l a s s T i c T a c T o e C l i e n t { p r i v a t e J F r a m e f r a m e = n e w J F r a m e ( " T i c T a c T o e " ) p r i v a t e J L a b e l m e s s a g e L a b e l = n e w J L a b e l ( " " ) p r i v a t e I m a g e I c o n i c o n p r i v a t e I m a g e I c o n o p p o n e n t I c o n

p r i v a t e S q u a r e [ ] b o a r d = n e w S q u a r e [ 9 ] p r i v a t e S q u a r e c u r r e n t S q u a r e p r i v a t e s t a t i c i n t P O R T = 8 9 0 1 p r i v a t e S o c k e t s o c k e t p r i v a t e B u f f e r e d R e a d e r i n p r i v a t e P r i n t W r i t e r o u t / * * * C o n s t r u c t s t h e c l i e n t b y c o n n e c t i n g t o a s e r v e r , l a y i n g o u t t h e * G U I a n d r e g i s t e r i n g G U I l i s t e n e r s . * / p u b l i c T i c T a c T o e C l i e n t ( S t r i n g s e r v e r A d d r e s s ) t h r o w s E x c e p t i o n { / / S e t u p n e t w o r k i n g s o c k e t = n e w S o c k e t ( s e r v e r A d d r e s s , P O R T ) i n = n e w B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( s o c k e t . g e t I n p u t S t r e a m ( ) ) ) o u t = n e w P r i n t W r i t e r ( s o c k e t . g e t O u t p u t S t r e a m ( ) , t r u e ) / / L a y o u t G U I m e s s a g e L a b e l . s e t B a c k g r o u n d ( C o l o r . l i g h t G r a y ) f r a m e . g e t C o n t e n t P a n e ( ) . a d d ( m e s s a g e L a b e l , " S o u t h " ) J P a n e l b o a r d P a n e l = n e w J P a n e l ( ) b o a r d P a n e l . s e t B a c k g r o u n d ( C o l o r . b l a c k ) b o a r d P a n e l . s e t L a y o u t ( n e w G r i d L a y o u t ( 3 , 3 , 2 , 2 ) ) f o r ( i n t i = 0 i < b o a r d . l e n g t h i + + ) { f i n a l i n t j = i b o a r d [ i ] = n e w S q u a r e ( ) b o a r d [ i ] . a d d M o u s e L i s t e n e r ( n e w M o u s e A d a p t e r ( ) { p u b l i c v o i d m o u s e P r e s s e d ( M o u s e E v e n t e ) { c u r r e n t S q u a r e = b o a r d [ j ] o u t . p r i n t l n ( " M O V E " + j ) } } ) b o a r d P a n e l . a d d ( b o a r d [ i ] ) } f r a m e . g e t C o n t e n t P a n e ( ) . a d d ( b o a r d P a n e l , " C e n t e r " ) } / * * * T h e m a i n t h r e a d o f t h e c l i e n t w i l l l i s t e n f o r m e s s a g e s * f r o m t h e s e r v e r . T h e f i r s t m e s s a g e w i l l b e a " W E L C O M E " * m e s s a g e i n w h i c h w e r e c e i v e o u r m a r k . T h e n w e g o i n t o a * l o o p l i s t e n i n g f o r " V A L I D _ M O V E " , " O P P O N E N T _ M O V E D " , " V I C T O R Y " , * " D E F E A T " , " T I E " , " O P P O N E N T _ Q U I T o r " M E S S A G E " m e s s a g e s , * a n d h a n d l i n g e a c h m e s s a g e a p p r o p r i a t e l y . T h e " V I C T O R Y " , * " D E F E A T " a n d " T I E " a s k t h e u s e r w h e t h e r o r n o t t o p l a y * a n o t h e r g a m e . I f t h e a n s w e r i s n o , t h e l o o p i s e x i t e d a n d * t h e s e r v e r i s s e n t a " Q U I T " m e s s a g e . I f a n O P P O N E N T _ Q U I T * m e s s a g e i s r e c e v i e d t h e n t h e l o o p w i l l e x i t a n d t h e s e r v e r * w i l l b e s e n t a " Q U I T " m e s s a g e a l s o . * / p u b l i c v o i d p l a y ( ) t h r o w s E x c e p t i o n { S t r i n g r e s p o n s e t r y { r e s p o n s e = i n . r e a d L i n e ( ) i f ( r e s p o n s e . s t a r t s W i t h ( " W E L C O M E " ) ) { c h a r m a r k = r e s p o n s e . c h a r A t ( 8 )

i c o n = n e w I m a g e I c o n ( m a r k = = ' X ' ? " x . g i f " : " o . g i f " ) o p p o n e n t I c o n = n e w I m a g e I c o n ( m a r k = = ' X ' ? " o . g i f " : " x . g i f " ) f r a m e . s e t T i t l e ( " T i c T a c T o e P l a y e r " + m a r k ) } w h i l e ( t r u e ) { r e s p o n s e = i n . r e a d L i n e ( ) i f ( r e s p o n s e . s t a r t s W i t h ( " V A L I D _ M O V E " ) ) { m e s s a g e L a b e l . s e t T e x t ( " V a l i d m o v e , p l e a s e w a i t " ) c u r r e n t S q u a r e . s e t I c o n ( i c o n ) c u r r e n t S q u a r e . r e p a i n t ( ) } e l s e i f ( r e s p o n s e . s t a r t s W i t h ( " O P P O N E N T _ M O V E D " ) ) { i n t l o c = I n t e g e r . p a r s e I n t ( r e s p o n s e . s u b s t r i n g ( 1 5 ) ) b o a r d [ l o c ] . s e t I c o n ( o p p o n e n t I c o n ) b o a r d [ l o c ] . r e p a i n t ( ) m e s s a g e L a b e l . s e t T e x t ( " O p p o n e n t m o v e d , y o u r t u r n " ) } e l s e i f ( r e s p o n s e . s t a r t s W i t h ( " V I C T O R Y " ) ) { m e s s a g e L a b e l . s e t T e x t ( " Y o u w i n " ) b r e a k } e l s e i f ( r e s p o n s e . s t a r t s W i t h ( " D E F E A T " ) ) { m e s s a g e L a b e l . s e t T e x t ( " Y o u l o s e " ) b r e a k } e l s e i f ( r e s p o n s e . s t a r t s W i t h ( " T I E " ) ) { m e s s a g e L a b e l . s e t T e x t ( " Y o u t i e d " ) b r e a k } e l s e i f ( r e s p o n s e . s t a r t s W i t h ( " M E S S A G E " ) ) { m e s s a g e L a b e l . s e t T e x t ( r e s p o n s e . s u b s t r i n g ( 8 ) ) } } o u t . p r i n t l n ( " Q U I T " ) } f i n a l l y { s o c k e t . c l o s e ( ) } } p r i v a t e b o o l e a n w a n t s T o P l a y A g a i n ( ) { i n t r e s p o n s e = J O p t i o n P a n e . s h o w C o n f i r m D i a l o g ( f r a m e , " W a n t t o p l a y a g a i n ? " , " T i c T a c T o e i s F u n F u n F u n " , J O p t i o n P a n e . Y E S _ N O _ O P T I O N ) f r a m e . d i s p o s e ( ) r e t u r n r e s p o n s e = = J O p t i o n P a n e . Y E S _ O P T I O N } / * * * G r a p h i c a l s q u a r e i n t h e c l i e n t w i n d o w . E a c h s q u a r e i s * a w h i t e p a n e l c o n t a i n i n g . A c l i e n t c a l l s s e t I c o n ( ) t o f i l l * i t w i t h a n I c o n , p r e s u m a b l y a n X o r O . * / s t a t i c c l a s s S q u a r e e x t e n d s J P a n e l { J L a b e l l a b e l = n e w J L a b e l ( ( I c o n ) n u l l ) p u b l i c S q u a r e ( ) { s e t B a c k g r o u n d ( C o l o r . w h i t e ) a d d ( l a b e l ) } p u b l i c v o i d s e t I c o n ( I c o n i c o n ) { l a b e l . s e t I c o n ( i c o n )

} } / * * * R u n s t h e c l i e n t a s a n a p p l i c a t i o n . * / 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 ) t h r o w s E x c e p t i o n { w h i l e ( t r u e ) { S t r i n g s e r v e r A d d r e s s = ( a r g s . l e n g t h = = 0 ) ? " l o c a l h o s t " : a r g s [ 1 ] T i c T a c T o e C l i e n t c l i e n t = n e w T i c T a c T o e C l i e n t ( s e r v e r A d d r e s s ) c l i e n t . f r a m e . s e t D e f a u l t C l o s e O p e r a t i o n ( J F r a m e . E X I T _ O N _ C L O S E ) c l i e n t . f r a m e . s e t S i z e ( 2 4 0 , 1 6 0 ) c l i e n t . f r a m e . s e t V i s i b l e ( t r u e ) c l i e n t . f r a m e . s e t R e s i z a b l e ( f a l s e ) c l i e n t . p l a y ( ) i f ( ! c l i e n t . w a n t s T o P l a y A g a i n ( ) ) { b r e a k } } } }

AMultiUserChatApplication
Theserver
ChatServer.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . i o . B u f f e r e d R e a d e r i m p o r t j a v a . i o . I O E x c e p t i o n i m p o r t j a v a . i o . I n p u t S t r e a m R e a d e r i m p o r t j a v a . i o . P r i n t W r i t e r i m p o r t j a v a . n e t . S e r v e r S o c k e t i m p o r t j a v a . n e t . S o c k e t i m p o r t j a v a . u t i l . H a s h S e t / * * * A m u l t i t h r e a d e d c h a t r o o m s e r v e r . W h e n a c l i e n t c o n n e c t s t h e * s e r v e r r e q u e s t s a s c r e e n n a m e b y s e n d i n g t h e c l i e n t t h e * t e x t " S U B M I T N A M E " , a n d k e e p s r e q u e s t i n g a n a m e u n t i l * a u n i q u e o n e i s r e c e i v e d . A f t e r a c l i e n t s u b m i t s a u n i q u e * n a m e , t h e s e r v e r a c k n o w l e d g e s w i t h " N A M E A C C E P T E D " . T h e n * a l l m e s s a g e s f r o m t h a t c l i e n t w i l l b e b r o a d c a s t t o a l l o t h e r * c l i e n t s t h a t h a v e s u b m i t t e d a u n i q u e s c r e e n n a m e . T h e * b r o a d c a s t m e s s a g e s a r e p r e f i x e d w i t h " M E S S A G E " . * * B e c a u s e t h i s i s j u s t a t e a c h i n g e x a m p l e t o i l l u s t r a t e a s i m p l e * c h a t s e r v e r , t h e r e a r e a f e w f e a t u r e s t h a t h a v e b e e n l e f t o u t . * T w o a r e v e r y u s e f u l a n d b e l o n g i n p r o d u c t i o n c o d e : * * 1 . T h e p r o t o c o l s h o u l d b e e n h a n c e d s o t h a t t h e c l i e n t c a n * s e n d c l e a n d i s c o n n e c t m e s s a g e s t o t h e s e r v e r . *

* 2 . T h e s e r v e r s h o u l d d o s o m e l o g g i n g . * / p u b l i c c l a s s C h a t S e r v e r { / * * * T h e p o r t t h a t t h e s e r v e r l i s t e n s o n . * / p r i v a t e s t a t i c f i n a l i n t P O R T = 9 0 0 1 / * * * T h e s e t o f a l l n a m e s o f c l i e n t s i n t h e c h a t r o o m . M a i n t a i n e d * s o t h a t w e c a n c h e c k t h a t n e w c l i e n t s a r e n o t r e g i s t e r i n g n a m e * a l r e a d y i n u s e . * / p r i v a t e s t a t i c H a s h S e t < S t r i n g > n a m e s = n e w H a s h S e t < S t r i n g > ( ) / * * * T h e s e t o f a l l t h e p r i n t w r i t e r s f o r a l l t h e c l i e n t s . T h i s * s e t i s k e p t s o w e c a n e a s i l y b r o a d c a s t m e s s a g e s . * / p r i v a t e s t a t i c H a s h S e t < P r i n t W r i t e r > w r i t e r s = n e w H a s h S e t < P r i n t W r i t e r > ( ) / * * * T h e a p p p l i c a t i o n m a i n m e t h o d , w h i c h j u s t l i s t e n s o n a p o r t a n d * s p a w n s h a n d l e r t h r e a d s . * / 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 ) t h r o w s E x c e p t i o n { S y s t e m . o u t . p r i n t l n ( " T h e c h a t s e r v e r i s r u n n i n g . " ) S e r v e r S o c k e t l i s t e n e r = n e w S e r v e r S o c k e t ( P O R T ) t r y { w h i l e ( t r u e ) { n e w H a n d l e r ( l i s t e n e r . a c c e p t ( ) ) . s t a r t ( ) } } f i n a l l y { l i s t e n e r . c l o s e ( ) } } / * * * A h a n d l e r t h r e a d c l a s s . H a n d l e r s a r e s p a w n e d f r o m t h e l i s t e n i n g * l o o p a n d a r e r e s p o n s i b l e f o r a d e a l i n g w i t h a s i n g l e c l i e n t * a n d b r o a d c a s t i n g i t s m e s s a g e s . * / p r i v a t e s t a t i c c l a s s H a n d l e r e x t e n d s T h r e a d { p r i v a t e S t r i n g n a m e p r i v a t e S o c k e t s o c k e t p r i v a t e B u f f e r e d R e a d e r i n p r i v a t e P r i n t W r i t e r o u t / * * * C o n s t r u c t s a h a n d l e r t h r e a d , s q u i r r e l i n g a w a y t h e s o c k e t . * A l l t h e i n t e r e s t i n g w o r k i s d o n e i n t h e r u n m e t h o d . * / p u b l i c H a n d l e r ( S o c k e t s o c k e t ) { t h i s . s o c k e t = s o c k e t } / * * * S e r v i c e s t h i s t h r e a d ' s c l i e n t b y r e p e a t e d l y r e q u e s t i n g a

* s c r e e n n a m e u n t i l a u n i q u e o n e h a s b e e n s u b m i t t e d , t h e n * a c k n o w l e d g e s t h e n a m e a n d r e g i s t e r s t h e o u t p u t s t r e a m f o r * t h e c l i e n t i n a g l o b a l s e t , t h e n r e p e a t e d l y g e t s i n p u t s a n d * b r o a d c a s t s t h e m . * / p u b l i c v o i d r u n ( ) { t r y { / / C r e a t e c h a r a c t e r s t r e a m s f o r t h e s o c k e t . i n = n e w B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( s o c k e t . g e t I n p u t S t r e a m ( ) ) ) o u t = n e w P r i n t W r i t e r ( s o c k e t . g e t O u t p u t S t r e a m ( ) , t r u e ) / / R e q u e s t a n a m e f r o m t h i s c l i e n t . K e e p r e q u e s t i n g u n t i l / / a n a m e i s s u b m i t t e d t h a t i s n o t a l r e a d y u s e d . N o t e t h a t / / c h e c k i n g f o r t h e e x i s t e n c e o f a n a m e a n d a d d i n g t h e n a m e / / m u s t b e d o n e w h i l e l o c k i n g t h e s e t o f n a m e s . w h i l e ( t r u e ) { o u t . p r i n t l n ( " S U B M I T N A M E " ) n a m e = i n . r e a d L i n e ( ) i f ( n a m e = = n u l l ) { r e t u r n } s y n c h r o n i z e d ( n a m e s ) { i f ( ! n a m e s . c o n t a i n s ( n a m e ) ) { n a m e s . a d d ( n a m e ) b r e a k } } } / / N o w t h a t a s u c c e s s f u l n a m e h a s b e e n c h o s e n , a d d t h e / / s o c k e t ' s p r i n t w r i t e r t o t h e s e t o f a l l w r i t e r s s o / / t h i s c l i e n t c a n r e c e i v e b r o a d c a s t m e s s a g e s . o u t . p r i n t l n ( " N A M E A C C E P T E D " ) w r i t e r s . a d d ( o u t ) / / A c c e p t m e s s a g e s f r o m t h i s c l i e n t a n d b r o a d c a s t t h e m . / / I g n o r e o t h e r c l i e n t s t h a t c a n n o t b e b r o a d c a s t e d t o . w h i l e ( t r u e ) { S t r i n g i n p u t = i n . r e a d L i n e ( ) i f ( i n p u t = = n u l l ) { r e t u r n } f o r ( P r i n t W r i t e r w r i t e r : w r i t e r s ) { w r i t e r . p r i n t l n ( " M E S S A G E " + n a m e + " : " + i n p u t ) } } } c a t c h ( I O 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 ( e ) } f i n a l l y { / / T h i s c l i e n t i s g o i n g d o w n ! R e m o v e i t s n a m e a n d i t s p r i n t / / w r i t e r f r o m t h e s e t s , a n d c l o s e i t s s o c k e t . i f ( n a m e ! = n u l l ) { n a m e s . r e m o v e ( n a m e ) } i f ( o u t ! = n u l l ) { w r i t e r s . r e m o v e ( o u t ) }

t r y { s o c k e t . c l o s e ( ) } c a t c h ( I O E x c e p t i o n e ) { } } } } }

Theclient
ChatClient.java
p a c k a g e e d u . l m u . c s . n e t w o r k i n g i m p o r t j a v a . a w t . e v e n t . A c t i o n E v e n t i m p o r t j a v a . a w t . e v e n t . A c t i o n L i s t e n e r i m p o r t j a v a . i o . B u f f e r e d R e a d e r i m p o r t j a v a . i o . I O E x c e p t i o n i m p o r t j a v a . i o . I n p u t S t r e a m R e a d e r i m p o r t j a v a . i o . P r i n t W r i t e r i m p o r t j a v a . n e t . S o c k e t i m p o r t j a v a x . s w i n g . J F r a m e i m p o r t j a v a x . s w i n g . J O p t i o n P a n e i m p o r t j a v a x . s w i n g . J S c r o l l P a n e i m p o r t j a v a x . s w i n g . J T e x t A r e a i m p o r t j a v a x . s w i n g . J T e x t F i e l d / * * * A s i m p l e S w i n g b a s e d c l i e n t f o r t h e c h a t s e r v e r . G r a p h i c a l l y * i t i s a f r a m e w i t h a t e x t f i e l d f o r e n t e r i n g m e s s a g e s a n d a * t e x t a r e a t o s e e t h e w h o l e d i a l o g . * * T h e c l i e n t f o l l o w s t h e C h a t P r o t o c o l w h i c h i s a s f o l l o w s . * W h e n t h e s e r v e r s e n d s " S U B M I T N A M E " t h e c l i e n t r e p l i e s w i t h t h e * d e s i r e d s c r e e n n a m e . T h e s e r v e r w i l l k e e p s e n d i n g " S U B M I T N A M E " * r e q u e s t s a s l o n g a s t h e c l i e n t s u b m i t s s c r e e n n a m e s t h a t a r e * a l r e a d y i n u s e . W h e n t h e s e r v e r s e n d s a l i n e b e g i n n i n g * w i t h " N A M E A C C E P T E D " t h e c l i e n t i s n o w a l l o w e d t o s t a r t * s e n d i n g t h e s e r v e r a r b i t r a r y s t r i n g s t o b e b r o a d c a s t t o a l l * c h a t t e r s c o n n e c t e d t o t h e s e r v e r . W h e n t h e s e r v e r s e n d s a * l i n e b e g i n n i n g w i t h " M E S S A G E " t h e n a l l c h a r a c t e r s f o l l o w i n g * t h i s s t r i n g s h o u l d b e d i s p l a y e d i n i t s m e s s a g e a r e a . * / p u b l i c c l a s s C h a t C l i e n t { B u f f e r e d R e a d e r i n P r i n t W r i t e r o u t J F r a m e f r a m e = n e w J F r a m e ( " C h a t t e r " ) J T e x t F i e l d t e x t F i e l d = n e w J T e x t F i e l d ( 4 0 ) J T e x t A r e a m e s s a g e A r e a = n e w J T e x t A r e a ( 8 , 4 0 ) / * * * C o n s t r u c t s t h e c l i e n t b y l a y i n g o u t t h e G U I a n d r e g i s t e r i n g a * l i s t e n e r w i t h t h e t e x t f i e l d s o t h a t p r e s s i n g R e t u r n i n t h e * l i s t e n e r s e n d s t h e t e x t f i e l d c o n t e n t s t o t h e s e r v e r . N o t e * h o w e v e r t h a t t h e t e x t f i e l d i s i n i t i a l l y N O T e d i t a b l e , a n d * o n l y b e c o m e s e d i t a b l e A F T E R t h e c l i e n t r e c e i v e s t h e N A M E A C C E P T E D

* m e s s a g e f r o m t h e s e r v e r . * / p u b l i c C h a t C l i e n t ( ) { / / L a y o u t G U I t e x t F i e l d . s e t E d i t a b l e ( f a l s e ) m e s s a g e A r e a . s e t E d i t a b l e ( f a l s e ) f r a m e . g e t C o n t e n t P a n e ( ) . a d d ( t e x t F i e l d , " N o r t h " ) f r a m e . g e t C o n t e n t P a n e ( ) . a d d ( n e w J S c r o l l P a n e ( m e s s a g e A r e a ) , " C e n t e r " ) f r a m e . p a c k ( ) / / A d d L i s t e n e r s t e x t F i e l d . a d d A c t i o n L i s t e n e r ( n e w A c t i o n L i s t e n e r ( ) { / * * * R e s p o n d s t o p r e s s i n g t h e e n t e r k e y i n t h e t e x t f i e l d b y s e n d i n g * t h e c o n t e n t s o f t h e t e x t f i e l d t o t h e s e r v e r . T h e n c l e a r * t h e t e x t a r e a i n p r e p a r a t i o n f o r t h e n e x t m e s s a g e . * / p u b l i c v o i d a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) { o u t . p r i n t l n ( t e x t F i e l d . g e t T e x t ( ) ) t e x t F i e l d . s e t T e x t ( " " ) } } ) } / * * * P r o m p t f o r a n d r e t u r n t h e a d d r e s s o f t h e s e r v e r . * / p r i v a t e S t r i n g g e t S e r v e r A d d r e s s ( ) { r e t u r n J O p t i o n P a n e . s h o w I n p u t D i a l o g ( f r a m e , " E n t e r I P A d d r e s s o f t h e S e r v e r : " , " W e l c o m e t o t h e C h a t t e r " , J O p t i o n P a n e . Q U E S T I O N _ M E S S A G E ) } / * * * P r o m p t f o r a n d r e t u r n t h e d e s i r e d s c r e e n n a m e . * / p r i v a t e S t r i n g g e t N a m e ( ) { r e t u r n J O p t i o n P a n e . s h o w I n p u t D i a l o g ( f r a m e , " C h o o s e a s c r e e n n a m e : " , " S c r e e n n a m e s e l e c t i o n " , J O p t i o n P a n e . P L A I N _ M E S S A G E ) } / * * * C o n n e c t s t o t h e s e r v e r t h e n e n t e r s t h e p r o c e s s i n g l o o p . * / p r i v a t e v o i d r u n ( ) t h r o w s I O E x c e p t i o n { / / M a k e c o n n e c t i o n a n d i n i t i a l i z e s t r e a m s S t r i n g s e r v e r A d d r e s s = g e t S e r v e r A d d r e s s ( ) S o c k e t s o c k e t = n e w S o c k e t ( s e r v e r A d d r e s s , 9 0 0 1 ) i n = n e w B u f f e r e d R e a d e r ( n e w I n p u t S t r e a m R e a d e r ( s o c k e t . g e t I n p u t S t r e a m ( ) ) ) o u t = n e w P r i n t W r i t e r ( s o c k e t . g e t O u t p u t S t r e a m ( ) , t r u e )

/ / P r o c e s s a l l m e s s a g e s f r o m s e r v e r , a c c o r d i n g t o t h e p r o t o c o l . w h i l e ( t r u e ) { S t r i n g l i n e = i n . r e a d L i n e ( ) i f ( l i n e . s t a r t s W i t h ( " S U B M I T N A M E " ) ) { o u t . p r i n t l n ( g e t N a m e ( ) ) } e l s e i f ( l i n e . s t a r t s W i t h ( " N A M E A C C E P T E D " ) ) { t e x t F i e l d . s e t E d i t a b l e ( t r u e ) } e l s e i f ( l i n e . s t a r t s W i t h ( " M E S S A G E " ) ) { m e s s a g e A r e a . a p p e n d ( l i n e . s u b s t r i n g ( 8 ) + " \ n " ) } } } / * * * R u n s t h e c l i e n t a s a n a p p l i c a t i o n w i t h a c l o s e a b l e f r a m e . * / 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 ) t h r o w s E x c e p t i o n { C h a t C l i e n t c l i e n t = n e w C h a t C l i e n t ( ) c l i e n t . f r a m e . s e t D e f a u l t C l o s e O p e r a t i o n ( J F r a m e . E X I T _ O N _ C L O S E ) c l i e n t . f r a m e . s e t V i s i b l e ( t r u e ) c l i e n t . r u n ( ) } }

You might also like