home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / ans / chap22 / exer2202 / NumberClient.java next >
Encoding:
Java Source  |  1997-04-20  |  2.7 KB  |  99 lines

  1. import java.io.*;
  2. import java.net.*;
  3. import java.awt.*;
  4.  
  5. /**
  6.  * NumberClient -- The client program for the guessing game.
  7.  *
  8.  * To run as a client, name the server as the first command line 
  9.  * argument:
  10.  *   java NumberClient bluehorse.com
  11.  * or
  12.  *   java NumberClient local          // to run locally
  13.  */
  14.  
  15. public class NumberClient {
  16.     private int              port = 5001;    // The default port.
  17.     private Socket           sock;
  18.     private DataInputStream  remoteIn;
  19.     private DataOutputStream remoteOut;
  20.     private boolean          listening = true;
  21.  
  22.     public static void main(String args[]) {
  23.  
  24.        if (args.length != 1) {
  25.           System.out.println("format is: java NumberClient <hostname>");
  26.           return;
  27.        }
  28.  
  29.        new NumberClient().client(args[0]);
  30.     }
  31.    
  32.     // As a client, we create a socket bound to the specified port,
  33.     // connect to the specified host, and then start communicating 
  34.     // over the network via the socket.
  35.  
  36.     private void client(String serverName) {
  37.         DataInputStream in = new DataInputStream(System.in);
  38.  
  39.         try {
  40.             if (serverName.equals("local"))
  41.                serverName = null;
  42.  
  43.             InetAddress serverAddr = InetAddress.getByName(serverName);
  44.             sock = new Socket(serverAddr.getHostName(), port, true);
  45.             remoteIn = new DataInputStream(sock.getInputStream());
  46.             remoteOut = new DataOutputStream(sock.getOutputStream());
  47.  
  48.             while (listening) {
  49.                String s = in.readLine();
  50.                if (s.equals(""))
  51.                   listening = false;
  52.                else 
  53.                   remoteOut.writeUTF(s);
  54.  
  55.                String response = remoteIn.readUTF();
  56.                System.out.println(response);
  57.             }
  58.  
  59.         } catch (IOException e) {
  60.             System.out.println(e.getMessage() +
  61.                ": Connection with server closed.");
  62.  
  63.         } finally {
  64.            try {
  65.               if (remoteIn != null) {
  66.                  remoteIn.close();
  67.                  remoteIn = null;
  68.               }
  69.  
  70.               if (remoteOut != null) {
  71.                  remoteOut.close();
  72.                  remoteOut = null;
  73.               }
  74.  
  75.               if (sock != null) {
  76.                  sock.close();
  77.                  sock = null;
  78.               }
  79.  
  80.            } catch (IOException x) {
  81.            }
  82.         }
  83.     }
  84.  
  85.     protected void finalize() throws Throwable {
  86.         try {
  87.            if (remoteIn != null)
  88.               remoteIn.close();
  89.            if (remoteOut != null)
  90.               remoteOut.close();
  91.            if (sock != null)
  92.               sock.close();
  93.         } catch (IOException x) {
  94.         }
  95.         super.finalize();
  96.     }
  97.  
  98. }
  99.