我以前遇到过这个问题,但我不记得我是如何解决的。我正在研究 RAT,到目前为止它只是将客户端屏幕发送到服务器。我曾经将图像保存到磁盘,但现在我试图让它只显示在 JFrame 上。它显示第一个屏幕截图,但仅显示第一个屏幕截图,它不会绘制新的屏幕截图。
服务器:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Server extends JFrame {
private BufferedImage image;
private BufferedImage oldImage;
public Server() {
setTitle("RAT");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws IOException {
try {
Server server = new Server();
ServerSocket serverSocket = new ServerSocket(25565);
Socket clientSocket = serverSocket.accept();
System.out.println("Connection accepted!");
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
boolean firstConnection = true;
int width = 0;
int height = 0;
while (true) {
if(firstConnection) {
width = in.readShort();
height = in.readShort();
server.setSize(width, height);
server.setVisible(true);
firstConnection = false;
}
server.oldImage = ImageIO.read(in);
if(server.oldImage != null) {
server.image = server.oldImage;
}
server.repaint();
}
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}
客户:
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.imageio.ImageIO;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 25565);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
Robot robot = new Robot();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle rectangle = new Rectangle(toolkit.getScreenSize());
out.writeShort(toolkit.getScreenSize().width);
out.writeShort(toolkit.getScreenSize().height);
while(true) {
ImageIO.write(robot.createScreenCapture(rectangle), "jpg", out);
Thread.sleep(200);
}
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}