How to Make an Echo Server

444 views 18 slides Oct 29, 2015
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

A brief presentation about developing a simple echo client/server application, with code snippets.


Slide Content

Introduction
HOW TOMAKE ANECHOSERVER
Muhammad Adil Raja
Roaming Researchers,R.
October 29, 2015

Introduction
OUTLINE
1
INTRODUCTION
2
THEECHOSERVER
3
THEECHOCLIENT
4
REFERENCES

Introduction
OUTLINE
1
INTRODUCTION
2
THEECHOSERVER
3
THEECHOCLIENT
4
REFERENCES

Introduction
OUTLINE
1
INTRODUCTION
2
THEECHOSERVER
3
THEECHOCLIENT
4
REFERENCES

Introduction
OUTLINE
1
INTRODUCTION
2
THEECHOSERVER
3
THEECHOCLIENT
4
REFERENCES

Introduction
INTRODUCTION

Introduction
THEECHOSERVERI
packageecho ;
importj a v a . i o . B u f f e r e d R e a d e r ;
importj a v a . i o . I O E x c e p t i o n ;
importj a v a . i o . I n p u t S t r e a m R e a d e r ;
importj a v a . i o . P r i n t W r i t e r ;
importj a v a . n e t . S e r v e r S o c k e t ;
importj a v a . n e t . S o c k e t ;
importj a v a . u t i l . A r r a y L i s t ;
/
T h i s c l a s s c o n t a i n s t h e programming t h e i m p l e m e n t s t h e model o f an
" Echo S e r v e r " . The echo s e r v e r r e c e i v e s messages and j u s t sends
t h e same message r i g h t back t o t h e c l i e n t t h a t s e n t t h e message
T h i s i s an e x e r c i s e i n s o c k e t s and c l i e n t / s e r v e r s o f t w a r e development .

T h i s s e r v e r c l a s s has no u s e r i n t e r f a c e o f i t s own . I t i s a daemon .

@author k a y d e l l

/
p u b l i c c l a s sEchoServerextendsThread
{
/
T h i s f i e l d r e f e r e n c e s a s e r v e r s o c k e t o b j e c t which t h e s e r v e r w i l l
use t o l i s t e n f o r c o n n e c t i o n r e q u e s t s f r o m c l i e n t s .
/
p r i v a t eS e r v e r S o c k e t s e r v e r S o c k e t ;

Introduction
THEECHOSERVERII
v o l a t i l e booleanh a l t e d =f a l s e;
p r i v a t e s t a t i c i n tn e x t I D = 1 ;
p r i v a t eLog l o g ;
p r i v a t eA r r a y L i s t < C l i e n t S e r v i c e r > c l i e n t S e r v i c e r s =newA r r a y L i s t < C l i e n t S e r v i c e r > ( ) ;
/
T h i s c o n s t r u c t o r c r e a t e s an EchoServer o b j e c t
@param portnum The p o r t t h a t t h e s e r v e r w i l l l i s t e n on .
@throws I O E x c e p t i o n
/
p u b l i cEchoServer (i n tportnum , Log l o g )throwsI O E x c e p t i o n {
t h i s. l o g = l o g ;
l o g . p r i n t l n ( " S :E n t e r i n gt h ec o n s t r u c t o rf o rEchoServer " ) ;
s e r v e r S o c k e t =newS e r v e r S o c k e t ( portnum ) ;
l o g . p r i n t l n ( " S :E x i t i n gt h ec o n s t r u c t o rf o rEchoServer " ) ;
}
/
T h i s method l i s t e n s f o r c o n n e c t i o n r e q u e s t s and when a c o n n e c t i o n
i s made , t h e s e r v e ( ) method i s c a l l e d t o s e r v e t h e c l i e n t .
/
p u b l i c voidr u n ( ) {
System . o u t . p r i n t l n ( " S :E n t e r i n gS e r v e rr u n ( ) " ) ;
w h i l e( ! h a l t e d ) {
t r y{
l o g . p r i n t l n ( " S :a b o u tt oc a l ls e r v e r S o c k e t . a c c e p t ( ) " ) ;
S o c k e t c l i e n t S o c k e t = s e r v e r S o c k e t . a c c e p t ( ) ;
l o g . p r i n t l n ( " S :j u s tr e t u r n e df r o ms e r v e r S o c k e t . a c c e p t ( ) " ) ;

Introduction
THEECHOSERVERIII
l o g . p r i n t l n ( " S :a b o u tt oc r e a t eaC l i e n t S e r v i c e r " ) ;
C l i e n t S e r v i c e r c l i e n t S e r v i c e r =newC l i e n t S e r v i c e r ( c l i e n t S o c k e t , n e x t I D + + ) ;
c l i e n t S e r v i c e r . s t a r t ( ) ;
c l i e n t S e r v i c e r s . add ( c l i e n t S e r v i c e r ) ;
l o g . p r i n t l n ( " S :j u s tr e t u r n e df r o mc r e a t i n gaC l i e n t S e r v i c e r " ) ;
}catch( I O E x c e p t i o n e ) {
System . e r r . p r i n t l n ( " S :E x c e p t i o n :" + e ) ;
e . p r i n t S t a c k T r a c e ( ) ;
break;
}
}
l o g . p r i n t l n ( " S :E x i t i n gS e r v e rr u n ( ) " ) ;
}
p u b l i c voidh a l t ( ) {
h a l t e d =t r u e;
}
p r i v a t e c l a s sC l i e n t S e r v i c e rextendsThread {
p r i v a t e i n ti d ;
p r i v a t eB u f f e r e d R e a d e r i n =n u l l;
p r i v a t eP r i n t W r i t e r o u t =n u l l;
p u b l i cC l i e n t S e r v i c e r ( S o c k e t c l i e n t S o c k e t ,i n ti d )throwsI O E x c e p t i o n {
t h i s. i d = i d ;
l o g . p r i n t l n ( " S :E n t e r i n gt h ec o n s t r u c t o rf o rC l i e n t S e r v i c e r . " ) ;
i n =newB u f f e r e d R e a d e r (newI n p u t S t r e a m R e a d e r ( c l i e n t S o c k e t . g e t I n p u t S t r e a m ( ) ) ) ;
o u t =newP r i n t W r i t e r ( c l i e n t S o c k e t . g e t O u t p u t S t r e a m ( ) , E c h o U t i l s . AUTO_FLUSH ) ;

Introduction
THEECHOSERVERIV
l o g . p r i n t l n ( " S :E x i t i n gt h ec o n s t r u c t o rf o rC l i e n t S e r v i c e r . " ) ;
}
p r i v a t e voids e n d T o C l i e n t ( S t r i n g message ) {
l o g . p r i n t l n ( " Sendingt h emessage :" + message + " t ot h ec l i e n t " ) ;
o u t . p r i n t l n ( message ) ;
l o g . p r i n t l n ( " Sentt h emessage :" + message + " t ot h ec l i e n t " ) ;
}
@Override
p u b l i c voidr u n ( ) {
setName ( " C l i e n t S e r v i c e" + i d ) ;
l o g . p r i n t l n ( " S :E n t e r i n gt h emethods e r v e ( ) " ) ;
t r y{
l o g . p r i n t l n ( " S :Aboutt osendwelcomemessaget ot h ec l i e n t " ) ;
s e n d T o C l i e n t ( " C o n n e c t i o ne s t a b l i s h e d ! " ) ;
l o g . p r i n t l n ( " S :J u s ts e n twelcomemessaget ot h ec l i e n t " ) ;
w h i l e( ! h a l t e d ) {
l o g . p r i n t l n ( " S :a b o u tt or e a dmessagef r o mt h ec l i e n t " ) ;
S t r i n g message = i n . r e a d L i n e ( ) ;
l o g . p r i n t l n ( " S :j u s tr e a dmessagef r o mt h ec l i e n t :" + message ) ;
i f( message ==n u l l) {
l o g . p r i n t l n ( " S :r e a dan u l ls t r i n g ,b r e a k i n go fo fl o o pi nt h es e r v e ( )method " + message ) ;
break;
}e l s e{
l o g . p r i n t l n ( " S :Aboutt osendmessagebackt ot h ec l i e n t :" + message ) ;
s e n d T o C l i e n t ( message ) ;
l o g . p r i n t l n ( " S :J u s tr e t u r n e df r o ms e n d i n gmessagebackt ot h ec l i e n t : " + message ) ;
}

Introduction
THEECHOSERVERV
}
}catch( I O E x c e p t i o n e ) {
e . p r i n t S t a c k T r a c e ( ) ;
}f i n a l l y{
i f( o u t ! =n u l l) {
o u t . c l o s e ( ) ;
}
i f( i n ! =n u l l) {
t r y{
i n . c l o s e ( ) ;
}catch( I O E x c e p t i o n e ) {
e . p r i n t S t a c k T r a c e ( ) ;
}
}
}
l o g . p r i n t l n ( " S :E x i t i n gt h er u n ( )methodf o rC l i e n t S e r v i c e r " ) ;
}
}
}

Introduction
THEECHOCLIENTI
/
I t ' s a l w a y s a good i d e a t o use a package o t h e r t h a n t h e d e f a u l t package .
/
packageecho ;
/
The u n i t i m p l e m e n t s an " echo c l i e n t " w i t h no u s e r i n t e r f a c e . T h i s i s p u r e l y
n e t w o r k programming . T h i s i s t h e " model " o f t h e " modelviewc o n t r o l l e r (MVC) "
d e s i g n p a t t e r n .
/
importj a v a . i o . B u f f e r e d R e a d e r ;
importj a v a . i o . C l o s e a b l e ;
importj a v a . i o . I O E x c e p t i o n ;
importj a v a . i o . I n p u t S t r e a m R e a d e r ;
importj a v a . i o . P r i n t W r i t e r ;
importj a v a . n e t . C o n n e c t E x c e p t i o n ;
importj a v a . n e t . S o c k e t ;
/
T h i s c l a s s c o n t a i n s t h e programming t h e i m p l e m e n t s t h e model o f an
" Echo C l i e n t " . The echo c l i e n t sends messages t o t h e echo s e r v e r and
t h e echo s e r v e r j u s t sends them r i g h t back .

T h i s i s an e x e r c i s e i n s o c k e t s and c l i e n t / s e r v e r s o f t w a r e development .

@author k a y d e l l

/
p u b l i c c l a s sE c h o C l i e n timplementsC l o s e a b l e {

Introduction
THEECHOCLIENTII
/
T h i s f i e l d i s a r e f e r e n c e t o a s o c k e t o b j e c t f r o m t h e Java API c l a s s c a l l e d " S o c k e t "
/
p r i v a t eS o c k e t s o c k e t =n u l l;
/
T h i s f i e l d a l l o w s us t o r e a d d a t a f r o m t h e s e r v e r u s i n g t h e method c a l l d " r e a d L i n e ( ) "
i t a l s o g i v e s us t h e b e n e f i t o f b u f f e r i n g t h e i n p u t so t h a t t h e program can r u n f a s t e r .
The speed doesn ' t r e a l l y m a t t e r i n t h i s program , b u t b u f f e r i n g can h e l p f o r programs
t h a t r e c e i v e l a r g e amounts o f d a t a .
/
p r i v a t eB u f f e r e d R e a d e r i n =n u l l;
/
T h i s f i e l d i s an o b j e c t o f t h e t y p e " P r i n t W r i t e r " which a l l o w s t h e c l i e n t t o e a s i l y
send d a t a t o t h e s e r v e r
/
p r i v a t eP r i n t W r i t e r o u t =n u l l;
p r i v a t eLog l o g =n u l l;
/
T h i s c o n s t r u c t o r a c c e p t s an i p A d d r e s s and t h e p o r t number o f t h e s e r v e r
t o s p e c i f y which s e r v e r t o c o n n e c t t o .

@param i p A d d r e s s The i p a d d r e s s o f t h e s e r v e r o r n u l l , meaning l o c a l h o s t
@param portNum The p o r t number o f t h e s e r v e r t o t r y t o c o n n e c t t o .
@throws I O E x c e p t i o n Thrown f o r any k i n d o f IO e r r o r .

Introduction
THEECHOCLIENTIII

/
p u b l i cE c h o C l i e n t ( S t r i n g i p A d d r e s s ,i n tportNum , Log l o g )throwsI O E x c e p t i o n {
t h i s. l o g = l o g ;
l o g . p r i n t l n ( "C :E n t e r i n gc o n s t r u c t o r " ) ;
l o g . p r i n t l n ( "C :I PAddress :" + i p A d d r e s s + "portNum :" + portNum ) ;
t r y{
/ / c r e a t e a S o c k e t and c o n n e c t t o t h e s e r v e r which i s r u n n i n g on t h e same computer on p o r t number 9999
s o c k e t =newS o c k e t ( i p A d d r e s s , portNum ) ;
/ / use t h e i n p u t s t r e a m o f t h e S o c k e t and use i t t o c r e a t e a B u f f e r e d R e a d e r
i n =newB u f f e r e d R e a d e r (newI 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 ( ) ) ) ;
/ / use t h e O u t p u t S t r e a m o f t h e S o c k e t and use i t t o c r e a t e a P r i n t W r i t e r
o u t =newP 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 ( ) , E c h o U t i l s . AUTO_FLUSH ) ;
}catch( C o n n e c t E x c e p t i o n e ) {
System . e r r . p r i n t l n ( "C :C o n n e c t i o n E x c e p t i o n :c o u l dn o tc o n n e c tt ot h es e r v e r :" + e ) ;
throwe ;
}
l o g . p r i n t l n ( "C :E x i t i n gc o n s t r u c t o r " ) ;
}
/
T h i s c o n s t r u c t o r uses t h e d e f a u l t v a l u e s f o r i p a d d r e s s and p o r t number

Introduction
THEECHOCLIENTIV
t o t r y t o c o n n e c t w i t h a s e r v e r .

@throws I O E x c e p t i o n
/
p u b l i cE c h o C l i e n t ( Log l o g )throwsI O E x c e p t i o n {
t h i s( E c h o U t i l s . LOCAL_HOST, E c h o U t i l s . DEFAULT_PORT_NUM, l o g ) ;
}
/
T h i s method r e t u r n s w h e t h e r t h e c l i e n t i s s t i l l c o n n e c t e d t o t h e
s e r v e r o r n o t .

@return R e t u r n s w h e t h e r t h i s c l i e n t i s s t i l l c o n n e c t e d t o t h e s e r v e r o r n o t .
/
p u b l i c booleani s C o n n e c t e d ( ) {
l o g . p r i n t l n ( "C :E n t e r i n gi s C o n n e c t e d ( ) " ) ;
booleani s C o n n e c t e d = ( s o c k e t ! =n u l l&& s o c k e t . i s C o n n e c t e d ( ) ) ;
l o g . p r i n t l n ( "C :E x i t i n gi s C o n n e c t e d ( )i s C o n n e c t e d :" + i s C o n n e c t e d ) ;
r e t u r ni s C o n n e c t e d ;
}
/
T h i s method r e a d s a l i n e f r o m t h e echo s e r v e r
@return R e t u r n s t h e S t r i n g r e a d f r o m t h e echo s e r v e r
@throws I O E x c e p t i o n Thrown when t h e r e i s an e r r o r r e a d i n g f r o m t h e echo s e r v e r
/
p u b l i cS t r i n g re c ei v eM e ss a ge ( )throwsI O E x c e p t i o n {
l o g . p r i n t l n ( "C :E n t e r i n gr ec e iv e Me s sa g e ( ) " ) ;
S t r i n g message = i n . r e a d L i n e ( ) ;

Introduction
THEECHOCLIENTV
l o g . p r i n t l n ( "C :E x i t i n gr ec e iv e Me s sa g e ( )message :" + message ) ;
r e t u r nmessage ;
}
/
T h i s method w r i t e s a message t o t h e echo s e r v e r .

@param message
/
p u b l i c voidsendMessage ( S t r i n g message ) {
l o g . p r i n t l n ( "C :E n t e r i n gsendMessage ( ) ,message :" + message ) ;
o u t . p r i n t l n ( message ) ;
l o g . p r i n t l n ( "C :E x i t i n gr ec e iv e Me s sa g e ( ) " ) ;
}
/
T h i s method i s c a l l e d t o c l o s e a l l IO o b j e c t s t h a t t h i s echo c l i e n t o b j e c t
has open . TODO: c l o s e " i n " and " o u t " .
/
@Override
p u b l i c voidc l o s e ( )throwsI O E x c e p t i o n {
l o g . p r i n t l n ( "C :E n t e r i n gc l o s e ( ) " ) ;
i f( s o c k e t ! =n u l l&& ! s o c k e t . i s C l o s e d ( ) ) {
s o c k e t . c l o s e ( ) ;
}
l o g . p r i n t l n ( "C :E x i t i n gc l o s e ( ) " ) ;
}
}

Introduction
THEECHOCLIENTVI

Introduction
REFERENCES
The source code has been taken from here.