凯哥学堂

搜索

凯哥学堂

客户端控制远程PC [复制链接]

查看: 7516|回复: 0
打印 上一主题 下一主题

72

主题

72

帖子

594

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
594
跳转到指定楼层
楼主
发表于 2017-8-24 22:30:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
package com.kaige123;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* 客户的控制远程的计算机
* @author 凯哥
*
*/
public class Console extends JFrame {

        private JTextField port;
        private JTextField ip;
        private JTextArea textArea;
        private JTextField textField;

        /**
         * Launch the application
         *
         * @param args
         */
        public static void main(String args[]) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        Console frame = new Console();
                                        frame.setVisible(true);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the frame
         */
        DatagramSocket socket = null;

        public Console() {
                super();
                try {
                        socket = new DatagramSocket();
                } catch (Exception e) {
                        // TODO: handle exception
                }
                setResizable(false);
                getContentPane().setLayout(null);
                setTitle("凯哥学堂远程控制PC 客户端");
                setBounds(100, 100, 353, 385);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JButton button = new JButton();
                button.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {

                                String path = javax.swing.JOptionPane.showInputDialog("请输入地址");
                                try {
                                        byte[] b = ("openIE        " + path).getBytes();
                                        DatagramPacket data = new DatagramPacket(b, b.length,
                                                        InetAddress.getByName(ip.getText()), Integer
                                                                        .parseInt(port.getText()));

                                        socket.send(data);
                                } catch (Exception e2) {
                                }

                        }
                });
                button.setText("打开浏览器");
                button.setBounds(10, 38, 106, 28);
                getContentPane().add(button);

                final JButton button_1 = new JButton();
                button_1.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {
                               
                                String path = javax.swing.JOptionPane.showInputDialog("请输入你要说的话");
                                try {
                                        byte[] b = ("openTXT        " + path).getBytes();
                                        DatagramPacket data = new DatagramPacket(b, b.length,
                                                        InetAddress.getByName(ip.getText()), Integer
                                                                        .parseInt(port.getText()));

                                        socket.send(data);
                                } catch (Exception e2) {
                                }
                        }
                });
                button_1.setText("打开记事本");
                button_1.setBounds(122, 38, 106, 28);
                getContentPane().add(button_1);

                final JButton button_2 = new JButton();
                button_2.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {
                       
                                try {
                                        byte[] b = ("openJSQ").getBytes();
                                        DatagramPacket data = new DatagramPacket(b, b.length,
                                                        InetAddress.getByName(ip.getText()), Integer
                                                                        .parseInt(port.getText()));

                                        socket.send(data);
                                } catch (Exception e2) {
                                }
                        }
                });
                button_2.setText("打开计算器");
                button_2.setBounds(234, 38, 106, 28);
                getContentPane().add(button_2);

                textField = new JTextField();
                textField.setBounds(10, 76, 255, 28);
                getContentPane().add(textField);

                final JButton button_3 = new JButton();
                button_3.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {
                               
                                String path = textField.getText();
                                try {
                                        byte[] b = ("DOS        " + path).getBytes();
                                        DatagramPacket data = new DatagramPacket(b, b.length,
                                                        InetAddress.getByName(ip.getText()), Integer
                                                                        .parseInt(port.getText()));

                                        socket.send(data);
                                       
                                        byte[] b1=new byte[1024*1024];
                                        DatagramPacket data1 = new DatagramPacket(b1, b1.length);
                                        socket.setSoTimeout(10000);
                                        socket.receive(data1);
                                       
                                        textArea.setText(new String(data1.getData(),0,data1.getLength()));
                                } catch (Exception e2) {
                                }
                        }
                });
                button_3.setText("执行");
                button_3.setBounds(272, 76, 68, 28);
                getContentPane().add(button_3);

                final JScrollPane scrollPane = new JScrollPane();
                scrollPane.setBounds(10, 110, 330, 219);
                getContentPane().add(scrollPane);

                textArea = new JTextArea();
                scrollPane.setViewportView(textArea);

                ip = new JTextField();
                ip.setText("192.168.2.102");
                ip.setBounds(10, 10, 234, 22);
                getContentPane().add(ip);

                port = new JTextField();
                port.setText("8889");
                port.setBounds(253, 10, 87, 22);
                getContentPane().add(port);
                //
        }

}
package com.kaige123.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* 接受命令的 UDP服务器 需要启动在被控制的电脑上
* @author 凯哥
*
*/

public class UDPServer extends Thread {

        private DatagramPacket data = null;

        // 用来接收数据包的
        public UDPServer(DatagramPacket data) {
                this.data = data;
        }

        // 用来执行命令
        public void run() {

                // openIE http://www.kaige123.com 打开浏览器
                // openTXT 你好呀!我推荐你看凯哥基础视频!http://www.kaige123.com 打开记事本
                // openJSQ
                // DOS del d:\a.xt

                String command = new String(data.getData(), 0, data.getLength());

                if (command.startsWith("openIE")) {
                        String ie_path = "\"C:\\Program Files\\Internet Explorer\\iexplore.exe\"";
                        String url = command.split("        ")[1];
                        try {
                                Runtime.getRuntime().exec(ie_path + " " + url);
                        } catch (Exception e) {
                                // TODO: handle exception
                        }
                } else if (command.startsWith("openTXT")) {
                        String txt_path = "\"c:\\windows\\system32\\notepad.exe\"";
                        String cs = command.split("        ")[1];
                        try {
                                // 把传递来文本 保存到临时的文件内 然后再打开那个文件就有了
                                File f = File.createTempFile("txt", "aaa");
                                FileOutputStream out = new FileOutputStream(f);
                                out.write(cs.getBytes());
                                out.close();

                                Runtime.getRuntime().exec(txt_path + " " + f.getPath());
                        } catch (Exception e) {
                        }

                } else if (command.startsWith("openJSQ")) {
                        String jsq_path = "\"c:\\windows\\system32\\calc.exe\"";
                        try {
                                Runtime.getRuntime().exec(jsq_path);
                        } catch (Exception e) {
                        }
                } else if (command.startsWith("DOS")) {

                        String c = command.split("        ")[1];
                        try {

                                Process p = Runtime.getRuntime().exec(c);
                                InputStream in = p.getInputStream();
                                BufferedReader br = new BufferedReader(
                                                new InputStreamReader(in));
                                String l = null;
                                String txt = "";
                                while ((l = br.readLine()) != null) {
                                        txt += l + "\n";
                                }
                                in.close();

                                // 根据来的地址 反馈信息回去
                                DatagramPacket data1 = new DatagramPacket(txt.getBytes(),
                                                txt.getBytes().length, data.getAddress(),
                                                data.getPort());

                                socket.send(data1);

                        } catch (Exception e) {
                                e.printStackTrace();
                        }

                }

        }

        static DatagramSocket socket = null;

        // 打开服务器 开始服务
        public static void openServer() throws Exception {

                // 开启服务器 准备监听 8889
                socket = new DatagramSocket(8889);

                while (true) {
                        // /准备数据包 准备一个空篮子 准备接受苹果
                        byte[] d = new byte[1024 * 100];
                        DatagramPacket data = new DatagramPacket(d, d.length);
                        socket.receive(data);// 这里就是实际的接收数据方法
                        new UDPServer(data).start();// 开始处理 开起线程进行处理
                }

        }

        public static void main(String[] args) throws Exception {
                openServer();

        }

}
回复

使用道具 举报