日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RMI使用小例

發布時間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RMI使用小例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

1、定義一個接口(繼承Remote):

方法要拋RemoteException。

?

package rmi;


import java.rmi.RemoteException;


public interface IAdd extends java.rmi.Remote{

int add ( int a , int b ) throws RemoteException; ?

?

?

}

?

?

?

2、定義一個類作為服務端?(繼承UnicastRemoteObject ?,實現接口IAdd 、,Serializable):

構造和方法要拋RemoteException。

?

package rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class AddServer extends java.rmi.server.UnicastRemoteObject implements IAdd?, Serializable{

public AddServer() throws RemoteException { ?
super();
}


public int add ( int a ,int b ) throws RemoteException {

return a+b ;
}

public static void main(String[] args) {


try { ?? ?
AddServer as = new AddServer(); ?
Registry registry = LocateRegistry.createRegistry(2500); ??

// ?取名 add
? ? ? ? ? ?registry.bind("add", as); ?

? ? ? ? ? ? ? // ?提示服務端開啟
? ? ? ? ? ??? ? ?System.out.println("AddServer Start..."); ?


} catch (Exception e) { ??
e.printStackTrace(); ??
} ?
}
}

?

3、測試類(作為客戶端)

package testdb;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import rmi.IAdd ;

public class Test {

? public static void main(String[] args) {

try {
// ?最后拼接的名字來自AddServer 類中取的名字。
IAdd ?add = ?(?IAdd )Naming.lookup("rmi://127.0.0.1:2500/add");

// ?測試加法運算
System.out.println( " a+b= " + add . add ( 2,3) );

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} ??
? ? ?}
}

?

注:?IAdd ?add = ?(?IAdd?)Naming.lookup("rmi://127.0.0.1:2500/add"); ? 此行中?add 是來自服務端??registry.bind("add", as); ?中設置的名字:add 。

即:服務端??registry.bind(" remove ", as); ? ,那么客戶端就是??IAdd ?add = ?(?IAdd?)Naming.lookup("rmi://127.0.0.1:2500 /?remove?"); ? 。

?

?

?

?

總結

以上是生活随笔為你收集整理的RMI使用小例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。