0

I have a UDP server reading images, both UDP and TCP. The protocol format is: length ID payload

When I try to read the payload or image itself, I can do it by using a FLAG, but this is not desirable. How can I stop reading the file in UDP (or know when there is no more data) and write into a file? The commented code, but using a FLAG, I dont want to use a FLAG as other clients may connect and will just transfer the raw image file.

Thanks

while (true) {
       socket.receive(receivePacket);
      /* 
       if (receivePacket.getLength() <= 8) {
           cmd = new String(receivePacket.getData(), 0,receivePacket.getLength());
           if (cmd.equals("END_FILE")) {
               System.out.println("UDP ServerThread() Command: " +  cmd);
               break;
           }
       }*/


       fos.write(receivePacket.getData(), 0, receivePacket.getLength());
   }

gogasca
  • 9,283
  • 6
  • 80
  • 125
  • 1
    Can't you send an initial packet with the length when connecting? – Tudor Sep 02 '12 at 10:51
  • 1
    UDP is not thought for applications like that. Better use TCP. – SJuan76 Sep 02 '12 at 10:55
  • I do have the length of the file, in initial message, how can i use it to read from socket.receive. – gogasca Sep 02 '12 at 10:58
  • 1
    You have to make sure you can handle lost, fragmented and out of order packets. You need to include a header of your own to determine this, and you can include a flag what says its the end of the file. – Peter Lawrey Sep 02 '12 at 11:41

0 Answers0