try {
// Create a server socket on port 9000.
ServerSocket serverSocket = new ServerSocket(9000);
// Wait for and obtain a socket connection from stub.
Socket socket = serverSocket.accept();
while (socket != null){
// Create an input stream to receive requests from stub.
ObjectInputStream inStream =
new ObjectInputStream(socket.getInputStream());
// Read next method request from stub. Block until request is
// sent.
String method = (String)inStream.readObject();
// Evaluate the type of method requested.
if (method.equals("age")){
// Invoke business method on server object.
int age = myServer.getAge();
// Create an output stream to send return values back to
// stub.
ObjectOutputStream outStream =
new ObjectOutputStream(socket.getOutputStream());
// Send results back to stub.
outStream.writeInt(age);
outStream.flush();
} else if(method.equals("name")){
// Invoke business method on server object.
String name = myServer.getName();
// Create an output stream to send return values back to
// the stub.
ObjectOutputStream outStream =
new ObjectOutputStream(socket.getOutputStream());
// Send results back to stub.
outStream.writeObject(name);
outStream.flush();
}
}
} catch(Throwable t) {t.printStackTrace();System.exit(0); }