Echo Client-Server in Java

These are a few simple examples meant to refresh your knowledge of client and server communication over Java sockets.

  1. Dowload echoJava.tar (or echoJava.zip, if working in Windows) and extract the files into a directory. You should see the following files:

    EchoServerSeq.java  Java code for a sequential echo server
    EchoServer.java  Java code for a multi-threaded echo server
    EchoClient.javaJava code for an echo client
    Connection.javaThread code for the echo server
    readmeThis file

  2. Compile the three programs either on tanner or on your local machine using the javac compiler:
         javac EchoServerSeq.java
         javac EchoServer.java
         javac EchoClient.java
    

  3. Test the client and sequential server on tanner (or on your local machine). Pick a random 5-digit integer N no greater than 64000 and pass it to the server as an argument:
         java EchoServerSeq N

    Using telnet: Open a second terminal window on the same machine and invoke telnet at the shell prompt, passing the machine on which the server runs and the port number N as arguments:

         telnet localhost  N
    The telnet application opens a connection to port N on the computer it runs on. Now anything that you type into the telnet window will be sent over this connection and echoed back to you by the server. To close the connection to the server, type in the dot character ('.', without quotes).

    Using the echo client: Open a second terminal window on the same machine and invoke EchoClient at the shell prompt, passing the machine on which the server runs and the port number N as arguments:

         java EchoClient tanner N
    Now anything you type into the client window will be sent over the connection and echoed back to you by the server. To close the connection to the server, type in the dot character ('.', without quotes).

  4. Try to understand what each step of the client and server code does. Report questions and answers you may generate while experimenting with this code.

  5. The multithreaded version EchoServer.java spawns a thread for each connection from a client, then gets back immediately to listening for new incoming requests from clients. To test your multithreaded server, open two client windows and have the clients talk simultaneously to the server.