The Java Program: Client.java

  1 // Client.java -- Client of a line-for-line server
  2 
  3 import java.net.Socket;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.io.BufferedReader;
  7 import java.io.PrintWriter;
  8 
  9 public class Client {
 10 
 11    public static final int DEFAULT_PORT = 56789;
 12 
 13    public static void usage() {
 14       System.out.println ("Usage: java Client <hostname> [<port>]");
 15       System.exit (0);
 16    }
 17 
 18    public static void main (String[] args) {
 19       int port = DEFAULT_PORT;
 20       Socket s = null;
 21         
 22       // Parse the port specification
 23       if ((args.length != 1) && (args.length != 2)) usage();
 24       if (args.length == 1) {
 25          port = DEFAULT_PORT;
 26       } else {
 27          try {
 28             port = Integer.parseInt(args[1]);
 29          } catch (NumberFormatException e) {
 30             usage();
 31          }
 32       }
 33       
 34       try {
 35          // Create a socket to communicate to the specified host and port
 36          s = new Socket (args[0], port);
 37          // Create streams for reading and writing lines of text
 38          // from and to this socket.
 39          final BufferedReader sin = new BufferedReader (
 40             new InputStreamReader (s.getInputStream()));
 41          final PrintWriter sout = new PrintWriter (s.getOutputStream());
 42          // Create a stream for reading lines of text from the console
 43          final BufferedReader in = new BufferedReader (
 44             new InputStreamReader (System.in));
 45 
 46          // Tell the user that we've connected
 47          System.out.println ("Client:  connected to " +
 48             s.getInetAddress() + " at port "+ s.getPort());
 49 
 50          while (true) {
 51             System.out.print ("> ");  // print a prompt
 52             System.out.flush();
 53             final String line = in.readLine();     // read a line from the console
 54             if (line == null) break;  // check for EOF
 55             System.out.println ("Client: sending line to server: "+ line);
 56             sout.println (line);
 57             sout.flush();                          // make sure output is sent
 58             final String response = sin.readLine();// read a line from the server
 59 
 60             // Check if connection is closed (i.e. for EOF)
 61             if (response == null) { 
 62                System.out.println ("Client: connection closed by server.");
 63                break;
 64             }
 65             // And write the server's response to the console.
 66             System.out.println ("Client: received from server:   " + response);
 67          }
 68       } catch (IOException e) {
 69          e.printStackTrace (System.err);
 70       } finally {
 71          try {
 72             if (s != null) s.close();
 73          } catch (IOException e) {
 74             e.printStackTrace (System.err);
 75          };
 76       }
 77    }
 78 }