简体   繁体   中英

Sending an image over a socket

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?

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 .

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM