发信人: dmhorse()
整理人: zjxyz(2002-09-10 10:21:23), 站内信件
|
我写的一个RMI实险例子
Server.java基本接口
package MyRMI;
import java.rmi.*;
public interface Server extends Remote
{
String getString(String order) throws RemoteException;
String getFile(String filename) throws RemoteException;
}
ServerImpl.java服务端
package MyRMI;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.net.InetAddress;
import java.io.*;
public class ServerImpl extends UnicastRemoteObject implements Server
{
private final String localIP;
public ServerImpl(String ip) throws RemoteException
{
localIP = ip;
}
public String getString(String order) throws RemoteException
{
StringBuffer buf = null;
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec(order);
InputStream in = p.getInputStream();
int c;
buf = new StringBuffer();
while((c=in.read())!=-1)
{
buf.append((char)c);
}
}catch(Exception e)
{
e.printStackTrace();
}
return new String(buf);
}
public String getFile(String filename) throws RemoteException
{
return filename;
}
public static void main(String arg[])
{
if(System.getSecurityManager()!=null)
System.out.println("The security manager null.");
System.setSecurityManager(new RMISecurityManager());
try
{
String ip = InetAddress.getLocalHost().getHostAddress();
ServerImpl si = new ServerImpl(ip);
System.out.println("server ip"+ip);
Naming.rebind("//"+ip+"/ServerImpl",si);
System.out.println("The server has bind the ServerImpl object");
}
catch(Exception e)
{
e.printStackTrace();
}
}//end main
}//end class
Client.java客户端
package MyRMI;
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
public class Client
{
//The Control windows init.
private Frame frame;
private TextField inputField;
private TextArea ta;
private Button cmd,cmdClose,cmdOpen,cmdClosePop3,cmdOpenPop3;
//Server is the interface implements RemoteObject in the bind server
static Server s;
final String CLOSEMYSQL = "killall -9 mysqld";
final String OPENMYSQL = "safe_mysqld &";
final String OPENPOP3 = "qmailctl startPop3d";
final String CLOSEPOP3 = "qmailctl stopPop3d";
/**
* When press the button <code>enter</code>
* Send and execute the command
*/
private class cmdHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
runCmd();
}
}
/**
* When the forcus on the textfield and the
* user press the key <code>enter</code>
* Send and execute the command
*/
private class inputFieldHandler implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
runCmd();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
/**
* When press the cmdClose button,
* Close the mysql on the RMI server.
*/
private class cmdCloseHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
inputField.setText(CLOSEMYSQL);
runCmd();
}
}
/**
* When press the cmdOpen button,
* Open the mysql on the RMI server
*/
private class cmdOpenHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
inputField.setText(OPENMYSQL);
runCmd();
}
}
/**
* When press the cmdClosePop3 button
* Close the qmail pop3 on the RMI server
*/
private class cmdClosePop3Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
inputField.setText(CLOSEPOP3);
runCmd();
}
}
/**
* When press the cmdOpenPop3 button.
* Open the qmail pop3 on the RMI server
*/
private class cmdOpenPop3Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
inputField.setText(OPENPOP3);
runCmd();
}
}
/**
* When close the window,system exit.
*/
private class frameHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
/**
* Send a command to the bind server
* After the server execute,return
* the string display the textarea.
*/
private void runCmd()
{
String cmdStr = inputField.getText();
String output = "";
ta.setText("cmd running");
try
{
output = s.getString(cmdStr);
}
catch(Exception e)
{
e.printStackTrace();
ta.setText(e.toString());
}
ta.setText(output);
}
/**
* Init the frame.
*/
private void initFrame()
{
frame = new Frame("Remote Control");
frame.setBounds(300,300,500,500);
inputField = new TextField("",30);
inputField.addKeyListener(new inputFieldHandler());
ta = new TextArea();
cmd = new Button("Enter");
cmd.addActionListener(new cmdHandler());
cmdClose = new Button("Close MySql");
cmdClose.addActionListener(new cmdCloseHandler());
cmdOpen = new Button("Open MySql");
cmdOpen.addActionListener(new cmdOpenHandler());
cmdClosePop3 = new Button("Close Pop3");
cmdClosePop3.addActionListener(new cmdClosePop3Handler());
cmdOpenPop3 = new Button("Open Pop3");
cmdOpenPop3.addActionListener(new cmdOpenPop3Handler());
frame.setLayout(new BorderLayout());
//Use Panel to store the button and txtfiled
Panel p = new Panel();
p.add(inputField);
p.add(cmd);
//Use Panel to store mysql button
Panel pButton = new Panel();
pButton.setLayout(new GridLayout(10,1));
pButton.add(cmdOpen);
pButton.add(cmdClose);
pButton.add(cmdOpenPop3);
pButton.add(cmdClosePop3);
frame.add(ta,BorderLayout.CENTER);
frame.add(p,BorderLayout.SOUTH);
frame.add(pButton,BorderLayout.EAST);
frame.addWindowListener(new frameHandler());
frame.setVisible(true);
}
/**
* Constructor to init the frame.
*/
Client()
{
initFrame();
}
public static void main(String arg[])
{
Client c = new Client();
String ip = "192.168.1.227";
try
{
System.out.println("The server ip is " + ip );
s = (Server)Naming.lookup("//" + ip + "/ServerImpl");
//System.out.println(s.getString("ls"));
}
catch(Exception e)
{
e.printStackTrace();
c.ta.setText(e.toString());
}
}//end main
}//end class
policy.txt权限文件,比较土的权限,是所有权限
grant{
permission java.security.AllPermission;
};
首先要在服务端运行
prompt>miregistry &
prompt>rmic MyRMI.ServerImpl
prompt>cp ServerImpl_*.class ./MyRMI/
prompt>java –Djava.security.policy=policy.txt MyRMI/ServerImpl
在客户端要将服务端的Server.class ServerImpl_Stub.class放入package MyRMI中
prompt>java MyRMI/Client
运行后可以输入一些常用的linux命令,如ls等,加上绝对路径也可实现添加用户等功能
---- I miss u 冬
我无自由,我失自由,痛啊苦啊眼泪流,每日茶送饭,罗我命,咸豆青菜
每日一次diablo,晚上唔使四围蒲
|
|