gpt4 book ai didi

java - RMI Registry 连接到错误的地址

转载 作者:行者123 更新时间:2023-12-04 06:24:38 25 4
gpt4 key购买 nike

我正在尝试运行一个简单的 rmi 应用程序。但是我遇到了(对我来说)无法解释的错误。

我有实现远程接口(interface) Checker 的对象 CheckerImplementation。我正在尝试使用此代码进行设置 [Instance() 返回 CheckerImplementation 类型的对象]:

            try 
{
Checker stub = (Checker) UnicastRemoteObject.exportObject(Instance(), 0);

// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Checker", stub);

System.err.println("Server ready");
}
catch (Exception e)
{
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}

运行时,我得到以下异常:
java.rmi.ConnectException: Connection refused to host: 10.105.124.34; nested exception is: 
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at Checker.CheckerImplementation.main(CheckerImplementation.java:172)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 5 more

为什么 Java 仍然试图连接到 10.105.124.34?那不是本地主机。可能是什么原因,如何解决?

在做了一些实验后,我发现 10.105.124.34 确实是我的 IP 地址。但问题仍然存在:为什么不是 127.0.0.1?

最佳答案

你确定10.105.124.34不是你的本地IP吗?运行ifconfig/ipconfig .这是负责选择此地址的代码( java.rmi.registry.LocateRegistry#getRegistry ):

if (port <= 0)
port = Registry.REGISTRY_PORT;

if (host == null || host.length() == 0) {
// If host is blank (as returned by "file:" URL in 1.0.2 used in
// java.rmi.Naming), try to convert to real local host name so
// that the RegistryImpl's checkAccess will not fail.
try {
host = java.net.InetAddress.getLocalHost().getHostAddress();
} catch (Exception e) {
// If that failed, at least try "" (localhost) anyway...
host = "";
}

总结一下:您需要在您的计算机上本地运行 RMI 注册表(服务器)。显然您正在尝试连接到它(端口 1099),但没有一个正在运行。

关于java - RMI Registry 连接到错误的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com