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!"
  • 相关阅读:
    (原创)sqlite封装库SmartDB1.3发布
    合索引 与 单一列的索引
    Sql中CHARINDEX用法
    Eclipse 的快捷键以及文档注释、多行注释的快捷键
    JAVA 方法或者类的注释快捷键
    关于/r与/n 以及 /r/n 的区别总结
    c#中Split 分离字符以及空格消除方法
    C#生成Guid的几种方式
    MVC ViewBag和ViewData的使用
    软考之高级系统架构设计师(包含历年真题详解+课本教程+论文范文+视频教程)
  • 原文地址:https://www.cnblogs.com/rojas/p/5390111.html
Copyright © 2011-2022 走看看