jdk1.5 下 rmi的编写
程序员文章站
2024-02-21 17:09:10
...
jdk1.5 自动生成stub 更加方便
接口Hello
服务端
客户端
接口Hello
package example.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
服务端
package example.hello;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.createRegistry(1078);
registry.bind("server", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
客户端
package example.hello;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
//可以用Naming.lookup("rmi://localhost:1078/server");
//来替换下面代码
Registry registry = LocateRegistry.getRegistry(null,1078);
Hello stub = (Hello) registry.lookup("server");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
上一篇: 可能会用到的CSS样式
下一篇: 微信小程序 bnner滚动实例详解
推荐阅读
-
jdk1.5 下 rmi的编写
-
MySQL学习笔记_6_SQL语言的设计与编写(下)_MySQL
-
自己编写了个PHP扩展模块,在扩展模块中需要调用另外一个动态库,也是自己写的。Linux下如何
-
用php语言编写的网页能在ipad下通过safari浏览吗
-
Python实现Linux下守护进程的编写方法
-
使用Python编写一个在Linux下实现截图分享的脚本的教程
-
linux下c语言入门04 main函数的编写
-
C语言:编写程序数一下 1到 100 的所有整数中出现多少次数字 9
-
[编写高质量iOS代码的52个有效方法](五)接口与API设计(下)
-
[编写高质量iOS代码的52个有效方法](八)内存管理(下)