关于java rmi在多网卡下(或者启动360,百度,腾讯等wifi共享下)无法连接问题(java.rmi.connectexception: connection refused to host: xx) -凯发k8网页登录

standing on the shoulders of giants
posts - 481, comments - 486, trackbacks - 0, articles - 1
  凯发k8网页登录-凯发天生赢家一触即发官网 :: 凯发k8网页登录首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
场景:java rmi 在服务端者启动360 wifi共享,报错java.rmi.connectexception: connection refused to host: xx。
         也就是服务端在调用时使用了wifi共享网卡的地址。此地址在rmi客户端pc上无法ping通。(因为没有连接此wifi。当然rmi客户端pc如果连接此wifi是不会报错的)。
想关资料:

http://docs.huihoo.com/java/rmi/whitepage/index.html
比较全的解释rmi的英文资料:http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/faq.html#netunknownhost
http://www.blogjava.net/shaolijun/archive/2007/05/22/119213.html

测试代码

(一)服务端:
  • 服务接口
import java.rmi.remote;
import java.rmi.remoteexception;
/**
 * rmi remote 接口
 * 
@author joe
 * @2014-12-5 @上午11:49:10
 
*/
public interface rmiinterface extends remote{
    
    public string say(string name) throws remoteexception;

}
  • 接口实现
import java.rmi.remoteexception;
import java.rmi.server.unicastremoteobject;


public class rmiserver extends unicastremoteobject implements rmiinterface{
    
    private static final long serialversionuid = 1l;

    protected rmiserver() throws remoteexception {
        super();
    }

    public string say(string name) throws remoteexception {
        return "hello," name;
    }
}
  • 发布服务
public static void main(string[] args) throws malformedurlexception, remoteexception, alreadyboundexception {
        rmiserver server=new rmiserver();
        locateregistry.createregistry(8808);  
        naming.rebind("//10.10.xx.xx:8808/sample-server", server);  
    }

(二)客户端 调用服务:

public static void main(string[] args) throws exception {
        rmiinterface server=(rmiinterface) naming.lookup("//10.10.116.xx:8808/sample-server");
        system.out.println(server.say("张三"));
    }

此时报错,java.rmi.connectexception: connection refused to host: 192.168.23.x。

rmi的调用原理基本如下:

大致翻译如下:首先客户端必须通过naming.lookup得到服务端服务的一个指针或者叫指针,一旦拥有的这个应用,客户端将使用服务的引用里面包含的主机名(ip)和端口来访问服务。
    也就是说:虽然我们就服务端的ip和端口去naming.lookup("//10.10.116.xx:8808/sample-server");,但是服务端返回的服务的引用里面包含的ip并不是lookup时的ip。
官方说法:
【in many  (all versions of the jdk except in  and the , java rmi may default to using an unresolvable server hostname (for example: unqualified names, windows internet naming service (wins) names, or unqualified dhcp names). when a java rmi client invokes a remote method using a reference that contains an unresolvable server hostname, the client will throw an unknownhostexception.】

in order to generate functional remote references, java rmi servers must be able to supply a fully qualified hostname or ip address that is resolvable from all java rmi clients (an example of a fully qualified hostname is foo.bar.com). if a java rmi program provides a remote callback operation, then that program serves a java rmi object and consequently, must be able to determine a resolvable hostname to use as its server hostname in the remote references it passes to java rmi clients. vms that make calls to applets that serve remote objects may throwunknownhostexceptions because the applet has failed to provide a usable server hostname.

if your java rmi application throws an unknownhostexception, you can look at the resulting stack trace to see if the hostname that the client is using to contact its remote server is incorrect or not fully qualified.【 if necessary, you can set the java.rmi.server.hostname property on the server to the correct ip address or hostname of the server machine and java rmi will use this property's value to generate remote references to the server.】

解决办法就是在服务端发布注册服务的之前设置:
system.setproperty("java.rmi.server.hostname", 指定ip);

对应到本文例子就是:
public static void main(string[] args) throws malformedurlexception, remoteexception, alreadyboundexception {
        rmiserver server=new rmiserver();
        system.setproperty("java.rmi.server.hostname", 指定ip);
        locateregistry.createregistry(8808);  
        naming.rebind("//10.10.116.74:8808/sample-server", server);  
    }

但是此时还是报相同的错没法访问,百思不得其解,原来java.rmi.server.hostname的设置必须在服务对象创建之前。
public static void main(string[] args) throws malformedurlexception, remoteexception, alreadyboundexception {
        system.setproperty("java.rmi.server.hostname", 指定ip);
        rmiserver server=new rmiserver();
        locateregistry.createregistry(8808);  
        naming.rebind("//10.10.116.74:8808/sample-server", server);  
    }
为什么呢:
    rmiserver 这个实现类使用了unicastremoteobject去联接rmi系统。在我们的例子中,我们是直接的从unicastremoteobject这个类上继承的,事实上并不一定要这样做,当然也可以不是从unicastrmeoteobject上继承,那必须使用它的exportobject()方法去联接到rmi。如果一个类继承自unicastremoteobject,那么它必须提供一个构造函数并且声明抛出一个remoteexception对象。当这个构造函数调用了super(),它久激活unicastremoteobject中的代码完成rmi的连接和远程对象的初始化。而此时应该已经决定了使用哪个hostname来实例化远程对象。因此必须在服务对象创建之前指定绑定的hostname。

~~~完。
网站地图