1

我正在尝试执行一个 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)

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

请有人帮助解决这个问题。自 2 天以来,我一直在尝试解决此问题,但没有用。请帮我!!

4

3 回答 3

0

在 RMI FAQ 的 A.1 项中有介绍。

于 2012-05-11T05:07:52.337 回答
-1

先运行rmiregistry.exe再运行Hello.class,它解决了我的问题。

于 2012-05-27T18:02:22.283 回答
-1

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

于 2013-05-22T05:24:48.380 回答