Exemplo n.º 1
0
  public Peer() {
    try {
      share = new PeerImpl();
      // check usable port and register the port
      for (int i = 0; i < PeerInfo.rmi.portList.length; i++) {
        if (checkPort(i)) {
          // register
          PeerInfo.rmi.port = PeerInfo.rmi.portList[i];
          PeerInfo.rmi.fileSharing = "rmi://localhost:" + PeerInfo.rmi.port + "/share";
          LocateRegistry.createRegistry(PeerInfo.rmi.port);
          break;
        }

        if (i == 2) {
          System.out.println("All ports are occupied!");
          System.exit(-1);
        }
      }
      // bind
      Naming.bind(PeerInfo.rmi.fileSharing, share);
      System.out.println("File sharing service start!");
    } catch (RemoteException e) {
      e.printStackTrace();
      System.out.println("Fail to create remote object!");
    } catch (MalformedURLException e) {
      e.printStackTrace();
      System.out.println("URL error!");
    } catch (AlreadyBoundException e) {
      e.printStackTrace();
      System.out.println("Service already bound!");
    }
  }
Exemplo n.º 2
0
  public static void main(String[] args) {
    try {
      // 创建一个远程对象
      IHello rhello = new HelloImpl();
      // 生成远程对象注册表Registry的实例,并指定端口为8888(默认端口是1099)

      LocateRegistry.createRegistry(6789);
      // 把远程对象注册到RMI注册服务器上,并命名为RHello
      // 绑定的URL标准格式为:rmi://host:port/name(协议名可以省略)
      Naming.bind("rmi://localhost:6789/RHello", rhello);
      System.out.println(">>INFO:远程IHello对象绑定成功!");

    } catch (RemoteException e) {
      System.out.println("创建远程对象发生异常!");
      e.printStackTrace();
    } catch (MalformedURLException e) {
      System.out.println("发生重复绑定对象异常!");
      e.printStackTrace();
    } catch (AlreadyBoundException e) {
      System.out.println("发生URL畸形异常!");
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {

    try {

      // Creation d'un "annuaire" des services
      // remote qu'on va fournir
      LocateRegistry.createRegistry(1099);

      if (System.getSecurityManager() == null) {
        System.setSecurityManager(new RMISecurityManager());
      }

      // J'instancie mon fournisseur de service meteo
      MeteoServiceImpl meteoserv = new MeteoServiceImpl();

      // Je construis l'url de mon service
      String adresse = "rmi://" + InetAddress.getLocalHost().getHostAddress() + "/meteoRMI";

      System.out.println("url = " + adresse);

      Naming.bind(adresse, meteoserv);

    } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (AlreadyBoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }