7. Introduction The Echo Server The Echo Client References
THE ECHO SERVER I
/∗
∗ Copyright ( c ) 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved .
∗
∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without
∗ modification , are permitted provided that the f o l l o w i n g conditions
∗ are met :
∗
∗ − Redistributions of source code must r e t a i n the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer .
∗
∗ − Redistributions in binary form must reproduce the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the
∗ documentation and / or other materials provided with the d i s t r i b u t i o n .
∗
∗ − Neither the name of Oracle or the names of i t s
∗ c o n t r i b u t o r s may be used to endorse or promote products derived
∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission .
∗
∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL ,
∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING
∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8. Introduction The Echo Server The Echo Client References
THE ECHO SERVER II
∗/
import java . net . ∗ ;
import java . io . ∗ ;
public class EchoServer {
public static void main ( String [ ] args ) throws IOException {
i f ( args . length != 1) {
System . err . p r i n t l n ( "Usage : java EchoServer <port number>" ) ;
System . e x i t ( 1 ) ;
}
int portNumber = Integer . parseInt ( args [ 0 ] ) ;
try (
ServerSocket serverSocket =
new ServerSocket ( Integer . parseInt ( args [ 0 ] ) ) ;
Socket clientSocket = serverSocket . accept ( ) ;
P r i n t W r i t e r out =
new P r i n t W r i t e r ( clientSocket . getOutputStream ( ) , true ) ;
BufferedReader in = new BufferedReader (
new InputStreamReader ( clientSocket . getInputStream ( ) ) ) ;
) {
String inputLine ;
while ( ( inputLine = in . readLine ( ) ) != null ) {
out . p r i n t l n ( inputLine ) ;
}
} catch ( IOException e ) {
9. Introduction The Echo Server The Echo Client References
THE ECHO SERVER III
System . out . p r i n t l n ( " Exception caught when t r y i n g to l i s t e n on port "
+ portNumber + " or l i s t e n i n g f o r a connection " ) ;
System . out . p r i n t l n ( e . getMessage ( ) ) ;
}
}
}
10. Introduction The Echo Server The Echo Client References
THE ECHO CLIENT I
/∗
∗ Copyright ( c ) 1995, 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved .
∗
∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without
∗ modification , are permitted provided that the f o l l o w i n g conditions
∗ are met :
∗
∗ − Redistributions of source code must r e t a i n the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer .
∗
∗ − Redistributions in binary form must reproduce the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the
∗ documentation and / or other materials provided with the d i s t r i b u t i o n .
∗
∗ − Neither the name of Oracle or the names of i t s
∗ c o n t r i b u t o r s may be used to endorse or promote products derived
∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission .
∗
∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL ,
∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING
∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11. Introduction The Echo Server The Echo Client References
THE ECHO CLIENT II
∗/
import java . io . ∗ ;
import java . net . ∗ ;
public class EchoClient {
public static void main ( String [ ] args ) throws IOException {
i f ( args . length != 2) {
System . err . p r i n t l n (
"Usage : java EchoClient <host name> <port number>" ) ;
System . e x i t ( 1 ) ;
}
String hostName = args [ 0 ] ;
int portNumber = Integer . parseInt ( args [ 1 ] ) ;
try (
Socket echoSocket = new Socket ( hostName , portNumber ) ;
P r i n t W r i t e r out =
new P r i n t W r i t e r ( echoSocket . getOutputStream ( ) , true ) ;
BufferedReader in =
new BufferedReader (
new InputStreamReader ( echoSocket . getInputStream ( ) ) ) ;
BufferedReader stdIn =
new BufferedReader (
new InputStreamReader ( System . in ) )
) {
String userInput ;
12. Introduction The Echo Server The Echo Client References
THE ECHO CLIENT III
while ( ( userInput = stdIn . readLine ( ) ) != null ) {
out . p r i n t l n ( userInput ) ;
System . out . p r i n t l n ( " echo : " + in . readLine ( ) ) ;
}
} catch ( UnknownHostException e ) {
System . err . p r i n t l n ( "Don ’ t know about host " + hostName ) ;
System . e x i t ( 1 ) ;
} catch ( IOException e ) {
System . err . p r i n t l n ( " Couldn ’ t get I /O f o r the connection to " +
hostName ) ;
System . e x i t ( 1 ) ;
}
}
}
13. Introduction The Echo Server The Echo Client References
REFERENCES
The source code has been taken from here.