// File: IO27_client.java (Module 13) // // Author: Rahul Simha // Created: Dec 7, 1998 // // Producer as client, using sockets, with Objectoutput stream. import java.io.*; import java.net.*; class Producer implements Runnable { OutputStream out_stream; public Producer (OutputStream out_stream) { // Store the reference to the output stream. this.out_stream = out_stream; } // Must implement the run() method. public void run () { try { // Get an instance of the object output stream. ObjectOutputStream oos = new ObjectOutputStream (out_stream); // Create some Person instances and write them out. Person p = new Person ("Murderous Matt", 28); System.out.println (p); oos.writeObject (p); p = new Person ("Necromancing Nancy", 33); System.out.println (p); oos.writeObject (p); oos.close (); } catch (IOException e) { System.out.println (e); } } } public class IO27_client { public static void main (String[] argv) { try { // Open a socket to the server. // Address: felix.seas.gwu.edu, port 5010. Socket soc = new Socket ("felix.seas.gwu.edu", 5010); InetAddress remote_machine = soc.getInetAddress(); System.out.println ("Producer as client: attempting connection" + " to " + remote_machine); // Note: server must be fired up first! // Now create the output stream and hand off to producer. OutputStream out_stream = soc.getOutputStream (); // Create a producer instance and thread. Producer p = new Producer (out_stream); Thread pthread = new Thread (p); // Start the threads. pthread.start(); } catch (IOException e) { System.out.println (e); } } }