1 package Networking;
  2 
  4 import java.io.*;
  5 import java.net.*;
  6 
  7 /**
  8  * A simple client class which creates a separate Listener thread and communicates
  9  * with a server by both sending and receiving complete lines of data.
 10  * @author aconover
 11  */
 12 public class Client {
 13 
 14     /**
 15      * @param args the command line arguments.  Open ports and create a simple
 16      * listener that will handle data coming from the Server.
 17      */
 18     public static void main(String[] args) throws UnknownHostException, IOException {
 19         // Create a new socket connection to the server
 20         Socket clientSocket = new Socket(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 8888);
 21 
 22         // Create a instance of a listener that will listen for data coming from the server.
 23         Listener listener = new Listener(clientSocket);
 24 
 25         // Start the listener thread (which runs concurrently with main
 26         Thread threadListener = new Thread(listener);
 27         threadListener.start();
 28 
 29         // Setup an ouput communication "channel" to the sever over the socket.
 30         PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
 31 
 32         // Send some date
 33         writer.println("this is a test");
 34         writer.println("this is another test");
 35         writer.println("QUIT");
 36 
 37         // Close the writer
 38         writer.close();
 39 
 40         // Wait for the listener to be done before we quit.  Note: This is just
 41         // an artifact of the fact that we are doing everthing inside of a
 42         // command-line application that has no user initiated shutdown.
 43         while (clientSocket.isConnected()) {
 44             Thread.yield();
 45         }
 46 
 47         // Be a good citizen and close the port.
 48         clientSocket.close();
 49 
 50         // We're done.
 51         System.out.println("Done");
 52     }
 53 }
 54 
 55 /**
 56  * The Listener class runs as a separate thread and listens for data coming
 57  * from the server.  It is not "public" simply because it's in the same file
 58  * as the Client class.  (A file can have only one public class).
 59  */
 60 class Listener implements Runnable {
 61 
 62     // A reader to hold incoming data.
 63     BufferedReader br;
 64 
 65     /**
 66      * Constructor for the listener
 67      * @param clientSocket The Socket to Listen on.
 68      * @throws java.io.IOException Thrown if an error occurs while communicating.
 69      */
 70     Listener(Socket clientSocket) throws IOException {
 71         br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 72     }
 73 
 74     /**
 75      * This is the "thread loop" which waits for data from the server and displays
 76      * it on a line-by-line basis.
 77      */
 78     public void run() {
 79         try {
 80             String dataLine;  // The line of input
 81 
 82             // while readLine does not return null, assign the line to dataLine.
 83             while ((dataLine = br.readLine()) != null) {
 84                 System.out.println("CLIENT (From Server)> " + dataLine);
 85                 System.out.flush(); // force writing.
 86             }
 87         } catch (java.net.SocketException se) {
 88             System.err.println("The Server disconnected.");
 89         } catch (java.io.IOException ioe) {
 90             System.err.println(ioe.getMessage());
 91         } finally {
 92             try {
 93                 // REGARDLESS OF HAPPENS, ALWAYS CLOSE YOUR READERS/WRITERS/PORTS!
 94                 br.close();
 95             } catch (IOException ex) {
 96                 // Oh well... Nothing more we can do about it.
 97                 System.err.println("Failed while failing!!! " + ex.getMessage());
 98             }
 99         }
100     }
101 }