gpt4 book ai didi

Java RMI 实现

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:34 25 4
gpt4 key购买 nike

我有一个典型的客户端和服务器情况,我希望客户端能够从服务器上运行的对象调用方法。我使用Java RMI来解决这种情况。

服务器端代码如下,它编译并运行良好:

ServerInt.java

package gpio.control;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ServerInt extends Remote{
public void setHeizung(boolean x) throws RemoteException;
public void quitError() throws RemoteException;
public void startPWM(int periode,int pulsbreite,int pulsanzahl) throws RemoteException;
}

ServerImpl.java

package gpio.control;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RemoteServer;
import java.rmi.server.UnicastRemoteObject;

public class ServerImpl implements ServerInt{
private Model myModel;

public ServerImpl(Model m) throws RemoteException {
myModel = m;

LocateRegistry.createRegistry(1099);
ServerInt stub = (ServerInt) UnicastRemoteObject.exportObject(this, 1099);
RemoteServer.setLog(System.out);

Registry registry = LocateRegistry.getRegistry();
registry.rebind("Server1", stub);
}

@Override
public void setHeizung(boolean x) throws RemoteException {
myModel.setHeizung(x);
}

@Override
public void quitError() throws RemoteException {
myModel.quitError();
}

@Override
public void startPWM(int periode, int pulsbreite, int pulsanzahl) throws RemoteException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

出于测试目的,我还创建了一个小型客户端应用程序,它与服务器应用程序在同一服务器上运行:

客户端.java

package client;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {

public static void main(String[] args) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry();
ServerInt serverint = (ServerInt) registry.lookup("rmi://127.0.0.1:1099/Server1");
serverint.setHeizung(true);
}

}

当我现在运行客户端程序时,出现以下错误:客户端错误:

Exception in thread "main" java.rmi.NotBoundException: rmi://localhost:1099/Server1
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$241(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at client.Client.main(Client.java:12)

服务器错误:

Sep 07, 2016 9:47:04 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(1)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: void rebind(java.lang.String, java.rmi.Remote)]
Sep 07, 2016 9:47:04 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(2)-127.0.0.1: [127.0.0.1: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
Sep 07, 2016 9:47:05 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(3)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: java.rmi.Remote lookup(java.lang.String)]
Sep 07, 2016 9:47:05 AM sun.rmi.server.UnicastServerRef logCallException
FINE: RMI TCP Connection(3)-127.0.0.1: [127.0.0.1] exception:
java.rmi.NotBoundException: rmi://localhost:1099/Server1
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$241(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我在这里做错了什么?

最佳答案

在Client.java中更改

registry.lookup("rmi://127.0.0.1:1099/Server1")

registry.lookup("Server1");

关于Java RMI 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39446583/

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