// 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 outStream; public Producer (OutputStream outStream) { // Store the reference to the output stream. this.outStream = outStream; } // Must implement the run() method. public void run () { try { // Get an instance of the object output stream. ObjectOutputStream oos = new ObjectOutputStream (outStream); // 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 ("hobbes.seas.gwu.edu", 5010); 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); } } }