I want to send an image to the socket server, but it doesn't fully arrive
first I create a screenshot that should be sent via the socket to the server with this code:
try {
Robot robot = new Robot();
Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
File file = new File("original.png");
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
try {
ImageIO.write(bufferedImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
socket(robot.createScreenCapture(rectangle));
} catch (AWTException ex) {
System.err.println(ex);
}
void socket(BufferedImage bufferedImage) {
try {
Socket socket = new Socket("localhost", 20000);
OutputStream outputStream = socket.getOutputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println(byteArrayOutputStream.size());
try {
sleep(1200);
} catch (InterruptedException e) {
e.printStackTrace();
}
socket.close();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
the server should then save this with this code:
try {
ServerSocket serverSocket = new ServerSocket(20000);
System.out.println("-------------------------------------------------------");
while ( true ) {
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
handleConnection(clientSocket);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException e) {
}
}
}
}
} catch (UnknownHostException uhe) {
System.out.println(uhe);
} catch (IOException ioe) {
System.out.println(ioe);
}
static void handleConnection(Socket clientSocket) {
try {
OutputStream socketoutstr = clientSocket.getOutputStream();
OutputStreamWriter osr = new OutputStreamWriter(socketoutstr);
BufferedWriter bw = new BufferedWriter(osr);
InputStream socketinstr = clientSocket.getInputStream();
InputStreamReader isr = new InputStreamReader(socketinstr);
BufferedReader br = new BufferedReader(isr);
byte[] sizeAr = new byte[4];
socketinstr.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageAr = new byte[size];
socketinstr.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
ImageIO.write(image, "png", new File("send.png"));
System.out.println("111");
bw.write("");
bw.newLine();
bw.flush();
bw.close();
br.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
And the sent picture needed more space:
Does anyone know a solution how I can send the pictures without changing anything?
I see two issues
The format being written to the socket is jpg, not png. So comparing the size of the files will always be different.
The other is mentioned by @joachim-sauer - the read(byte[])
needs to be in a loop:
int read = 0;
while(read<size)
{
read += socketinstr.read(imageAr,read,size-read);
}