1public class Send {
2
3 public static void main(String[] args) throws Exception {
4 Socket socket = new Socket("localhost", 13085);
5 OutputStream outputStream = socket.getOutputStream();
6
7 BufferedImage image = ImageIO.read(new File("C:\\Users\\Jakub\\Pictures\\test.jpg"));
8
9 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
10 ImageIO.write(image, "jpg", byteArrayOutputStream);
11
12 byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
13 outputStream.write(size);
14 outputStream.write(byteArrayOutputStream.toByteArray());
15 outputStream.flush();
16 System.out.println("Flushed: " + System.currentTimeMillis());
17
18 Thread.sleep(120000);
19 System.out.println("Closing: " + System.currentTimeMillis());
20 socket.close();
21 }
22}
23
24
25public class Receive {
26
27 public static void main(String[] args) throws Exception {
28 ServerSocket serverSocket = new ServerSocket(13085);
29 Socket socket = serverSocket.accept();
30 InputStream inputStream = socket.getInputStream();
31
32 System.out.println("Reading: " + System.currentTimeMillis());
33
34 byte[] sizeAr = new byte[4];
35 inputStream.read(sizeAr);
36 int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
37
38 byte[] imageAr = new byte[size];
39 inputStream.read(imageAr);
40
41 BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
42
43 System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
44 ImageIO.write(image, "jpg", new File("C:\\Users\\Jakub\\Pictures\\test2.jpg"));
45
46 serverSocket.close();
47 }
48
49}
50