title: Receiving packets in UDP- Java
body: <p>Hi i am trying to send packets of 1 kb from client to server using UDP. I can receive all the packets but the problem is while loop is not exiting so i used socket timeout to achieve this.Since this does not suit dynamic environment i want to replace something sensible. Below are my server and client side code, client side follows server side from while loop.I just want to implement RDT 1.0 where the packets are divided into 1kb and send it over the UDP socket to receive in the other side. But while receiving all 1kb packet i have tried quite a few things to exit the while loop and finally end up setting a socket timeout at the receiving side. Also i declared 'i' (i&lt;9 knowing my file size) to get out of the loop in order to execute the rest of the codings before the timeout.Is there a way i can change my while loop so that it suits all environment (sending N number of packets).</p>
  
  <pre><code>//Client side
      class Client
  {
      public static DatagramSocket sock =null;
      public static FileInputStream in=null;
      public static DatagramPacket[] sendpacket=new DatagramPacket[1024];
  public static void main(String[] args) throws Exception
  {
      byte[] array = new byte[1024];
      DatagramSocket sock = new DatagramSocket();
      InetAddress ip= InetAddress.getByName("localhost");
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter a file name: ");
      System.out.flush(); 
      String filename = scanner.nextLine();
      File file = new File(filename);
      in=new FileInputStream(file);
      int length =0,checksum=0,h=1;
      while((length = in.read(array))!=-1)
      {
          sendpacket[h]=new DatagramPacket(array,array.length,ip,1234);
          sock.send(sendpacket[h]);
          checksum+=length;
          System.out.println("Sent packet "+h);
          System.out.println(length);
          System.out.println(checksum);
          h++;
           }
          in.close();
  
  }
  }
  
  
  
  
  
   //Server side
  public class server
  {
      public static DatagramPacket[] recvpacket=new DatagramPacket[100];
      public static DatagramSocket sock=null;
      public static FileOutputStream fos=null;
      public static BufferedOutputStream bos=null;
      public static void main(String args[]) 
      {
  
      try
          {
          try
          {
          sock=new DatagramSocket(1234);//opening a socket
      }
      catch(IOException ex)
      {
      System.out.println(ex);
      }
      sock.setSoTimeout(30000);
      boolean socketalive=true;
      byte[] array=new byte[1024];
      int i=1,checksum=0;
      System.out.println("server is ready to receive packets");
      while(recvpacket!=null)//I need change in this loop 
      {
      recvpacket[i]=new DatagramPacket(array,array.length);
      sock.receive(recvpacket[i]);
      System.out.println("server received packet "+i);
      int length=array.length;
      checksum+=length;
      System.out.println(length);//here i get the size of the buffer so  //checksum is wrong
      System.out.println(checksum);
      fos=new FileOutputStream(new File("profile1.docx"),true);
      fos.write(array);
      for(int j=0; j&lt;1024; j++)
      {
        array[j]=0;
     }
     i++;
      }//while
      System.out.println("server received packet "+i);
      }//try
      catch(IOException e)
      {
      System.out.println(e);
      }
      }//main
      }//class
  </code></pre>