这个例子模拟了一个远程请求对象属性的过程。有一个远程对象Dog在网络上,现在要得到它的名字(strName)属性。程序在客户端设一个存根(Dog_Stub)类,在服务器端启动一个骨架(dog_Skeleton)类,这两个类都实现了Dog接口,Dog_Stub与 Dog_Skeleton通过Socket进行远程通信。当客户程序DogClient向Dog_Stub发出获取名字属性的请求时,Dog_Stub对象把方法名“getName()”作为一个字符串通过Socket发给远程的Dog_Skeleton对象,Dog_Skeleton对象收到这个字符串后再根据字符串的内容执行DogServer对象的getName()方法,得到Dog的名字,然后又通过Socket返回给DogStub对象。整个流程通过网络实现,但对于客户程序DogClient来讲,它并不知道真正的Dog对象在哪里,甚至也不知道这个过程通过了网络,它只知道发出的获取名字属性的请求得到了满意的结果而已。
Dog.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
public interface Dog {
String getName() throws Exception;
}
DogSkeleton.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class DogSkeleton extends Thread {
public static final int PORT = 8123;
public static final String GET_NAME = "getName()";
static ServerSocket ss;
private DogServer dogServer;
DogSkeleton (final DogServer dogServer) {
try {
this.dogServer = dogServer;
if (ss == null) {
ss = new ServerSocket(PORT);
System.out.println("Server started in port" + PORT);
this.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void run() {
try {
while (ss != null) {
System.out.println("Waiting for request...");
Socket socket = ss.accept();
// oos must be locate before ois. remember!!!
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String methodName = (String)ois.readObject();
System.out.println("Request method name: " + methodName);
if (methodName.equals(GET_NAME)) {
oos.writeObject(dogServer.getName());
oos.flush();
System.out.println("Wrote dog name: " + dogServer.getName() + " to client.");
ois.close();
oos.close();
}
}
sleep(1000);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
DogStub.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class DogStub implements Dog {
public static final String SERVER_URL = "localhost";
public static final int PORT = 8123;
public static final String EMPTY = "";
Socket socket;
ObjectInputStream ois;
ObjectOutputStream oos;
DogStub () {
try {
socket = new Socket(SERVER_URL, PORT);
// oos must be locate before ois. remember!!!
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getName() throws Exception {
final String GET_NAME = "getName()";
String dogName = EMPTY;
oos.writeObject(GET_NAME);
oos.flush();
dogName = (String) ois.readObject();
return dogName;
}
}
DogServer.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
public class DogServer implements Dog {
private String name;
DogServer(final String name) {
this.name = name;
}
public String getName() throws Exception {
return this.name;
}
/**
* @param args
*/
public static void main(String[] args) {
new DogSkeleton(new DogServer("Tomcat"));
}
}
DogClient.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
public class DogClient {
/**
* @param args
*/
public static void main(String[] args) {
DogStub dogStub = new DogStub();
try {
String dogName = dogStub.getName();
System.out.println("The dog name is " + dogName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Dog.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
public interface Dog {
String getName() throws Exception;
}
DogSkeleton.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class DogSkeleton extends Thread {
public static final int PORT = 8123;
public static final String GET_NAME = "getName()";
static ServerSocket ss;
private DogServer dogServer;
DogSkeleton (final DogServer dogServer) {
try {
this.dogServer = dogServer;
if (ss == null) {
ss = new ServerSocket(PORT);
System.out.println("Server started in port" + PORT);
this.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void run() {
try {
while (ss != null) {
System.out.println("Waiting for request...");
Socket socket = ss.accept();
// oos must be locate before ois. remember!!!
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String methodName = (String)ois.readObject();
System.out.println("Request method name: " + methodName);
if (methodName.equals(GET_NAME)) {
oos.writeObject(dogServer.getName());
oos.flush();
System.out.println("Wrote dog name: " + dogServer.getName() + " to client.");
ois.close();
oos.close();
}
}
sleep(1000);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
DogStub.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class DogStub implements Dog {
public static final String SERVER_URL = "localhost";
public static final int PORT = 8123;
public static final String EMPTY = "";
Socket socket;
ObjectInputStream ois;
ObjectOutputStream oos;
DogStub () {
try {
socket = new Socket(SERVER_URL, PORT);
// oos must be locate before ois. remember!!!
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getName() throws Exception {
final String GET_NAME = "getName()";
String dogName = EMPTY;
oos.writeObject(GET_NAME);
oos.flush();
dogName = (String) ois.readObject();
return dogName;
}
}
DogServer.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
public class DogServer implements Dog {
private String name;
DogServer(final String name) {
this.name = name;
}
public String getName() throws Exception {
return this.name;
}
/**
* @param args
*/
public static void main(String[] args) {
new DogSkeleton(new DogServer("Tomcat"));
}
}
DogClient.java
----------------------------------------------------------------------------------------------------
package rmi.dog;
public class DogClient {
/**
* @param args
*/
public static void main(String[] args) {
DogStub dogStub = new DogStub();
try {
String dogName = dogStub.getName();
System.out.println("The dog name is " + dogName);
} catch (Exception e) {
e.printStackTrace();
}
}
}