gpt4 book ai didi

java - 另一个 RMI 问题

转载 作者:行者123 更新时间:2023-12-02 06:30:01 26 4
gpt4 key购买 nike

在你说之前,是的,我知道,Stackoverflow 上有很多讨论相同问题的线程,但其中任何一个都解决了我的问题。我的问题是在两台不同的计算机中使用 RMI 接口(interface)(这对于我的目的是必需的),其中一台提供 RMI 对象(服务器),另一台请求 stub 并获取代理。我的代码与 Oracle Java 7 引用手册提供并由 Oracle Press 编辑的代码完全相同:

IRmi.java

import java.rmi.*;

public interface IRmi extends Remote {
double add() throws RemoteException;
}

RmiImpl.java

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

public class RmiImpl extends UnicastRemoteObject implements IRmi {
public RmiImpl() throws RemoteException {}

public double add() { double d = 5.0; return d; }
}

服务器.java

import java.net.*;
import java.rmi.*;

public class Server {

public static void main(String s[]) {
try {
RmiImpl ri = new RmiImpl();
Naming.rebind("Server",ri);
} catch (Exception e) {
System.err.println("Err");
}
}

}

Client.java

import java.rmi.*;

public class Client {

public static void main(String s[]) {
try {
IRmi itf = (IRmi)Naming.lookup("rmi://192.168.0.8/Server");
System.out.println(itf.add());
} catch (Exception e) {
e.printStackTrace();
}
}
}

之后,我编译:

javac IRmi.java
javac RmiImpl.java
rmic RmiImpl
javac Client.java
javac Server.java

在这段代码之后,我复制了客户端和服务器上的所有类,然后在传输类的同一文件夹上运行 rmiregistry。假设在我的本地局域网(192.168.0.0/255)中有两台机器,其中客户端是192.168.0.3,服务器192.168.0.8我分别在这些机器上运行java client java 服务器,客户端返回以下错误:

java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: 
java.net.ConnectException: Connessione rifiutata
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.invoke(UnicastRef.java:129)
at RmiImpl_Stub.add(Unknown Source)
at Client.main(Client.java:8)
Caused by: java.net.ConnectException: Connessione rifiutata
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 5 more

例如,在另一个测试中,我还尝试使用以下代码来实现服务器:

    try {
RemImpl obj = new RemImpl(this.serv);
if (this.ob_list.size()>0) {
for (Observer ob: ob_list) {
obj.addObserver(ob);
}
}
this.myrec = (Rem) UnicastRemoteObject.exportObject(obj, 9999);
Registry registry = LocateRegistry.createRegistry(9999);
registry.rebind(this.serv, this.myrec);
//this.has_error = false;
System.out.println("Binded as "+this.serv);
} catch (RemoteException e) {
System.err.println("Remote exception catched: " + e.getMessage());
//this.has_error = true;
this.myrec = null;
}

以及具有以下其他代码的客户端:

    try {
Registry registry = LocateRegistry.getRegistry(host);
this.myrec = (Rem) registry.lookup(service);
} catch (Exception e) {
e.printStackTrace();
}

并且,在这种情况下,客户端会向我返回以下不同的错误:

java.rmi.NoSuchObjectException: no such object in table
ERROR
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy0.send(Unknown Source)
at rmi.lowlevel.NullSenderPolicy.send(NullSenderPolicy.java:81)
at message.policy.old.BroadcastSenderPolicy.single_send(BroadcastSenderPolicy.java:104)
at message.policy.old.AtomicBroadcastSender.fifo_send(AtomicBroadcastSender.java:54)
at message.policy.old.AtomicBroadcastNode.fifo_send(AtomicBroadcastNode.java:131)
at elements.testunit.TestPairBroadcastNodes.main(TestPairBroadcastNodes.java:20)

此时我不知道该转向哪个方向。预先感谢您的任何其他建议。

最佳答案

连接拒绝似乎是item A.1 in the RMI FAQ的情况.

NoSuchObjectInTable 问题是因为您查找了错误的注册表。您在端口 9999 上创建了它,但您正在查找另一个端口。这个问题可以通过在客户端调用 getRegistry(serverHost, 9999) 来解决。

您还应该在服务器 JVM 中将注册表引用设为静态。

关于java - 另一个 RMI 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20153657/

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