<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    瘋狂

    STANDING ON THE SHOULDERS OF GIANTS
    posts - 481, comments - 486, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
    場景: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://m.tkk7.com/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("http://10.10.XX.XX:8808/SAMPLE-SERVER", server);  
        }

    (二)客戶端 調用服務:

    public static void main(String[] args) throws Exception {
            RmiInterface server=(RmiInterface) Naming.lookup("http://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("http://10.10.116.XX:8808/SAMPLE-SERVER");,但是服務端返回的服務的引用里面包含的ip并不是lookup時的ip。
    官方說法:
    【In many versions of the JDK (all versions of the JDK except in v1.1 and the latest releases), 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("http://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("http://10.10.116.74:8808/SAMPLE-SERVER", server);  
        }
    為什么呢:
        RmiServer 這個實現類使用了UnicastRemoteObject去聯接RMI系統。在我們的例子中,我們是直接的從UnicastRemoteObject這個類上繼承的,事實上并不一定要這樣做,當然也可以不是從UnicastRmeoteObject上繼承,那必須使用它的exportObject()方法去聯接到RMI。如果一個類繼承自UnicastRemoteObject,那么它必須提供一個構造函數并且聲明拋出一個RemoteException對象。當這個構造函數調用了super(),它久激活UnicastRemoteObject中的代碼完成RMI的連接和遠程對象的初始化。而此時應該已經決定了使用哪個hostname來實例化遠程對象。因此必須在服務對象創建之前指定綁定的hostname。

    ~~~完。
    主站蜘蛛池模板: 女性自慰aⅴ片高清免费| 国产又黄又爽又猛免费app| gogo全球高清大胆亚洲| 亚洲中文字幕无码中文| 成在人线AV无码免费| 亚洲一级毛片中文字幕| 国产卡一卡二卡三免费入口| 亚洲乱码卡一卡二卡三| 日本免费网站视频www区| 亚洲一区二区三区91 | 中文在线观看永久免费| 亚洲色成人WWW永久网站| 久久精品免费一区二区三区| 亚洲欧洲日产国码久在线观看| 亚洲最大免费视频网| 亚洲精品国产国语| 四虎永久免费地址在线观看| 成人a毛片免费视频观看| 亚洲人成网站在线观看播放| 91精品啪在线观看国产线免费| 亚洲精品中文字幕麻豆| 最近2019中文字幕mv免费看| 九九精品国产亚洲AV日韩| 亚洲七七久久精品中文国产| 十八禁视频在线观看免费无码无遮挡骂过 | 成人毛片免费视频| 美女视频黄a视频全免费网站色| 久久久久一级精品亚洲国产成人综合AV区 | 一级毛片免费在线播放| 午夜亚洲www湿好大| 丁香花免费高清视频完整版| 国产成人亚洲综合在线| 久久久久亚洲AV成人无码| 精品久久久久久久久免费影院| 日本亚洲高清乱码中文在线观看| 国产美女亚洲精品久久久综合| 久久久久成人片免费观看蜜芽| 亚洲av无码一区二区三区天堂古代 | 亚洲国产日韩一区高清在线| 成人毛片18岁女人毛片免费看| 中文在线日本免费永久18近|