0

Im sending a object over a socket, an object of the following class:

public class ClientDetail implements Serializable{
public String username;
public String [] capturedText;
public String site;
public BufferedImage screenShot; 
 }

and I am trying to send it using the following:

        ClientDetail cD = new ClientDetail();
        cD.capturedText = mon.getCapturedText();
        cD.username = username;
        cD.site = mon.getWebsite();
        ScreenShot ss = new ScreenShot();
        cD.screenShot = ss.getScreenShot();

        if(mon.isCaptured)
        {
            try{
                output.writeObject(cD);
                output.flush(); 
            }catch(IOException ex){
                System.out.println("\nSomething messed up sending the message!");
            }
        }

This is failing to send. Can you send an object that contains a image over a socket?

  • What error message did you get? / What was the exception? If you are just swallowing `ex`, then its no wonder that you do not know what goes wrong. – skiwi Feb 17 '14 at 17:12
  • ava.io.NotSerializableException: java.awt.image.BufferedImage at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) at java.io.ObjectOutputStream.writeSerialData(Unknown Source) at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at client.Client.whileConnected(Client.java:76) at client.Client.startRunning(Client.java:31) at client.ClientMain.main(ClientMain.java:8) – user3253722 Feb 17 '14 at 17:16
  • I knew it had something to do with the bufferedimage, as u use a different method to write a image over a socket. (Im a a newbie btw..go easy lol) – user3253722 Feb 17 '14 at 17:17
  • because the bufferedimage isnt serializable i will probably just have to send it separate.. or do u know of a way to make it serializable? – user3253722 Feb 17 '14 at 17:20
  • Write the BufferedImage to an ByteArrayOutputStream using the method shown in [this answer](http://stackoverflow.com/a/12674109/3080094), remove BufferedImage from ClientDetail and add the byte-array. Byte arrays are Serializable. – vanOekel Feb 17 '14 at 18:39
  • Yeah thats what I ended up doing. thanks very much – user3253722 Feb 17 '14 at 19:26

1 Answers1

0

Send thing:

ImageIO.write((RenderedImage) image, "png", output);

Receiver thing you know:

ImageIO.read(input);

  • please [add more detail](https://stackoverflow.com/help/how-to-answer) to explain more about how this accomplishes what the OP is asking, so that the OP *understands why* this will help – landru27 Nov 17 '18 at 19:16