How to send images via sockets in java?

I am writing a client-server program and I want to send an image. The code is as follows:

//RECEIVER while(true){ try{ socket = server.accept(); out = new ObjectOutputStream(socket.getOutputStream()); out.flush(); in = new ObjectInputStream(socket.getInputStream()); System.out.println("Connected to "+PORTA+"."); while(!socket.isClosed()){ System.out.println("\nPrint the action"); azione = reader.readLine(); if(azione.equals("screenshot")){ out.writeObject("screenshot"); out.flush(); BufferedImage screenshot = ImageIO.read(in); ImageIO.write(screenshot, "jpg", new File("screenshot.jpg")); } }catch(Exception ex){ System.out.println("Not connected.\n"); } } 

And server:

 while(true){ try{ socket = new Socket(INDIRIZZO, PORT); out = new ObjectOutputStream(socket.getOutputStream()); out.flush(); in = new ObjectInputStream(socket.getInputStream()); while(!socket.isClosed()){ try { action = (String)in.readObject(); if(azione.equals("screenshot")){ BufferedImage screenshot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(screenshot, "jpg", out); }catch(Exception e){ } } }catch(Exception ex){ // } } 

My problem is that the client receives the image only if I close the socket or outgoing stream, but I do not want this to happen. How can I get around this? How to send an image in bytes? Thanks!

+5
source share
2 answers

The problem is that ImageIO.read is waiting for the end of the thread. Sockets only send it after it closes. (which makes sense)

What you want to do is first send the image size and on the receiver side to read the image as an array of bytes.

 public class Send { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 13085); OutputStream outputStream = socket.getOutputStream(); BufferedImage image = ImageIO.read(new File("C:\\Users\\Jakub\\Pictures\\test.jpg")); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", byteArrayOutputStream); byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array(); outputStream.write(size); outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.flush(); System.out.println("Flushed: " + System.currentTimeMillis()); Thread.sleep(120000); System.out.println("Closing: " + System.currentTimeMillis()); socket.close(); } } public class Receive { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(13085); Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); System.out.println("Reading: " + System.currentTimeMillis()); byte[] sizeAr = new byte[4]; inputStream.read(sizeAr); int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get(); byte[] imageAr = new byte[size]; inputStream.read(imageAr); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr)); System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis()); ImageIO.write(image, "jpg", new File("C:\\Users\\Jakub\\Pictures\\test2.jpg")); serverSocket.close(); } } 
+11
source

You can find a (non-compiled) example in easywayprogramming .

I simplified and fixed the errors, I hope this is a useful answer to your question.

Start the server first, and then start the client as often as you want.

In the example, take a screenshot of the upper left 200x100 pixels of your screen, send them to the server, which will open a new window and display a screenshot.

GreetingServer.java

 import java.awt.image.BufferedImage; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; import java.sql.SQLException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class GreetingServer extends Thread { private ServerSocket serverSocket; Socket server; public GreetingServer(int port) throws IOException, SQLException, ClassNotFoundException, Exception { serverSocket = new ServerSocket(port); serverSocket.setSoTimeout(180000); } public void run() { while(true) { try { server = serverSocket.accept(); BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(server.getInputStream())); JFrame frame = new JFrame(); frame.getContentPane().add(new JLabel(new ImageIcon(img))); frame.pack(); frame.setVisible(true); } catch(SocketTimeoutException st) { System.out.println("Socket timed out!"); break; } catch(IOException e) { e.printStackTrace(); break; } catch(Exception ex) { System.out.println(ex); } } } public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception { Thread t = new GreetingServer(6066); t.start(); } } 

GreetingClient.java

 import java.awt.AWTException; import java.awt.Image; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.Socket; import javax.imageio.ImageIO; public class GreetingClient { Image newimg; static BufferedImage bimg; byte[] bytes; public static void main(String [] args) { String serverName = "localhost"; int port = 6066; try { Socket client = new Socket(serverName, port); Robot bot; bot = new Robot(); bimg = bot.createScreenCapture(new Rectangle(0, 0, 200, 100)); ImageIO.write(bimg,"JPG",client.getOutputStream()); client.close(); } catch(IOException | AWTException e) { e.printStackTrace(); } } } 
+5
source

Source: https://habr.com/ru/post/983558/


All Articles