import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.ServerSocket;
public class Person_Skeleton extends Thread{
PersonServer myServer;
public Person_Skeleton(PersonServer server){
// get a reference to the object server that this skeleton wraps
this.myServer = server;
}
public void run(){
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();
// Evalute 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); }
}
public static void main(String args [] ){
// obtain a unique instance Person
PersonServer person = new PersonServer("Richard", 34);
Person_Skeleton skel = new Person_Skeleton(person);
skel.start();
}
}
|