RMI如何处理存根

时间:2011-06-04 16:56:39

标签: java rmi

任何人都可以解释下面的rmi代码如何在没有存根的情况下工作

ReceiveMessageInterface.java

import java.rmi.*;

public interface ReceiveMessageInterface extends Remote{

  void receiveMessage(String x) throws RemoteException;

}

RmiServer.java

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;

public class RmiServer extends 
  java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface{
  String address;
  Registry registry; 

  public void receiveMessage(String x) throws RemoteException{
  System.out.println(x);
  }

  public RmiServer() throws RemoteException{
  try{  
  address = (InetAddress.getLocalHost()).toString();
  }
  catch(Exception e){
  System.out.println("can't get inet address.");
  }
  int port=3232; 
  System.out.println("this address=" + address +  ",port=" + port);
  try{
  registry = LocateRegistry.createRegistry(port);
  registry.rebind("rmiServer", this);
  }
  catch(RemoteException e){
  System.out.println("remote exception"+ e);
  }
  }
  static public void main(String args[]){
  try{
  RmiServer server = new RmiServer();
  }
  catch (Exception e){
  e.printStackTrace();
  System.exit(1);
  }
  }
}

RMIClient.java

import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class RmiClient{
  static public void main(String args[]){
  ReceiveMessageInterface rmiServer;
  Registry registry;
  String serverAddress=args[0];
  String serverPort=args[1];
  String text=args[2];
  System.out.println
   ("sending " + text + " to " +serverAddress + ":" + serverPort);
  try{
  registry=LocateRegistry.getRegistry
  (serverAddress,(new Integer(serverPort)).intValue());
  rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
  // call the remote method
  rmiServer.receiveMessage(text);
  }
  catch(RemoteException e){
  e.printStackTrace();
  }
  catch(NotBoundException e){
  System.err.println(e);
  }
  }
} 

2 个答案:

答案 0 :(得分:0)

使用java.reflect.Proxy在运行时

中合成接口的实现

答案 1 :(得分:0)

构建或导出时,您所要做的就是提供端口号参数,甚至是0。只需在上面的代码中的构造函数中添加super(0)即可。

相关问题