// File: IO7_client_plain.java (Module 13) // // Author: Rahul Simha // Created: Nov 28, 1998 // Modified: Nov 13, 2000. // // Producer as client, using sockets. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; class Producer implements Runnable { OutputStream outStream; public Producer (OutputStream outStream) { // Store the reference to the output stream. this.outStream = outStream; } // Must implement the run() method. public void run () { // Write 25 random bytes to the buffer. for (int i=1; i<=25; i++) { // Create a random byte byte k = (byte) UniformRandom.uniform (1, 100); // Write it to the screen. System.out.println ("Producer: writing " + k); // Write integer to buffer. try { outStream.write (k); } catch (IOException e) { System.out.println (e); } // Sleep for a while. try { Thread.sleep ((int)UniformRandom.uniform(100,1000)); } catch (InterruptedException e) { System.out.println (e); } } // Write EOF and close output stream. try { outStream.write (-1); outStream.close (); } catch (IOException e) { System.out.println (e); } System.out.println (" Done!"); } } public class IO7_client_plain { public static void main (String[] argv) { try { // Open a socket to the server. // Address: hobbes.seas.gwu.edu, port 5010. // 161.253.77.33 is the IP addr of the Unix UDP server //Socket soc = new Socket ("161.253.77.33", 40013); Socket soc = new Socket ("unix.seas.gwu.edu", 40013); InetAddress remoteMachine = soc.getInetAddress(); System.out.println ("Producer as client: attempting connection" + " to " + remoteMachine); // Note: server must be fired up first! // Now create the output stream and hand off to producer. OutputStream outStream = soc.getOutputStream (); // Create a producer instance and thread. Producer p = new Producer (outStream); Thread pthread = new Thread (p); // Start the threads. pthread.start(); } catch (IOException e) { System.out.println (e); } } }