当我使用类文件测试 RMI 客户端和服务器时,它可以在两台不同的机器上正常工作。现在,当我对 jar 执行相同操作时,客户端无法连接到服务器上的 RMIRegistry。然后,当我在同一台机器上使用 jar 进行测试时,客户端可以连接到服务器(即使通过网络 ip 而不是 localhost)。
我怀疑这与 RMI 注册表有关,但我是 RMI 的新手,所以我不知道......
我像这样创建一个 RMIRegistry:
rmiregistry = LocateRegistry.createRegistry(ServiceConstraints.REGISTRY_PORT);
rmiService = (RmiSubjectService) UnicastRemoteObject.exportObject(this,
ServiceConstraints.REGISTRY_PORT);
rmiregistry.rebind("RmiService", rmiService);
客户端连接如下:
remoteService = (RmiSubjectService) Naming.lookup("/" + host + ":"+ port + "/RmiService");
使用 jar 运行时我在客户端上遇到的错误:
java.rmi.ConnectException: Connection refused to host: 192.168.2.132; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at ch.uzh.ifi.group10.client.Client.subscribe(Client.java:148)
at ch.uzh.ifi.group10.client.Client.main(Client.java:242)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
... 13 more