Java rmi ConnectException连接拒绝托管

时间:2014-11-18 20:05:52

标签: java rmi connectionexception

java.rmi.ConnectException: Connection refused to host: 10.0.0.100; nested exception is: 
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at guiproject.AddClient.main(AddClient.java:12)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 6 more

import java.rmi.*;

public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws Exception;
}

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

public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf{    

public AddServerImpl() throws RemoteException{
}
    public double add(double d1, double d2) throws RemoteException{
        return d1 + d2;
    }

public static void main(String args[]){

}
}

public class AddServer {        
public static void main(String args[]){
    try{
        AddServerImpl addServerImpl = new AddServerImpl();
        Naming.rebind("AddServer", addServerImpl);
    }
    catch(Exception e){
        e.printStackTrace();
    }
 }
}

//the client side
public class AddClient {

public static void main(String args[]){
    try{            
        Scanner in = new Scanner(System.in);           
        String addServerURl = "rmi://" + "/AddServer";
        AddServerIntf addServerintf = (AddServerIntf)Naming.lookup(addServerURl);
        System.out.println("The first Numer is :");
        double d1 = in.nextDouble();
        System.out.println("The second number is:");
        double d2 = in.nextDouble();

        System.out.println("The sum is:" +addServerintf.add(d1,d2));            
    }
    catch(Exception e){
        e.printStackTrace();
    }
 }
}

每次我连接到我的客户端时都会遇到此异常。我读了一些关于Connection异常的东西,但没有一个解决方案似乎有效。 // 该代码也按要求粘贴在上面,我希望这有效......

2 个答案:

答案 0 :(得分:1)

由于您尚未发布任何代码,我认为异常的原因不是由实现本身引起的。

如果远程主机没有响应,通常会发生此异常。可能的原因可能是:

  • 您尚未开始rmiregistry
  • 较新版本的Java(自7u21起为afaik)需要以下命令:rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false
  • 防火墙阻止连接

答案 1 :(得分:0)

String addServerURl = "rmi://" + "/AddServer";

您正在尝试连接错误的注册表。除非服务器和客户端在同一主机上,在这种情况下你几乎不需要RMI,这一行应该是:

String addServerURl = "rmi://" + serverHost + "/AddServer";

其中serverHost是运行RMI服务器和注册表的IP地址或主机名。