当前位置: 技术问答>java相关
谁能给我一个RMI的程序?
来源: 互联网 发布时间:2015-08-16
本文导语: 谁能给我一个RMI的程序?简单点的 | import java.rmi.Remote; import java.rmi.RemoteException; public interface RemoteInterface extends Remote{ String message(String message)throws RemoteException; } 客户 import java.rmi.RMISecur...
谁能给我一个RMI的程序?简单点的
|
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteInterface extends Remote{
String message(String message)throws RemoteException;
}
客户
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
public class RemoteClient
{
public static void main(String args[]){
System.setSecurityManager(new RMISecurityManager());
try{
RemoteInterface server = (RemoteInterface)Naming.lookup("servertext");
String serverString = server.message("hello");
}
catch(Exception e){
System.out.println("e");
}
}
}
服务器
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface{
String name;
public RemoteObject(String name) throws RemoteException{
super();
this.name=name;
}
public String message(String message) throws RemoteException{
String returnString = name+message;
System.out.println(returnString);
return returnString;
}
public static void main(String args[]){
try{
String myName = "servertext";
RemoteObject theserver = new RemoteObject(myName);
Naming.rebind(myName,theserver);
System.out.println("ksdkfksd");
}
catch(Exception e){
System.out.println(e);
}
}
}
import java.rmi.RemoteException;
public interface RemoteInterface extends Remote{
String message(String message)throws RemoteException;
}
客户
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
public class RemoteClient
{
public static void main(String args[]){
System.setSecurityManager(new RMISecurityManager());
try{
RemoteInterface server = (RemoteInterface)Naming.lookup("servertext");
String serverString = server.message("hello");
}
catch(Exception e){
System.out.println("e");
}
}
}
服务器
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface{
String name;
public RemoteObject(String name) throws RemoteException{
super();
this.name=name;
}
public String message(String message) throws RemoteException{
String returnString = name+message;
System.out.println(returnString);
return returnString;
}
public static void main(String args[]){
try{
String myName = "servertext";
RemoteObject theserver = new RemoteObject(myName);
Naming.rebind(myName,theserver);
System.out.println("ksdkfksd");
}
catch(Exception e){
System.out.println(e);
}
}
}