无法在RMI通信中调用远程方法

时间:2012-05-10 11:41:54

标签: rmi

我正在尝试执行一个RMI程序,但是当我尝试从RMI客户端程序调用远程方法时,我遇到异常。

服务器程序:

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


public class Hello extends UnicastRemoteObject implements HelloInterface {

    private String message;

    public Hello() throws RemoteException{
           int port=1024; 
          Registry registry;
           try{
              registry = LocateRegistry.createRegistry(port);
              registry.rebind("samplermi", this);
              System.out.println ("Server started and listening on port " + port);

          }
          catch(RemoteException e){
              System.out.println("remote exception"+ e);
          }
      }

      public String sayHi (String name) throws RemoteException {
          message = "Hi .. Welcome " + name;
      return message;
      }


      public static  void main(String args[]){
          try{
              Hello serverObj = new Hello();
          }
          catch (Exception e){
          e.printStackTrace();
          System.exit(1);
          }
          }


}

客户计划:

registry=LocateRegistry.getRegistry(serverAddress,serverPort);
          if(registry !=null){
               String[] availRemoteServices = registry.list();
              for(int i=0;i<availRemoteServices.length;i++){
                  System.out.println("Service " + i + ": " +availRemoteServices[i]);                  
              }           
          }
           rmiServer=(HelloInterface)(registry.lookup("samplermi"));

          System.out.println("calling remote method!");
          // call the remote method
          welcomeMsg = rmiServer.sayHi(text);
      System.out.println("Message from server: " + welcomeMsg);

我只在调用远程方法sayHI时才获得连接异常。它适用于查找和列出服务名称。

R:\Deptapps\itdm\Sample_RMI>java NewSampleRMIClient
Getting Registry Object from server!!
Registry Object Created!!
Service 0: samplermi
Services listed successfully!

Look up successful!
calling remote method!

java.rmi.ConnectException: Connection refused to host; nested exception is:
        java.net.ConnectException: Connection timed out: connect
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
        at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
        at sun.rmi.server.UnicastRef.invoke(Unknown Source)
        at Hello_Stub.sayHi(Unknown Source)
        at NewSampleRMIClient.main(NewSampleRMIClient.java:42)
Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Unknown Source)
        at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)

注意:在windows中运行solaris和client中的服务器时,相同的程序正常工作。它仅在从AIX运行服务器和从Windows运行客户端时才起作用。

请有人帮忙解决这个问题。我一直试图解决这个问题,因为2天但没有用。请帮帮我!!

3 个答案:

答案 0 :(得分:0)

Item A.1 of the RMI FAQ中介绍了这一点。

答案 1 :(得分:-1)

在运行rmiregistry.exe之前运行Hello.class,它解决了我的问题。

答案 2 :(得分:-1)

RMi使用默认端口1099.因此无需创建端口..如果使用默认端口号,则可能不会触发异常。和程序可以正常工作。