public class Person_Stub implements Person { Socket socket;
public Person_Stub() throws Throwable { // connect to skeleton socket = new Socket("computer_name", 9000); }
public int getAge() throws Throwable { // pass method name to skeleton ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream()); outStream.writeObject("age"); outStream.flush();
ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream()); return inStream.readInt(); }
public String getName() throws Throwable { // pass method name to skeleton ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream()); outStream.writeObject("name"); outStream.flush();
ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream()); return (String)inStream.readObject(); } }
public class Person_Skeleton extends Thread { PersonServer myServer;
public Person_Skeleton(PersonServer server) { // get reference of object server this.myServer = server; }
public void run() { try { // new socket at port 9000 ServerSocket serverSocket = new ServerSocket(9000); // accept stub's request Socket socket = serverSocket.accept();
while (socket != null) { // get stub's request ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream()); String method = (String)inStream.readObject();
// check method name if (method.equals("age")) { // execute object server's business method int age = myServer.getAge(); ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
// return result to stub outStream.writeInt(age); outStream.flush(); }
if(method.equals("name")) { // execute object server's business method String name = myServer.getName(); ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
// return result to stub outStream.writeObject(name); outStream.flush(); } } } catch(Throwable t) { t.printStackTrace(); System.exit(0); } }
public static void main(String args []) { // new object server PersonServer person = new PersonServer("Richard", 34);
Person_Skeleton skel = new Person_Skeleton(person); skel.start(); } }
Skeleton类extends from Thread,它长驻在后台运行,随时接收client发过来的request。并根据发送过来的key去调用相应的business method。