zoukankan      html  css  js  c++  java
  • java RMI

    import java.rmi.*;
    
    public interface Hello extends Remote {
        public String getGreeting() throws RemoteException;
    }
    import java.rmi.*;
    import java.rmi.server.*;
    
    public class HelloImpl extends UnicastRemoteObject implements Hello {
        
        public HelloImpl() throws RemoteException {
            // No action needed here.
        }
    
        public String getGreeting() throws RemoteException {
            return ("Hello there!");
        }
    }
    import java.rmi.*;
    
    public class HelloServer {
        private static final String HOST = "localhost";
    
        public static void main(String[] args) throws Exception {
            // Create a reference to an
            // implementation object
            HelloImpl temp = new HelloImpl();
            // Create the string URL holding the
            // object's name
            String rmiObjectName = "rmi://" + HOST + "/Hello";
            // (Could omit host name here, since 'localhost'
            // would be assumed by default.)
            // 'Bind' the object reference to the name
            Naming.rebind(rmiObjectName, temp);
            // Display a message so that we know the process
            // has been completed
            System.out.println("Binding complete…
    ");
        }
    }
    import java.rmi.*;
    
    public class HelloClient {
        private static final String HOST = "localhost";
    
        public static void main(String[] args) {
            try {
                // Obtain a reference to the object from the
                // registry and typecast it into the appropriate
                // type
                Hello greeting = (Hello) Naming.lookup("rmi://" + HOST + "/Hello");
                // Use the above reference to invoke the remote
                // object's method
                System.out.println("Message received: " + greeting.getGreeting());
            } catch (ConnectException conEx) {
                System.out.println("Unable to connect to server!");
                System.exit(1);
            } catch (Exception ex) {
                ex.printStackTrace();
                System.exit(1);
            }
        }
    }

    编译运行用到的命令

    note
    
    javac Hello.java
    javac HelloImpl.java
    javac HelloServer.java
    javac HelloClient.java
    
    //This would generate both a stub fi le and a skeleton fi le. However, this stage is no
    //longer required
    rmic HelloImpl
    
    //Starting the RMI registry
    rmiregistry
    
    //start server
    java HelloServer
    
    //start client
    java HelloClient
    
    
    //result
    "Hello there!"
  • 相关阅读:
    2015网易校招Java开发工程师(技术架构)在线笔试题
    2015百度校招用户行为分析研发工程师笔试题
    2016届百度实习生前端笔试题上海卷a
    数据库 三范式最简单最易记的解释
    从几个sample来学习JAVA堆、方法区、JAVA栈和本地方法栈
    C++中虚函数和纯虚函数的总结
    MFC一些基本控件操作的总结
    单文档多视图一些基本操作
    MFC单文档静态分割视图
    iOS通讯录相关知识-浅析
  • 原文地址:https://www.cnblogs.com/rojas/p/5390111.html
Copyright © 2011-2022 走看看