刚学rmi.什么也不懂,就写了这个,分享一下.
我不太明白,为什么红色那里为什么不需要安全管理器.之前也写了一个最简单的.没有了安全管理器.就发生错误.而这里又不需要.否则又错误.不太明白.
/**
* @(#)Multi.java
*
*
* @author if
* @version 1.00 2007/3/28
*/
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Multi extends Remote{
public int mul(int a,int b) throws RemoteException;
}
/**
* @(#)Plus.java
*
*
* @author
* @version 1.00 2007/3/28
*/
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Plus extends Remote{
public int add(int a,int b) throws RemoteException;
}
/**
* @(#)PlusImpl.java
*
*
* @author if
* @version 1.00 2007/3/28
*/
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class PlusImpl extends UnicastRemoteObject implements Plus{
private String name;
public PlusImpl(String s) throws RemoteException{
super();
this.name=s;
}
public int add(int a,int b) throws RemoteException{
System.out.println("lnvoke the add");
return (a+b);
}
}
/**
* @(#)MultiImpl.java
*
*
* @author if
* @version 1.00 2007/3/28
*/
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MultiImpl extends UnicastRemoteObject implements Multi{
private String name;
public MultiImpl(String s) throws RemoteException{
super();
this.name=s;
}
public int mul(int a,int b) throws RemoteException{
System.out.println("lnvoke the multiply");
return (a*b);
}
}
/**
* @(#)CMSServer.java
*
*
* @author if
* @version 1.00 2007/3/28
*/
import java.rmi.*;
public class CMSServer {
public static void main(String[] args) {
//System.setSecurityManager(new RMISecurityManager());
try{
MultiImpl obj=new MultiImpl("MultiServer");
PlusImpl obj1=new PlusImpl("PlusServer");
Naming.rebind("rmi://localhost:1099/plusService",obj1);
Naming.rebind("rmi://localhost:1099/multiService",obj);
System.out.println("plusxx and multixx!!");
}catch(Exception e){
System.out.println(e.toString());
}
}
}
/**
* @(#)Frame1.java
*
*
* @author if
* @version 1.00 2007/3/28
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.rmi.*;
public class Frame1 extends JFrame implements ActionListener{
private JPanel nPanel;
private JTextField jtf1,jtf2,jtf3,jtf4;
private JButton jb;
public Frame1() {
setTitle("rmi");
initComponent();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
private void initComponent(){
nPanel=new JPanel();
jtf1=new JTextField(6);
jtf2=new JTextField(6);
jtf3=new JTextField(6);
jtf4=new JTextField(50);
nPanel.add(jtf1);
nPanel.add(jtf2);
nPanel.add(jtf3);
jb=new JButton("run");
jb.addActionListener(this);
getContentPane().add(nPanel,BorderLayout.NORTH);
getContentPane().add(jtf4,BorderLayout.CENTER);
getContentPane().add(jb,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
int i1=Integer.parseInt(jtf1.getText());
int i2=Integer.parseInt(jtf2.getText());
int i3=Integer.parseInt(jtf3.getText());
try{
Plus obj1=(Plus)Naming.lookup("rmi://localhost/plusService");
System.out.println("Found plus object");
Multi obj2=(Multi)Naming.lookup("rmi://localhost/multiService");
System.out.println("Found multi object");
int i4=obj1.add(i1,i2);
System.out.println("Add the first two number");
int i5=obj2.mul(i4,i3);
System.out.println("Multi with the last number");
jtf4.setText(""+i5);
System.out.println("ok");
}catch(Exception ex){
System.out.println(ex.toString());
}
}
}
/**
* @(#)CMSClient.java
*
*
* @author if
* @version 1.00 2007/3/28
*/
import javax.swing.JFrame;
public class CMSClient {
public static void main(String[] args) {
Frame1 frame=new Frame1();
frame.setVisible(true);
}
}