import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteServer extends Remote {
String helloWorld(String name) throws RemoteException;
Person getPerson(String name, int age) throws RemoteException;
}
------------------
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class RemoteServerImpl extends UnicastRemoteObject implements RemoteServer {
protected RemoteServerImpl() throws RemoteException {
// super();
// TODO Auto-generated constructor stub
}
@Override
public String helloWorld(String name) throws RemoteException {
// TODO Auto-generated method stub
return "hello "+name;
}
@Override
public Person getPerson(String name, int age) throws RemoteException {
// TODO Auto-generated method stub
return new Person(name,age);
}
public static void main(String args[]) throws Exception{
RemoteServer remoteServer = new RemoteServerImpl();
LocateRegistry.createRegistry(1099);
Naming.rebind("rmi://127.0.0.1:1099/mytest", remoteServer);
}
}
----------------
import java.io.Serializable;
public class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4033550198229762186L;
private String name;
private int age;
@Override
public String toString(){
return name+age;
}
public Person(){
}
public Person(String pname, int page){
this.name = pname;
this.age=page;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
--------------
import java.rmi.Naming;
public class Client {
public static void main(String[] args) {
try {
//
RemoteServer remoteService = (RemoteServer) Naming.lookup("rmi://10.0.0.9:1099/mytest");
System.out.println(remoteService.helloWorld("king"));
System.out.println(remoteService.getPerson("bing", 32));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Rmi
猜你喜欢
转载自wangbing9577.iteye.com/blog/2159867
今日推荐
周排行