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();
}
}
}
每次连接到客户端时,我都会收到此异常。我阅读了一些有关连接异常的内容,但似乎没有一个解决方案有效。//代码也按要求粘贴在上面,我希望这有效...
由于您尚未发布任何代码,因此我认为异常的原因不是由实现本身引起的。
如果远程主机没有响应,通常会发生此异常。可能的原因可能是:
- 您尚未开始
rmiregistry
. - 较新版本的Java(自7u21起的afaik)需要以下命令:
rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false
- 防火墙阻止了连接
String addServerURl = "rmi://" + "/AddServer";
您尝试连接到错误的注册表。除非服务器和客户端在同一主机上,在这种情况下,您几乎不需要 RMI,否则此行应为:
String addServerURl = "rmi://" + serverHost + "/AddServer";
其中serverHost
是运行 RMI 服务器和注册表的 IP 地址或主机名。