Hello,
I have to take a snapshot of my screen and send the image to a client using Datagrams. I think my problem is at the client part. The image is null, and I don't know why.

Here is the server part. I tried to send 534 byte packets (bigger are not accepted by UDP), packet by packet. These packets arrive to the client but I don't know WHY the image is null.

BufferedImage img = bot.createScreenCapture(rect);

        DatagramSocket datagramSocket = null;

        datagramSocket = new DatagramSocket(); 
  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        ImageIO.write(img, "jpg", baos);
        
        baos.flush();

        byte[] buffer = baos.toByteArray();
        InetAddress receiverAddress = null;

            receiverAddress = InetAddress.getByName("192.168.1.66"); 
  

            byte[] buff = new byte[534];
            int c=0;
            for(int i=0;i<buffer.length;i++){
                buff[c] = buffer[i];
                c++;
                if(i%533==0){
                    DatagramPacket packet = new DatagramPacket(buff, buff.length, receiverAddress, 80);
                    buff = new byte[534];
                    c=0;

                    datagramSocket.send(packet);
    
                    System.out.println("sent a mini-packet");
                }
            }


            DatagramPacket packet = new DatagramPacket(buff, buff.length, receiverAddress, 80);
 
            datagramSocket.send(packet);
            System.out.println("sent last mini-packet");

Code for client:

byte[] buffer = new byte[534];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        try {
            datagramSocket.receive(packet);
        } catch (IOException ex) {
            Logger.getLogger(UPDReceiveThread.class.getName()).log(Level.SEVERE, null, ex);
        }

byte[] buff = packet.getData();
System.out.println("the length of the packet: "+buff.length);
BufferedImage img = null;
        try {
            img = ImageIO.read(new ByteArrayInputStream(buff));
        } catch (IOException ex) {
            Logger.getLogger(chatClient.class.getName()).log(Level.SEVERE, null, ex);
        }



ImagePanel ipanel = new ImagePanel(img);

    img_panel.removeAll();
    img_panel.add(ipanel);
    img_panel.repaint();

So... where is my mistake? Or why is this not working? I get a NullPointerException here: "ImagePanel ipanel = new ImagePanel(img);".

Help appreciated!

try simply creating a byte array or something and send it over using UDP to the client, that way you can tell if the problem is the way you are getting the image or if it is in the network code. Just break the problem up. Also it looked like you were using port 80 to send and receive the data, I am not an expert and don't really know if it causes a problem but I would stay away from using the HTTP port which is 80.

try simply creating a byte array or something and send it over using UDP to the client, that way you can tell if the problem is the way you are getting the image or if it is in the network code. Just break the problem up. Also it looked like you were using port 80 to send and receive the data, I am not an expert and don't really know if it causes a problem but I would stay away from using the HTTP port which is 80.

Thanks for reply. UDP port 80 has nothing to do with TCP port 80... and you can see the client class:
- I have a byte array - the image as a byte array
- I send the byte array using more 533KB packets cause UPD packet is size limited.
- At the server side I try to build an image from the packets I receive. The packets arrive at destination.. this is sure, but I don't know why the image seems to be 0 byte lenght. Maybe should I send the length of the image before the image itself?

post all of your code with import statements and stuff and Ill help fix it. I just dont wanna piece together anything without all the code.

A lot of code is design for oher tasks so I will not post is cause has nothing to do with this.
I have a button at server application. When I push it, the server-side code from post#1 runs. It take a screenshot - BufferedImage, makes a byte array of it then splits it in 533 bytes parts, sending every part to the destination. After the for loop finishede we send the last remaining part.
Now the client. The piece of code from post#1 is sufficient. When I push a button at the client I run that code that freezes the application waiting for a packet to come. After the packet arrives it is converted frmo a byte array to image and it fills an inage panel, so the image shouldn't be null.
You can just put that code in a new project and test it. I tried to send the image from server in a single packet but I cannot. It says I cannot send a packet big like that.

Maybe can someone show me how to send an image using UPD protocol in Java? And how to receive it at the client? Please.

can u send whole code to my mail id...
i can't fiddle with 2 or 3 lines ..

i done such udp program...

that's why i asking you send whole code..and i sure to resolve...

if you willing then i will give my id...

then i we continue this forum..

Edited 6 Years Ago by musthafa.aj: n/a

Of course I read about Datagrams. The problem is that I should find a way to send the image over UDP. This means I have to:
- convert the image to a byte array
- send the byte array as multiple packets - that means split the image byte array

- receive every packet at client
- make an image from the packet and paint it on the panel.

This is the send image function:

public void sendUDPImg() throws IOException{
        int port = 8892;

        //take a screenshot
        BufferedImage img = bot.createScreenCapture(rect);


        DatagramSocket datagramSocket = null;///////////////


        try {
            ///////////////
            datagramSocket = new DatagramSocket(); ////////////////////////////
        } catch (SocketException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(img, "jpg", baos);
        } catch (IOException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        

            byte[] buffer = baos.toByteArray();//////////////
            InetAddress receiverAddress = null;//////////////////


        try {
            //////////////////
            receiverAddress = InetAddress.getByName("92.84.113.196"); ////////////////////////
        } catch (UnknownHostException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }

            byte[] buff = new byte[534];
            int c=0;
            //split image byte array to parts because too big packets => datagram error
            for(int i=0;i<buffer.length;i++){
                buff[c] = buffer[i];
                c++;
                if(i%533==0){

                    DatagramPacket packet = new DatagramPacket(buff, buff.length, receiverAddress, port);//////////
                    buff = new byte[534];
                    c=0;

                     try {
            //////////
                        
            datagramSocket.send(packet);
        } catch (IOException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }


                    System.out.println("sent mini-packet");
                }
            }
//last packet... don't know the length... however < 534

            DatagramPacket packet = new DatagramPacket(buff, buff.length, receiverAddress, port);//////////
System.out.println("sent mini-packet");
        try {
           datagramSocket.send(packet);
        } catch (IOException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }

            System.out.println("sent image");baos.flush();
    }

This is the receive function: receives ONE packet. It does receive the packet packet but WHY is the image null after I made it from the byte array (from the incoming packet)?

public void receiveUDPImage(){

        byte[] buffer = new byte[534];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
              try {
                  datagramSocket.receive(packet);
              } catch (IOException ex) {
                  Logger.getLogger(UPDReceiveThread.class.getName()).log(Level.SEVERE, null, ex);
              }

        BufferedImage img = null;
             try {
                    img = ImageIO.read(new ByteArrayInputStream(packet.getData()));
              } catch (IOException ex) {
                  Logger.getLogger(chatClient.class.getName()).log(Level.SEVERE, null, ex);
               }



         ImagePanel ipanel = new ImagePanel(img);

         img_panel.removeAll();
         img_panel.add(ipanel);
         img_panel.repaint();
    }

The image panel - as a matter of fact"

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

class ImagePanel extends JPanel {

  private BufferedImage img;

  public ImagePanel(BufferedImage img) {
    this.img = img;
    try{
        Dimension size = new Dimension(img.getWidth(), img.getHeight());

        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);

        }catch(NullPointerException ex){
        ex.printStackTrace();
    }
  }

    @Override
  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }

}

have you checked whether socket is crested or not...

because i noted you using public ip to create connection...

are you doing this as LAN app or Internet app...

I want to do Internet application. I use now port 8892 which is forworded from my router. I tested it and works to send over UDP on internet. When I start the receive function the application freezes until a packet is sent. So, if a packet arives the application UNfreezes. That happens... so that means I can receive and I send the packet with no problem. I think the problem is at arrival of the packet at the client. If you have a similar application using Datagram sockets (not multicast) can you send me a code snipet of sending/receiving the image, please?

Thanks.

Edited 6 Years Ago by Clawsy: n/a

Hmm... it seems the problem is when I split the packets and send them. When they arrive to the client some header information about the images may miss... I get to this conclusion because I could send images smaller than 60k using this code:

public void sendUDPImg(Rectangle r,int port) throws IOException{
        

        //take a screenshot
        BufferedImage img = bot.createScreenCapture(r);

        DatagramSocket datagramSocket = null;///////////////


        try {
            ///////////////
            datagramSocket = new DatagramSocket(); ////////////////////////////
        } catch (SocketException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(img, "jpg", baos);
        } catch (IOException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        

            byte[] buffer = baos.toByteArray();
            InetAddress receiverAddress = null;


        try {
  
            receiverAddress = InetAddress.getByName("192.168.1.66"); 
        } catch (UnknownHostException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }
 DatagramPacket packet = new DatagramPacket(buffer, buffer.length, receiverAddress, port);//////////
System.out.println("sent packet with length:"+buffer.length);
        try {
           datagramSocket.send(packet);
        } catch (IOException ex) {
            Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
        }

            System.out.println("sent image");baos.flush();

So I can send small images. But I need to send bigger images... that's in fact my problem. I have to split the image in packets at server, send it and then make it again from packets at client.

This article has been dead for over six months. Start a new discussion instead.