打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java局域网远程控制
package org.fw.qq.plugins.remoteassistance;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.io.Serializable;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;public class ControlCarrier implements Serializable {  private String type;  private int mouseX = -1;  private int mouseY = -1;  private int wheelAmt = -1;  private int mousePressBtn = -1;  private int mouseReleaseBtn = -1;  private List<Integer> keyPressCode = new ArrayList<Integer>();  private List<Integer> keyReleaseCode = new ArrayList<Integer>();  private byte[] desktopImg = null;  public byte[] getDesktopImg() {    return desktopImg;  }  public void setDesktopImg(byte[] desktopImg) {    this.desktopImg = desktopImg;  }  public int getMousePressBtn() {    return mousePressBtn;  }  public void setMousePressBtn(int mousePressBtn) {    this.mousePressBtn = mousePressBtn;  }  public int getMouseReleaseBtn() {    return mouseReleaseBtn;  }  public void setMouseReleaseBtn(int mouseReleaseBtn) {    this.mouseReleaseBtn = mouseReleaseBtn;  }  public int getMouseX() {    return mouseX;  }  public void setMouseX(int mouseX) {    this.mouseX = mouseX;  }  public int getMouseY() {    return mouseY;  }  public void setMouseY(int mouseY) {    this.mouseY = mouseY;  }  public int getWheelAmt() {    return wheelAmt;  }  public void setWheelAmt(int wheelAmt) {    this.wheelAmt = wheelAmt;  }  /**   *    */  private static final long serialVersionUID = -3074156105274743383L;  public List<Integer> getKeyPressCode() {    return keyPressCode;  }  public void setKeyPressCode(List<Integer> keyPressCode) {    this.keyPressCode = keyPressCode;  }  public List<Integer> getKeyReleaseCode() {    return keyReleaseCode;  }  public void setKeyReleaseCode(List<Integer> keyReleaseCode) {    this.keyReleaseCode = keyReleaseCode;  }  @Override  public String toString() {    StringBuilder builder = new StringBuilder();    PropertyDescriptor[] pd = null;    try {      pd = Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors();      for (PropertyDescriptor propertyDescriptor : pd) {        Method method = propertyDescriptor.getReadMethod();        builder.append("\n ");        builder.append(propertyDescriptor.getName());        builder.append(" : ");        builder.append(method.invoke(this));      }    } catch (Exception e) {      e.printStackTrace();    }    return builder.toString();  }  public String getType() {    return type;  }  public void setType(String type) {    this.type = type;  }}
package org.fw.qq.plugins.remoteassistance;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;public class Client extends JPanel implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener {  /**   *    */  private static final long serialVersionUID = -806686211556049511L;  private String controlIp;  private int controlPort;  private BufferedImage controlDesktop;  public Client(String controlIp, int controlPort) {    this.controlIp = controlIp;    this.controlPort = controlPort;    setFocusable(true);    this.addMouseListener(this);    this.addMouseMotionListener(this);    this.addMouseWheelListener(this);    this.addKeyListener(this);  }  public void start() {    ControlCarrier command = new ControlCarrier();    sendControlCommand(command);  }  public void paintComponent(Graphics g) {    if (controlDesktop != null) {      g.drawImage(controlDesktop, 0, 0, this);    }  }  private void sendControlCommand(ControlCarrier command) {    ObjectOutputStream objectOut = null;    ObjectInputStream objectInput = null;    try {      Socket commandSocket = new Socket(InetAddress.getByName(controlIp), controlPort);      objectOut = new ObjectOutputStream(new BufferedOutputStream(commandSocket.getOutputStream()));      objectOut.writeObject(command);      objectOut.flush();      objectInput = new ObjectInputStream(new BufferedInputStream(commandSocket.getInputStream()));      ControlCarrier desktopState = (ControlCarrier) objectInput.readObject();      displayDesktopState(desktopState);    } catch (UnknownHostException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    } catch (ClassNotFoundException e) {      e.printStackTrace();    } finally {      if (objectOut != null) {        try {          objectOut.close();        } catch (IOException e) {          e.printStackTrace();        }      }      if (objectInput != null) {        try {          objectInput.close();        } catch (IOException e) {          e.printStackTrace();        }      }    }  }  private void displayDesktopState(ControlCarrier desktopState) {    ByteArrayInputStream bin = new ByteArrayInputStream(desktopState.getDesktopImg());    try {      controlDesktop = ImageIO.read(bin);    } catch (IOException e) {      e.printStackTrace();      JOptionPane.showMessageDialog(null, "被控制端已断开连接,或网络异常!");    }    repaint();  }  public void mouseClicked(MouseEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("mouseClicked");    if (e.getClickCount() == 1) {      if (e.getButton() == MouseEvent.BUTTON1) {        command.setMousePressBtn(InputEvent.BUTTON1_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON1_MASK);      } else if (e.getButton() == MouseEvent.BUTTON2) {        command.setMousePressBtn(InputEvent.BUTTON2_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON2_MASK);      } else if (e.getButton() == MouseEvent.BUTTON3) {        command.setMousePressBtn(InputEvent.BUTTON3_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON3_MASK);      }    } else if (e.getClickCount() == 2) {      if (e.getButton() == MouseEvent.BUTTON1) {        command.setMousePressBtn(InputEvent.BUTTON1_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON1_MASK);        command.setMousePressBtn(InputEvent.BUTTON1_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON1_MASK);      } else if (e.getButton() == MouseEvent.BUTTON2) {        command.setMousePressBtn(InputEvent.BUTTON2_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON2_MASK);        command.setMousePressBtn(InputEvent.BUTTON2_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON2_MASK);      } else if (e.getButton() == MouseEvent.BUTTON3) {        command.setMousePressBtn(InputEvent.BUTTON3_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON3_MASK);        command.setMousePressBtn(InputEvent.BUTTON3_MASK);        command.setMouseReleaseBtn(InputEvent.BUTTON3_MASK);      }    }    sendControlCommand(command);  }  public void mouseEntered(MouseEvent e) {  }  public void mouseExited(MouseEvent e) {  }  public void mousePressed(MouseEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("mousePressed");    if (e.getButton() == MouseEvent.BUTTON1) {      command.setMousePressBtn(InputEvent.BUTTON1_MASK);    } else if (e.getButton() == MouseEvent.BUTTON2) {      command.setMousePressBtn(InputEvent.BUTTON2_MASK);    } else if (e.getButton() == MouseEvent.BUTTON3) {      command.setMousePressBtn(InputEvent.BUTTON3_MASK);    }    sendControlCommand(command);  }  public void mouseReleased(MouseEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("mouseReleased");    if (e.getButton() == MouseEvent.BUTTON1) {      command.setMouseReleaseBtn(InputEvent.BUTTON1_MASK);    } else if (e.getButton() == MouseEvent.BUTTON2) {      command.setMouseReleaseBtn(InputEvent.BUTTON2_MASK);    } else if (e.getButton() == MouseEvent.BUTTON3) {      command.setMouseReleaseBtn(InputEvent.BUTTON3_MASK);    }    sendControlCommand(command);  }  public void mouseDragged(MouseEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("mouseDragged");    command.setMousePressBtn(e.getButton());    command.setMouseX(e.getX());    command.setMouseY(e.getY());    sendControlCommand(command);  }  public void mouseMoved(MouseEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("mouseMoved");    command.setMouseX(e.getX());    command.setMouseY(e.getY());    sendControlCommand(command);  }  public void mouseWheelMoved(MouseWheelEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("mouseWheelMoved");    command.setWheelAmt(e.getWheelRotation());    sendControlCommand(command);  }  public void keyPressed(KeyEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("keyPressed");    List<Integer> keyPress = new ArrayList<Integer>();    if (e.isControlDown()) {      keyPress.add(KeyEvent.VK_CONTROL);    }    if (e.isAltDown()) {      keyPress.add(KeyEvent.VK_ALT);    }    if (e.isShiftDown()) {      keyPress.add(KeyEvent.VK_SHIFT);    }    keyPress.add(e.getKeyCode());    command.setKeyPressCode(keyPress);    sendControlCommand(command);  }  public void keyReleased(KeyEvent e) {    ControlCarrier command = new ControlCarrier();    command.setType("keyReleased");    List<Integer> keyRelease = new ArrayList<Integer>();    keyRelease.add(e.getKeyCode());    command.setKeyReleaseCode(keyRelease);    sendControlCommand(command);  }  public void keyTyped(KeyEvent e) {  }  public Dimension getPreferredSize() {    return new Dimension(getWidth(), getHeight());  }  public static void main(String[] args) {    JFrame frame = new JFrame("远程控制");    String ip = JOptionPane.showInputDialog("请输入远程连接IP");    if ("null".equals(ip)) {      System.exit(0);    }    while (ip.isEmpty()) {      ip = JOptionPane.showInputDialog("请输入远程连接IP");    }    while (!StringUtil.isValidIP(ip)) {      ip = JOptionPane.showInputDialog("请输入合法IP");    }    String port = JOptionPane.showInputDialog("输入远程连接端口");    if ("null".equals(port)) {      System.exit(0);    }    while (port.isEmpty()) {      port = JOptionPane.showInputDialog("请输入远程连接端口");    }    while (!StringUtil.isValidPort(port)) {      port = JOptionPane.showInputDialog("请输入合法端口");    }    JOptionPane.showMessageDialog(null, "开始远程连接!");    Client client = new Client(ip, Integer.parseInt(port));    JScrollPane jsp = new JScrollPane(client, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    frame.add(jsp);    frame.setDefaultCloseOperation(3);    frame.setSize(800, 600);    frame.setVisible(true);  }}
package org.fw.qq.plugins.remoteassistance;import java.awt.AWTException;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;public class Server extends JPanel implements Runnable {  /**   *    */  private static final long serialVersionUID = -927388268343256207L;  private ServerSocket server;  private Thread thread;  private Robot controlMouseRobot;  private JButton releaseConnect;  private JLabel label;  public Server(String ip, int port) throws IOException, AWTException {    server = new ServerSocket(port, 1, InetAddress.getByName(ip));    thread = new Thread(this);    controlMouseRobot = new Robot();    label = new JLabel("监听" + ip + ":" + port);        releaseConnect = new JButton("断开连接");    this.add(releaseConnect);    releaseConnect.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        stop();      }    });    this.add(label);    this.add(releaseConnect);  }  public void start() {    thread.start();  }  @SuppressWarnings("deprecation")  public void stop() {    label.setText("已断开连接");    thread.stop();    try {      server.close();    } catch (IOException e) {      e.printStackTrace();    }  }  public void run() {    while (true) {      ObjectInputStream request = null;      ObjectOutputStream response = null;      try {        Socket client = server.accept();        label.setText("被控制中");        request = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));        ControlCarrier carrier = (ControlCarrier) request.readObject();        System.out.println("收到命令:" + carrier);        if (carrier.getMouseX() != -1 && carrier.getMouseY() != -1) {          controlMouseRobot.mouseMove(carrier.getMouseX(), carrier.getMouseY());        }        if (carrier.getMousePressBtn() != -1) {          controlMouseRobot.mousePress(carrier.getMousePressBtn());        }        if (carrier.getMouseReleaseBtn() != -1) {          controlMouseRobot.mouseRelease(carrier.getMouseReleaseBtn());        }        if (carrier.getWheelAmt() != -1) {          controlMouseRobot.mouseWheel(carrier.getWheelAmt());        }        for (Integer pressKey : carrier.getKeyPressCode()) {          controlMouseRobot.keyPress(pressKey);        }        for (Integer releaseKey : carrier.getKeyReleaseCode()) {          controlMouseRobot.keyRelease(releaseKey);        }        Dimension desktopSize = Toolkit.getDefaultToolkit().getScreenSize();        BufferedImage curDesktop = controlMouseRobot.createScreenCapture(new Rectangle(desktopSize));        ByteArrayOutputStream out = new ByteArrayOutputStream();        ImageIO.write(curDesktop, "jpg", out);        ControlCarrier desktopState = new ControlCarrier();        desktopState.setDesktopImg(out.toByteArray());        response = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));        response.writeObject(desktopState);        response.flush();      } catch (IOException e) {        e.printStackTrace();      } catch (ClassNotFoundException e) {        e.printStackTrace();      } finally {        if (request != null) {          try {            request.close();          } catch (IOException e) {            e.printStackTrace();          }        }        if (response != null) {          try {            response.close();          } catch (IOException e) {            e.printStackTrace();          }        }      }    }  }  public static void main(String[] args) {    Server server;    try {      String ip = JOptionPane.showInputDialog("请输入本地IP");      if ("null".equals(ip)) {        System.exit(0);      }      while (ip.isEmpty()) {        ip = JOptionPane.showInputDialog("请输入本地IP");      }      while (!StringUtil.isValidIP(ip)) {        ip = JOptionPane.showInputDialog("请输入合法IP");      }      String port = JOptionPane.showInputDialog("请输入本地端口");      if ("null".equals(port)) {        System.exit(0);      }      while (port.isEmpty()) {        port = JOptionPane.showInputDialog("请输入本地端口");      }      while (!StringUtil.isValidPort(port)) {        port = JOptionPane.showInputDialog("请输入合法端口");      }      while (!StringUtil.isPortUsed(Integer.parseInt(port))) {        port = JOptionPane.showInputDialog("端口被占用,请输入其他端口");      }      server = new Server(ip, Integer.parseInt(port));      server.start();            JFrame frame = new JFrame("远程连接被控制端");      frame.add(server);      frame.setSize(300,85);      frame.setDefaultCloseOperation(3);      frame.setVisible(true);    } catch (IOException e) {      e.printStackTrace();    } catch (AWTException e) {      e.printStackTrace();    }  }}
package org.fw.qq.plugins.remoteassistance.demo;import java.awt.AWTException;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import org.fw.qq.plugins.remoteassistance.Client;import org.fw.qq.plugins.remoteassistance.Server;import org.fw.qq.plugins.remoteassistance.StringUtil;public class RemoteAssistance extends JFrame {  /**   *    */  private static final long serialVersionUID = -502914245090231502L;  private JRadioButton serverJRB;  private JRadioButton clientJRB;  private ButtonGroup btnGroup;  private JLabel infoJL;  private JButton okBtn;  public RemoteAssistance() {    initComponent();    registerListeners();  }  private void registerListeners() {    okBtn.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        if (serverJRB.isSelected()) {          createServer();        } else if (clientJRB.isSelected()) {          createClient();        }      }      private void createClient() {        JFrame frame = new JFrame("远程控制");        String ip = JOptionPane.showInputDialog("请输入远程连接IP");        if ("null".equals(ip)) {          System.exit(0);        }        while (ip.isEmpty()) {          ip = JOptionPane.showInputDialog("请输入远程连接IP");        }        while (!StringUtil.isValidIP(ip)) {          ip = JOptionPane.showInputDialog("请输入合法IP");        }        String port = JOptionPane.showInputDialog("输入远程连接端口");        if ("null".equals(port)) {          System.exit(0);        }        while (port.isEmpty()) {          port = JOptionPane.showInputDialog("请输入远程连接端口");        }        while (!StringUtil.isValidPort(port)) {          port = JOptionPane.showInputDialog("请输入合法端口");        }        JOptionPane.showMessageDialog(null, "开始远程连接!");        Client client = new Client(ip, Integer.parseInt(port));        JScrollPane jsp = new JScrollPane(client, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);        frame.add(jsp);        frame.setDefaultCloseOperation(3);        frame.setSize(800, 600);        frame.setVisible(true);      }      private void createServer() {        Server server;        try {          String ip = JOptionPane.showInputDialog("请输入本地IP");          if ("null".equals(ip)) {            System.exit(0);          }          while (ip.isEmpty()) {            ip = JOptionPane.showInputDialog("请输入本地IP");          }          while (!StringUtil.isValidIP(ip)) {            ip = JOptionPane.showInputDialog("请输入合法IP");          }          String port = JOptionPane.showInputDialog("请输入本地端口");          if ("null".equals(port)) {            System.exit(0);          }          while (port.isEmpty()) {            port = JOptionPane.showInputDialog("请输入本地端口");          }          while (!StringUtil.isValidPort(port)) {            port = JOptionPane.showInputDialog("请输入合法端口");          }          while (!StringUtil.isPortUsed(Integer.parseInt(port))) {            port = JOptionPane.showInputDialog("端口被占用,请输入其他端口");          }          server = new Server(ip, Integer.parseInt(port));          server.start();          JFrame frame = new JFrame("远程连接被控制端");          frame.add(server);          frame.setSize(300, 85);          frame.setDefaultCloseOperation(3);          frame.setVisible(true);        } catch (IOException e) {          e.printStackTrace();        } catch (AWTException e) {          e.printStackTrace();        }      }    });  }  private void initComponent() {    infoJL = new JLabel("请选择");    btnGroup = new ButtonGroup();    serverJRB = new JRadioButton("被控制者");    serverJRB.setSelected(true);    clientJRB = new JRadioButton("控制端");    btnGroup.add(serverJRB);    btnGroup.add(clientJRB);    okBtn = new JButton("确定");    setLayout(new FlowLayout());    this.add(infoJL);    this.add(serverJRB);    this.add(clientJRB);    this.add(okBtn);  }  public static void main(String[] args) {    RemoteAssistance demo = new RemoteAssistance();    demo.setTitle("远程协助");    demo.setDefaultCloseOperation(3);    demo.setSize(320, 240);    demo.setVisible(true);  }}
package org.fw.qq.plugins.remoteassistance;import java.net.DatagramSocket;import java.net.SocketException;import java.util.regex.Matcher;import java.util.regex.Pattern;public class StringUtil {  /*   * 检测端口是否被用   */  public static boolean isPortUsed(int port) {    try {      DatagramSocket socket = new DatagramSocket(port);      socket.close();      return true;    } catch (SocketException e) {      return false;    }  }  public static boolean isValidIP(String ipAddress) {    String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";    Pattern pattern = Pattern.compile(ip);    Matcher matcher = pattern.matcher(ipAddress);    return matcher.matches();  }  public static boolean isValidPort(String port) {    try {      int temp = Integer.parseInt(port);      if (temp > 0 && temp < 65535)        return true;    } catch (Exception e) {      return false;    }    return false;  }}

 效果图:



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java右键菜单实现文本组件内容的的复制、粘贴、剪切功能
编程语言java常见事件响应方法实例汇总
java实现记事本
鼠标控制小球
《Java语言编程基础立体化实用教程》2-4 应用程序主窗体的实现
Swing文件下载
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服