// // example of use of java.net.* classes and serialization: // set up client/server interaction in which client sends request // object to server for processing and server returns result. // this file contains the program for the client; Request.java contains // definitions for request objects, and RequestServer.java contains // the program for the server. // example adapted from one in chapter 11 of _Exploring Java_ by // Niemeyer & Peck. // import java.net.* ; import java.io.* ; // // a RequestClient object is used to send requests to the server // and receive replies. // class RequestClient { String theHost ; int thePort ; Socket server ; ObjectInputStream fromServer ; ObjectOutputStream toServer ; RequestClient(String h, int p) throws IOException { theHost = h ; thePort = p ; // make connection to server. server = new Socket(theHost, thePort) ; // get connection's output, input streams and // connect to Object* streams. // note that server must obtain input stream, // then output stream, while client must // do the reverse -- if both try to obtain // input stream first, deadlock apparently // results. toServer = new ObjectOutputStream( server.getOutputStream()) ; fromServer = new ObjectInputStream( server.getInputStream()) ; // be sure "finalize" method will be called // to close connection. System.runFinalizersOnExit(true) ; } // sends request to server, waits for result, and returns it. // observe that readObject() can throw a ClassNotFoundException. Object makeRequest(Request r) throws IOException, ClassNotFoundException { // send (serialized) request to server. toServer.writeObject(r) ; // flush to ensure request is sent right away and not // buffered. toServer.flush() ; // receive (serialized) result. return fromServer.readObject() ; } public void finalize() { try { server.close() ; } catch (IOException e) { } } // // ---- main --------------------------------------------------- // // command-line arguments are hostname, port for server. // hostname is the name (or IP address) of the machine // on which RequestServer is executing. port is the port // on which it was started. // // program sends an assortment of requests to server and // prints results it gets back. // public static void main(String[] args) { if (args.length < 2) { System.out.println("Arguments are host, port") ; System.exit(-1) ; } try { RequestClient client = new RequestClient(args[0], Integer.parseInt(args[1])) ; // define an assortment of requests. Request[] reqs = new Request[] { new ShowLocalTime(), new FindRealRoots(1.0, 0.0, 1.0), new FindRealRoots(1.0, -2.0, 1.0), new FindRealRoots(1.0, 0.0, -1.0), new ShowLocalTime() } ; // process the requests. for (int i = 0 ; i < reqs.length ; i++) { System.out.println("request: " + reqs[i]) ; // send the request to the server and get // the result. Object o = client.makeRequest(reqs[i]) ; // print result of ShowLocalTime (string) if (reqs[i] instanceof ShowLocalTime) System.out.println("result: " + o) ; // print result of FindRealRoots (array of // double) else if (reqs[i] instanceof FindRealRoots) { double[] roots = (double[]) o ; if (roots.length == 0) System.out.println("result: " + "no real roots") ; else if (roots.length == 1) System.out.println("result: " + "repeated root " + roots[0]) ; else System.out.println("result: " + "roots " + roots[0] + ", " + roots[1]) ; } } } catch (IOException e) { System.out.println("I/O error: " + e) ; } catch (ClassNotFoundException e) { System.out.println("Internal error: " + e) ; } } }