2012-05-10 28 views
1

我试图执行一个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) 

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

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

+0

客户端和服务器之间是否有可能导致连接失败的网络设备? –

回答

-1

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

+0

它不会解决这个问题。他的注册表查询显然已经成功。没有答案。 – EJP

-1

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

+0

不正确。你必须提供一个端口号给'LocateRegistry.createRegistry()'。有一个默认的*值,*由'Registry.REGISTRY_PORT'给出,但你必须将它提供给'LocateRegistry.createRegistry()'。这并没有回答这个问题。他的'lookup()'成功了,它的*方法调用失败了。 – EJP