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!