java 远程监控

//Host.java

 

import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.Provider.Service;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Host extends JFrame{
     JPanel mainPanel ;
     JButton startButton ;
     JButton stopButton ;
     JTextArea msgText ;
     JLabel stateLabel ;
     JScrollPane scrollPane ;
    
     ServerSocket server;
     Socket socket;
     public Host(){
         //----------初始化组件-----------
        startButton = new JButton( "启动服务器" ) ;
        startButton.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {

    startServer();
   }
  });
       
         stopButton = new JButton( "停止服务器" ) ;
         stopButton.setEnabled(false);
         stopButton.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {

    stopServer();
   }
  });
         mainPanel = new JPanel();
         stateLabel = new JLabel( "未监控" ) ;
         msgText = new JTextArea() ;
         scrollPane = new JScrollPane( msgText ) ;
        
         //-------------设置组件属性----------
        mainPanel.setLayout( null );
         startButton.setBounds( 10 , 10 , 100 , 30 ) ;
         stopButton.setBounds( 120 , 10 , 100 , 30 );
         stateLabel.setBounds( 230 , 10 , 100 , 30 );
         scrollPane.setBounds( 10 , 50 , 350 , 320 );
         mainPanel.add( stopButton ) ;
         mainPanel.add( startButton ) ;
         mainPanel.add( stateLabel ) ;
         mainPanel.add( scrollPane ) ;
         //----------初始化窗体属性----------
        this.add( mainPanel ) ;
         this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
         this.setBounds( 800,0,370 , 400 );
         this.setVisible( true );
     }
    

 /***
      * 启动服务器
      */
     protected void startServer() {
      setMessage("服务器启动中。。。。");
      try{
       server=new ServerSocket(9999);
       new Thread(new Runnable() {
    
    @Override
    public void run() {

    service(); 
    }
   }).start();
       startButton.setEnabled(false);
       stopButton.setEnabled(true);
      
//       startButton.setEnabled(false);
//       stopButton.setEnabled(true);
////       Socket socket=server.accept();
////       InputStream in=socket.getInputStream();
////       int command=in.read();
////       if(command==1){
////        System.out.println("正在关机...");
//////        Runtime runtime =Runtime.getRuntime();
//////        runtime.exec("cmd /c shutdown -s -t 0");
////       }
      
      }catch(Exception e){
       e.printStackTrace();
       setMessage("程序出错:"+e.getMessage());
      }
     
     
 }


 /***
      * 为用户提供服务
      */
     protected void service() {
      setMessage("开始为用户提供服务....");
      try{
       socket=server.accept();
       setMessage("来了一个控制者");
       stateLabel.setText("状态:被监控");
       InputStream in=socket.getInputStream();
       OutputStream out=socket.getOutputStream();
       while(socket!=null){
        int command=in.read();
        if(command== -1){
         setMessage("控制者离开了。。。");
         break;
        }else if(command==1){
        
//         Runtime runtime= Runtime.getRuntime();
//         runtime.exec("cmd /c shutdown -s -t 0");
         setMessage("这个禽兽要关你的电脑。。。");
        }else if(command==2){
         setMessage("这个禽兽要锁定你的鼠标");
         new Thread(new Runnable() {
      
      @Override
      public void run() {

       try{
        while(true){
         Robot robot=new Robot();
         robot.mouseMove(0, 0);
         Thread.sleep(5000);
        }
       }catch(Exception e){
        setMessage("程序出错"+e.getMessage());
       }
      }
     }).start();
        } else if(command==3){
         setMessage("这个禽兽要累死你的CPU和硬盘");
         new Thread(new Runnable() {
      
      @Override
      public void run() {

       while(true){
        
       }
      }
     }).start();
         new Thread(new Runnable() {
      
      @Override
      public void run() {

       try{
        FileInputStream in=new FileInputStream("c:/a.txt");
        while(true){
         FileOutputStream out=new FileOutputStream("c:/b.txt");
        int a=in.read();
        out.write(a);
        out.close();
        }
        
       }catch(Exception e){
        setMessage("程序出错:"+e.getMessage());
       }
      }
     }).start();
           
           }
       }
       stopService();
      
      }catch(Exception e){
       e.printStackTrace();
      }

     
 }

/***
 * 停止为用户提供服务
 */
 private void stopService() {

  try{
   socket.close();
  }catch(Exception e){
   
  }
  socket=null;
  stopServer();
  setMessage("终止服务。。。");
 }


 /***
      * 停止服务器
      */
     protected void stopServer() {

      try{
       server.close();
      
      
      }catch(Exception e){
       e.printStackTrace();
      }
      server=null;
      startButton.setEnabled(true);
      stopButton.setEnabled(false);
      stateLabel.setText("状态:未监控");
      setMessage("服务器停止运行。。。");
 
     }
     /***
      * 输入消息的文本框
      * @param msg
      */
     private void setMessage(String msg) {

      msg=msgText.getText()+msg+"    "+new Date().toLocaleString()+"\r\n";
      msgText.setText(msg);
     }
 public static void main(String args[]){
         new Host() ;
     }
    
}

//Control.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;


public class Control extends JFrame{

     JPanel mainPanel ;
     JTextField ipText ;
     JButton connButton ;
     JButton disConnButton ;
     JButton closeButton ;
     JButton clockButton;
     JButton workToDieButton;
     JComboBox  killPathBox;
     Socket socket;
    
     Object s[]={"进程1","进程2","进程3","进程4","进程5","进程6","进程7","进程8"};
     public Control(){
         //-----------初始化组件---------
        mainPanel = new JPanel() ;
         ipText = new JTextField() ;
         connButton = new JButton( "连接" ) ;
         connButton.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {

    connectServer();
   }
  });
        
         disConnButton = new JButton( "断开" ) ;
         disConnButton.setEnabled(false);
         disConnButton.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {

    disConnServer();
   }
  });
      
        
         closeButton = new JButton( "关机" ) ;
         closeButton.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {

    closeComputer();
   }
  });
        
          clockButton = new JButton("锁定");
          clockButton.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {

    clockMouse();
   }
  });
          workToDieButton=new JButton("工作到死");
          workToDieButton.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {

    workToDie();
   }
  });
          killPathBox=new JComboBox(s);
         
          killPathBox.addPopupMenuListener(new PopupMenuListener() {
   
   @Override
   public void popupMenuWillBecomeVisible(PopupMenuEvent e) {

    System.out.println("sasadsas");
    killPath();
    
   }
   
   @Override
   public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void popupMenuCanceled(PopupMenuEvent e) {
    // TODO Auto-generated method stub
    
   }
  });
         //------------设置组件-------------
         mainPanel.setLayout( null );
         ipText.setBounds( 10 , 10 , 150 , 30 ) ;
         connButton.setBounds( 170 , 10 , 70 , 30 );
         disConnButton.setBounds( 250 , 10 , 70 , 30 );
         closeButton.setBounds( 330 , 10 , 70 , 30 );
         clockButton.setBounds(410,10,70,30);
         workToDieButton.setBounds(490,10,70,30);
         killPathBox.setBounds(600, 10, 70, 30);
         mainPanel.add( ipText ) ;
         mainPanel.add( connButton ) ;
         mainPanel.add( disConnButton ) ;
         mainPanel.add( closeButton ) ;
         mainPanel.add(clockButton);
         mainPanel.add(workToDieButton);
         mainPanel.add(killPathBox);
         //----------初始化窗体属性----------
        this.add( mainPanel ) ;
         this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
         this.setSize( 800 , 500 );
         this.setVisible( true );
     }
 /***
      * 连接目标主机
      */
     protected void connectServer() {

      try{
      
       socket=new Socket(ipText.getText(),9999);
       connButton.setEnabled(false);
       disConnButton.setEnabled(true);
      
      
//       InputStream in=socket.getInputStream();
//       OutputStream out =socket.getOutputStream();
//      
//       //1表示关闭电脑
//       out.write(1);
//       out.flush();
      
      }catch(Exception e){
       e.printStackTrace();
      }
     
 }
     /***
      * 断开与目标主机的连接
      */
     protected void disConnServer() {

      try{
      
       socket.close();
      
      }catch(Exception e){
       e.printStackTrace();
      }
      socket=null;
      connButton.setEnabled(true);
      disConnButton.setEnabled(false);
     }
     /***
      * 关闭远程计算机
      */
     protected void closeComputer() {
      if(socket!=null){
       try{
        OutputStream out=socket.getOutputStream();
        out.write(1);
        out.flush();
       }catch(Exception e){
        e.printStackTrace();
       }
      }
     
     
     }
     /***
      * 锁定鼠标
      */
     protected void clockMouse() {

      if(socket!=null){
       OutputStream out;
   try {
    out = socket.getOutputStream();
    out.write(2);
    out.flush();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
      }
     }
     protected void workToDie() {

      if(socket!=null){
       try{
        OutputStream outputStream=socket.getOutputStream();
        outputStream.write(3);
        outputStream.flush();
       }catch(Exception e){
        e.printStackTrace();
       }
      }
     }
     /***
      * 结束用户的进程
      */
     protected void killPath() {

      if(socket!=null){
       try{
        OutputStream outputStream=socket.getOutputStream();
        outputStream.write(4);
        outputStream.flush();
       }catch(Exception e){
        e.printStackTrace();
       }
      }
     
     }
 public static void main(String args[]){
         new Control() ;
     }
    
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值