Search code examples
javaimageclienthost

Java send image from client to server


Im coding a little program and i want the client to take a printscreen and then send the image to the server they are connected to.

Heres the client code.

public void run() 
{
    while(true)
    {
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage PrintScreen = robot.createScreenCapture(screenRect);
        //what do I do here?!?!?!
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

And here is the server code

public void run() 
{
    while(true)
    {
        //Read image sent from client.


        //Sleep shit
        try 
        {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Hopefully you can help me. I've tried to google to get my answer but there are some things i don't really understand. I've seen people converting the image to an array of bytes. Why do you have to do this?


Solution

  • I recommend you read This, to see how to transfer BufferedImage vie Streams, This to see how to send data from client to server(if have any problems with it), and this to see how to create image on server side.