接口
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{
String sayHello() throws RemoteException;
}
它定义了一个方法,sayHello,实现向调用者返回一个字符串Server类import java.rmi.registry.Resistry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello
{
public Server(){}
public String sayHello()
{
return “Hello,World!”;
}
public static void main(String args[])
{
Try{
Server obj=new Server ();
Hello stub=(Hello)UnicastRemoteObject.explortObject(obj,0);
//Bind the remote object’ s stub in the registry
Registry registry=LocateRegistry.getRegistry();
Registry.bind(“Hello”,stub);
System.err.println(“Server ready”);
}catch(Exception e)
{
System.err.println(“Server exception:”+e.toString());
e.printStackTrace();
}
}
}
接口
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ReceiveMessageInterface extends Remote
{
String receiveMessage(String x) throws RemoteException;
}
server端
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class RmiServer extends java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.
// This method is called from the remote client by the RMI.
// This is the implementation of the 锟斤拷ReceiveMessageInterface锟斤拷.
public String receiveMessage(String x) throws RemoteException
{
System.out.println(x);
return "Got " +x;
}
public RmiServer() throws RemoteException, MalformedURLException
{
try
{
// get the address of this host.
thisAddress = (InetAddress.getLocalHost()).toString();
}catch (Exception e)
{
throw new RemoteException("can't get inet address.");
}
thisPort = 3232; // this port(registry锟斤拷s port)
System.out.println("server address=" + thisAddress + ",port=" + thisPort);
try
{
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry(thisPort);
registry.rebind("rmiServer", this);
//Naming.rebind("rmiServer", this);
}catch (RemoteException e)
{
throw e;
}
//System.out.println(this.getRef());
}
static public void main(String args[])
{
try
{
RmiServer s = new RmiServer();
}catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
client端
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class RmiClient
{
static public void main(String args[])
{
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress = "127.0.0.1";//args[0];
String serverPort = "3232";//args[1];
String text = "Hi rmi!";//args[2];
System.out.println("sending " + text + " to " + serverAddress + ":"+ serverPort);
try
{
// get the 锟斤拷registry锟斤拷
registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)).intValue());
// look up the remote object
rmiServer = (ReceiveMessageInterface) (registry.lookup("rmiServer"));
// call the remote method
System.out.println(rmiServer.receiveMessage(text));
} catch (RemoteException e)
{
e.printStackTrace();
} catch (NotBoundException e)
{
e.printStackTrace();
}
}
}
另外,对于多个对象我们可以指定为相同的端口也可以指定为不同的端口,只要保证绑定的key指唯一就可以了。当然在服务器端指定端口后,启动RMI注册服务和客户端进行lookup时要与服务器指定的端口相同。一个端口一个registry。