gpt4 book ai didi

java - for循环未收到完整数据

转载 作者:行者123 更新时间:2023-12-01 06:11:10 25 4
gpt4 key购买 nike

我遇到的问题是我的程序卡在 for 循环中,因为 dataInputSteam 在 DataOutputSteam 完成之前没有接收到所有数据。在我的程序中,我想向服务器发送屏幕截图,客户端应该收到它:服务器:

public sendScreen(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
Robot robot;
try {
robot = new Robot();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screen = new Rectangle( screenSize );
BufferedImage image = robot.createScreenCapture( screen );

BufferedImage scaledImage = Scalr.resize(image, 300);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] screenBytes = baos.toByteArray();


daos = new DataOutputStream(socket.getOutputStream());
daos.writeInt(screenBytes.length);
daos.write(screenBytes);
System.out.println("Screen sent");
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

客户:

public static class GetScreenshot implements Runnable{

Socket socket;
private static DataInputStream din;
private static BufferedImage screenshot;

public GetScreenshot(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
System.out.println("start method getScreenshot");
int length;
byte[] buffer;
PrintWriter out;

try {
//sending command to send screenshot
out = new PrintWriter(socket.getOutputStream(), true);
out.println("GETSCREENSHOT");

din = new DataInputStream(socket.getInputStream());
System.out.println("DIS created");
length = din.readInt();
System.out.println("Got data from DIS");
buffer = new byte[length];
System.out.println("Filled buffer");
for(int i = 0; i < length; i++){
buffer[i] = (byte) din.read();
System.out.println("read" + i+ "while length is " + length + " read data " + buffer);
}
System.out.println("got buffer");
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);

screenshot = ImageIO.read(bais);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//writing screenshot to local disk
File outputfile = new File("C:\\users\\XXXX\\documents\\image2.png");
try {
ImageIO.write(screenshot, "png", outputfile);
System.out.println("image written to local disk");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//set screenshot in the tool
Main.labelScreenshot.setIcon(new ImageIcon(screenshot));
}

}

有人知道如何传输屏幕截图的所有数据吗?

问候最大

最佳答案

我认为你的问题是因为你的 View ( Canvas /标签/等)宽度和高度小于你的图像。尝试调整屏幕截图的大小,使宽度和高度与您的 View ( Canvas /标签/等)相同。

关于java - for循环未收到完整数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34306989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com