zoukankan      html  css  js  c++  java
  • 远程访问对象java实现

    服务端:

    定义远程接口:

    package com.my.rmi;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    
    public interface Impl extends Remote{
    public String name() throws RemoteException;
    }
    

     实现接口:

    package com.my.rmi;
    
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    
    public class RmtImpl extends UnicastRemoteObject implements Impl{
    	
    	private int i;
    	
    	public RmtImpl(int i) throws RemoteException {
    		super();
    		this.i = i;
    	}
    	public RmtImpl() throws RemoteException{
    	}
    	public String name() throws RemoteException{
    		return "test"+20*i;
    	}
    }
    

    注册远程对象:

    package com.my.rmi;
    
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    
    public class rmi {
    public static void main(String[] args) {
              Registry r = null ;
              try {
        RmtImpl s = new RmtImpl(25);
        r = LocateRegistry.createRegistry(50000);
                    //也可以用它注册远程服务
                 //Naming.rebind("rmi://192.168.6.66:50000/mytest", s);
        r.rebind("mytest", s);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    }

     客户端:

    package com.my.rmi;
    
    import java.rmi.Naming;
    
    public class ClientRMI {
    public static void main(String[] args) {
    	try {
    	Impl oo = (Impl)Naming.lookup("rmi://192.168.6.66:50000/mytest");
    	System.out.println(oo.name());
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	}
    }
    
  • 相关阅读:
    [JavaScript]JS由来
    [HTML5]HTML表单(Forms)
    Linux 配置SSH 无密钥登陆
    Spring Boot 的 application.properties
    Spring Boot 全局异常捕获
    Linux hostname设置,静态ip设置,hostname与静态ip相互映射
    Hadoop 集群的三种方式
    网站列表
    Hadoop -- 概念
    shell 编程
  • 原文地址:https://www.cnblogs.com/wbjgogogo/p/5383302.html
Copyright © 2011-2022 走看看