实现本地电脑监控服务器端电脑监控功能
public class Client {
// 入口
public static void main(String[] args) {
try {
int choice = JOptionPane.showConfirmDialog(null, "请求控制对方电脑", "远程控制系统-Charles", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.NO_OPTION) {
return;
}
String input = JOptionPane.showInputDialog("请输入要连接电脑的ip(包括端口号)", "127.0.0.1:10000");
// 获取服务器的主机
String host = input.substring(0, input.indexOf(":"));
// 获取服务器的端口号
String post = input.substring(input.indexOf(":") + 1);
System.out.println("服务器的主机:" + host + " " + "端口号:" + post);
Socket client = new Socket(host,Integer.parseInt(post));
DataInputStream dis = new DataInputStream(client.getInputStream());
JFrame jframe = new JFrame("本地监控系统 - Charles");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭进程
jframe.setSize(1024, 768);// 设置窗体大小
double height = dis.readDouble();
double width = dis.readDouble();
Dimension ds = new Dimension((int)width, (int)height);
//设置
jframe.setSize(ds);
//将服务器图片作为背景
JLabel backImage = new JLabel();
JPanel panel = new JPanel();
//设置滚动条
JScrollPane scrollPane = new JScrollPane(panel);
panel.setLayout(new FlowLayout());
panel.add(backImage);
jframe.add(scrollPane);
jframe.setAlwaysOnTop(true);
jframe.setVisible(true);
while(true){
int len = dis.readInt();
byte[] imageData = new byte[len];
dis.readFully(imageData);
ImageIcon image = new ImageIcon(imageData);
backImage.setIcon(image);
jframe.repaint();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
1