// // 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 server; Request.java contains // definitions for request objects, and RequestClient.java contains // the program for the client. // example adapted from one in chapter 11 of _Exploring Java_ by // Niemeyer & Peck. // import java.net.* ; import java.io.* ; // // ---- main ----------------------------------------------------------- // // command-line argument is port number. // // instructions for testing: // // (1) start this program: // java RequestServer nnnn // (nnnn is any port number you choose, higher than 1024) // suppose mymachine.org is the name of the machine on which // you've done this, and nnnn = 1234. // // (2) run RequestClient one or more times: // java RequestClient mymachine.org 1234 // // you can do this on the same machine (in a different window // usually) or on a different machine. // // if you don't know the name of the machine on which you're // running, you can use its IP address instead. for // a Windows machine, you can (at least on the CIRCA // machines) get its IP address by typing "winipcfg" // at a DOS prompt. // // when done, stop the server with control-C. // // these instructions have been tested on Unix and Windows systems at // UF, and on Linux systems at Trinity. // class RequestServer { public static void main(String[] args) throws IOException { if (args.length < 1) { System.out.println("Argument is port") ; System.exit(-1) ; } // create server socket to accept client requests. ServerSocket ss = new ServerSocket(Integer.parseInt(args[0])) ; // accept client requests, creating a new thread for // each. // (ss.accept() returns a connection (socket) to the // client.) while (true) { new RequestServerConnection(ss.accept()) .start() ; } } } // // a RequestServerConnection object is a thread that serves a // client request (consisting of a sequence of Request objects). // it sends back to the client a result object for each Request // object it receives. // class RequestServerConnection extends Thread { Socket client ; RequestServerConnection(Socket client) throws SocketException { this.client = client ; setPriority(NORM_PRIORITY - 1) ; } public void run() { System.out.println("Processing requests from " + client.getInetAddress() ) ; try { // get connection's input, output 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. ObjectInputStream fromClient = new ObjectInputStream( client.getInputStream()) ; ObjectOutputStream toClient = new ObjectOutputStream( client.getOutputStream()) ; try { // process requests until client closes // connection (EOF). while (true) { // loop ended by EOFException // read (serialized) request object. Request r = (Request) fromClient.readObject() ; // service request and write out // (serialized) result. toClient.writeObject( r.serviceRequest()) ; // flush to ensure request is sent right // away and not buffered. toClient.flush() ; } } catch (EOFException e) { } client.close() ; } catch (IOException e) { System.out.println("I/O error: " + e) ; } catch (ClassNotFoundException e) { // this exception is potentially thrown by // readObject(). System.out.println("Internal error: " + e) ; } System.out.println("Finished with requests from " + client.getInetAddress() ) ; } }