0

So I am trying to write code to implement an image being sent over in bytes packet by packet within Java over client and server. This is the jist of the assignment

Here is the code for the server:

import java.io.*;
import java.net.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

class UDPServer {
   public static void main(String args[]) throws Exception
   {
   DatagramSocket serverSocket = new DatagramSocket(9876);                                               //makes the datagram socket at the port 9876 for dat transfer 
   byte[] receiveData = new byte[1024];                                                                  //allocate memory for data being recieved
   byte[] sendData = new byte[1024];                                                                     //allocate memory for data being sent
   byte[] buffer = new byte[1024];

   DatagramPacket receiveByte = new DatagramPacket(buffer, buffer.length);
   serverSocket.receive(receiveByte);
   byte[] data = receiveByte.getData();
   ByteArrayInputStream bais = new ByteArrayInputStream(data);
   BufferedImage image2 = ImageIO.read(bais);
   ImageIO.write(image2, "bmp", new File("C:\\Users\\Demon FFA\\Documents\\Network Design\\Phase 2\\word1.bmp"));
   System.out.println("Image receieved!"); 

Here is the code for client side:

import java.io.*;
import java.net.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

class UDPClient {
   public static void main(String args[]) throws Exception
      {
         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));            //create input stream for client to server message
         DatagramSocket clientSocket = new DatagramSocket();                                          //create the socket for the client
         InetAddress IPAddress = InetAddress.getByName("localhost");                                  //change the hostname into an ip address with DNS
         byte[] sendData = new byte[1024];                                                            //allocate memory for data being sent
         byte[] receiveData = new byte[1024];                                                         //allocate memory for the data being recieved


      BufferedImage image = ImageIO.read(new File("C:\\Users\\Demon FFA\\Documents\\Network Design\\Phase 2\\word.bmp"));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(image, "bmp", baos);
      //baos.flush();
      byte[] data = baos.toByteArray();
      byte[] buffer = new byte[1024];
      int i;
      int c = 0;

      for(i = 0; i<data.length; i++) {
        buffer[c] = data[i];
        c++;
        if (i%1024==0){
        c = 0;
        }
        DatagramPacket sendByte  = new DatagramPacket(buffer, buffer.length, IPAddress, 9876);
        clientSocket.send(sendByte);
    } 
  }
}

Here is the error:

CMD Error: Image= Null

  • The error you have attached is coming out of the server. Take a look at: byte[] data = receiveByte.getData(); What is in the data array after this call? Have you run this code in debug? Take a look at this answer for how to use a debugger https://stackoverflow.com/a/25385174/1978785 – Charlie Wallace Feb 19 '19 at 01:15
  • Your server needs to know how many bytes should be read before the image is fully loaded – MadProgrammer Feb 19 '19 at 01:52
  • Your client is capable of sending more then one packet, but you server is only expecting one. How would the server know when it's received the last packet? – MadProgrammer Feb 19 '19 at 03:20
  • I'm kind of new to java, so I am not sure how I would implement it to expect more than one packet. I have been looking around for an implementation similar to this that does have a server taking more than one packet, but no luck. I am extremely lost. – Farhan Ahmed Feb 19 '19 at 03:39

0 Answers0