// File: IO8_client_plain.java (Module 13) // // Author: Rahul Simha // Created: Nov 28, 1998 // Modified: Nov 13, 2000. // // Consumer as client, using sockets. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; class Consumer implements Runnable { InputStream inStream; public Consumer (InputStream inStream) { this.inStream = inStream; } public void run () { // Read byte values until EOF. while (true) { // Get the next byte int i = -1; try { i = inStream.read (); } catch (IOException e) { System.out.println (e); } // Extract byte. byte k = (byte) i; // Check if end-of-data. if ( (i < 0) || (k < 0) ) break; System.out.println ("Consumer: just read " + k); // Sleep for a while. try { Thread.sleep ((int)UniformRandom.uniform(5,10)); } catch (InterruptedException e) { System.out.println (e); } } System.out.println (" Done!"); try { inStream.close (); } catch (IOException e) { System.out.println (e); } } } public class IO8_client_plain { public static void main (String[] argv) { try { // Open a socket to the server. Socket soc = new Socket ("unix.cs.gwu.edu", 40013); InetAddress remoteMachine = soc.getInetAddress(); System.out.println ("Consumer as client: attempting connection" + " to " + remoteMachine); // Note: server must be fired up first! // Now create the input stream for the Consumer. InputStream inStream = soc.getInputStream (); // Create a consumer instance and thread. Consumer c = new Consumer (inStream); Thread cthread = new Thread (c); // Start the thread. cthread.start(); } catch (IOException e) { System.out.println (e); } } }