简体   繁体   English

java RMI连接到服务器

[英]java RMI connection to server

I have a very simple rmi client / server application. 我有一个非常简单的rmi客户端/服务器应用程序。 I don't use the "rmiregistry" application though, I use this to create the server: 我不使用“rmiregistry”应用程序,我使用它来创建服务器:

server = new RemoteServer();
registry = LocateRegistry.createRegistry(PORT);
registry.bind("RemoteServer", server);

The client part is: 客户端部分是:

registry = LocateRegistry.getRegistry(IPADDRESS, PORT);
remote = (IRemoteServer) registry.lookup("RemoteServer");

Here is the fascinating problem: The application works perfectly when both server and client are running in my (private) local network. 这是一个引人入胜的问题:当我的(私有)本地网络中运行服务器和客户端时,应用程序运行良好。 As soon as I put the server on a public server, the application hangs for a minute, then gives me the following error: 一旦我将服务器放在公共服务器上,应用程序挂起一分钟,然后给我以下错误:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.ConnectException: Connection refused to host: 192.168.x.y; nested exception is: 
    java.net.ConnectException: Connection timed out: connect
    at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
    at sun.rmi.transport.Transport$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source  ... (the rest is truncated)

The key I think is that the client (running on my private network) cannot connect to myself (my address is 192.168.xy where xy is some other numbers, but the real error message shows my ip address listed there) 我认为关键是客户端(在我的专用网络上运行)无法连接到我自己 (我的地址是192.168.xy,其中xy是其他一些数字,但真正的错误消息显示我的IP地址列在那里)

If I kill the rmi server on the public internet, then I instantly get a "connection refused to host: abcd") message, so I know that something at the server end is at least working. 如果我在公共互联网上杀死rmi服务器,那么我立即得到一个“连接拒绝主机:abcd”的消息,所以我知道服务器端的某些东西至少有效。

Any suggestions? 有什么建议?

EDIT: just to make this a little more clear: 192.168.xy is the client address, abcd is the server address. 编辑:只是为了使这更清楚:192.168.xy是客户端地址,abcd是服务器地址。 The stacktrace shows the client cannot connect to the client. stacktrace显示客户端无法连接到客户端。

尝试使用此参数为虚拟机运行服务器:

-Djava.rmi.server.hostname=hostname_of_the_server

192.168.* contains private IP addresses (as does 10.*). 192.168。*包含私有IP地址(10. *)。 These will not be routed on the open internet. 这些不会在开放的互联网上路由。 You need to make sure that you are using an public IP address for the server, and any firewalls (including NAT) are configured for access. 您需要确保使用服务器的公共IP地址,并配置任何防火墙(包括NAT)以进行访问。 You can do a quick check with telnet on the required port. 您可以在所需端口上使用telnet快速检查。

I would believe that the server tries to open a socket back to the client and it fails, and the exception is a bit unclear on its wording. 我相信服务器试图打开一个套接字回到客户端,它失败了,并且它的措辞有点不清楚。

RMI is not very NAT-friendly, IIRC. RMI对NAT不友好,IIRC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM