标签:++ local target rgs inter remote row str ace
Java RMI(远程方法调用)示例程序
1. 编写接口
import java.rmi.*;
public interface HelloIn extends java.rmi.Remote{
String sayHello() throws RemoteException;
}
2. 实现远程接口
import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class Hello extends java.rmi.server.UnicastRemoteObject implements HelloIn {
public Hello() throws RemoteException{
super();
}
public String sayHello() throws RemoteException{
return "Hello,World!";
}
public static void main(String[] args){
try {
Hello h=new Hello();
java.rmi.Naming.rebind("hello",h);
System.out.print("Ready......");
} catch(Exception e) {
e.printStackTrace();
}
}
}
3. 客户端程序
import java.rmi.*;
import java.rmi.registry.*;
public class Helloworld{
public static void main(String[] args){
try{
HelloIn hi=(HelloIn)Naming.lookup("//localhost/hello");
for(int i=0;i<10;i++){
System.out.println(hi.sayHello());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}陕西老医协生殖医院
4. 启动服务端
Rmiregistry &
java Hello &
陕西老医协生殖医院
5. 启动客户端
java Helloworld
标签:++ local target rgs inter remote row str ace
原文地址:https://www.cnblogs.com/javaahb/p/13225087.html