Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an image over a socket

Tags:

java

sockets

I am lately trying to create a program, just like teamviewer. It's going kinda well, but I am currently facing a problem.

I am trying to make my program send an image over the socket. When I run this code, it never outputs "Image should be sent!", so I think the problem is in the ImageIO.write line.

BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
try {
    ImageIO.write(screencapture, "jpg", socket.getOutputStream());
    System.out.println("Image should be sent!");
} catch (IOException ex){
    ex.printStackTrace();
} finally {
    if ( socket != null ){
        try { socket.close(); } catch (IOException ex){}
    }
    System.out.println("Image sent and socket closed!");
}

There is also a client on the other side, consuming the data being sent by the code above. The code to do this is:

BufferedImage image = ImageIO.read(socket.getInputStream());
JLabel label = new JLabel(new ImageIcon(image));
f.getContentPane().add(label);

Now my question is, what is wrong with this code, and how can I make this work?

like image 521
user2988879 Avatar asked May 25 '26 11:05

user2988879


2 Answers

Actually the code looks ok. Here's a complete code, that works fine on my machine:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Server {
  public static void main(String[] args) throws Exception {
    BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    try (ServerSocket serv = new ServerSocket(25000)) {
      System.out.println("waiting...");
      try (Socket socket = serv.accept()) {
        System.out.println("client connected");
        ImageIO.write(screencapture, "jpg", socket.getOutputStream());
        System.out.println("sent");
      }
    }
  }
}

class Client {
  public static void main(String[] args) throws Exception {
    try(Socket socket = new Socket("localhost", 25000)){
      BufferedImage image = ImageIO.read(socket.getInputStream());
      JLabel label = new JLabel(new ImageIcon(image));
      JFrame f = new JFrame("vnc");
      f.getContentPane().add(label);
      f.pack();
      f.setVisible(true);
    }
  }
}

Though this will only work if you close the socket after sending the images. It will fail if you'll just try to send a few images over the same socket. See https://stackoverflow.com/a/6973863/211205.

like image 120
rzymek Avatar answered May 28 '26 01:05

rzymek


I had worked on this earlier and posted the solution in my blog. Please visit it for complete source code. Need your feed back too.

You need to read thread, socket and image writing to do this.

http://javabelazy.blogspot.in/2013/10/sending-screenshot-from-client-to.html

BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimensions));
ImageIO.write(screenshot,"png",serverSocket.getOutputStream());
ImageIO.write(img, "png", new File(fileName+".png"))
like image 31
Konzern Avatar answered May 28 '26 01:05

Konzern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!