title: DatagramSocket will randomly stop receiving packets (still receives sometimes)
body: <p>I am trying to implement a TFTP client in Java. The client works perfectly on Localhost and will sometimes work sending to a TFTP server over the network. However, sometimes my DatagramSocket will randomly stop receiving packets. It will send a read/write request but it never receives the next message the server tries to send back. I've checked Wireshark, and the server is for sure receiving and trying to send. And firewalls are turned off where the need to be. Can't figure out what the problem is. Here is the code I am using: </p>
  
  <pre><code>public class TFTPClient {
  String filename;
  String mode;
  boolean read;
  PacketBuilder builder;
  String IP;
  JFrame frame;
  public TFTPClient(String uifilename, String uimode, boolean uiread, String uiIP, JFrame uiFrame){
      this.filename = uifilename;
      this.read = uiread;     
      this.mode = uimode;
      this.IP = uiIP;
      builder = new PacketBuilder();
      this.frame = uiFrame;
  }
  
  
  /*
   * Method choses between reading a file and writing a file based on boolean selected in main UI. 
   */
  public void startTFTP() throws IOException{ 
      if (read){
          readFile();
      }
      else{
          writeFile();
      }
  }
  /*
   * Method is used for writing a file
   */
  private void writeFile() throws IOException{
      byte[] WRQ =  builder.getWRQ(filename,mode);
      String filenameAndExtension = filename;
      RandomAccessFile f = new RandomAccessFile(filenameAndExtension, "r");
      byte[] fileBytes = new byte[(int)f.length()];
      f.read(fileBytes); 
      f.close();
  
  
  
       DatagramSocket TFTPSocket = new DatagramSocket();
       TFTPSocket.setSoTimeout(5000);
  
  
       //create the packet and send to port 69 of the given IP 
       DatagramPacket wrqPacket = new DatagramPacket(WRQ, WRQ.length,
               InetAddress.getByName(IP), 69);
  
         try {
             TFTPSocket.send(wrqPacket);
           } catch (IOException e) {
              e.printStackTrace();
              System.exit(1);
           }
         byte[] ackByte = new byte[4];
         DatagramPacket ackPacket = new DatagramPacket(ackByte,
                 ackByte.length);
         int blockNumber = 0;
         DatagramPacket dataPacket;
         boolean terminateOnNextAck = false;
         boolean needExtraDataPacket = false;
         int currentIndex = 0;
         while(true)
          {
             TFTPSocket.receive(ackPacket);
             System.out.println("Server acked " + ackByte[3]);
             System.out.println("Expected ack " + blockNumber);
  
             blockNumber++;
  
             if(terminateOnNextAck){
                 break;
             }
  
  
            byte[]DATAdata;
            if (needExtraDataPacket){
                DATAdata = new byte[0];
                terminateOnNextAck = true;
            }
            else if (currentIndex + 512 &gt; fileBytes.length){
                //This is our last byte. Length will be smaller than 508
                DATAdata = new byte [fileBytes.length - currentIndex];
                terminateOnNextAck = true;
            }
            else{
                DATAdata = new byte[512];
            }
            if (currentIndex + 512 ==fileBytes.length){
                needExtraDataPacket = true;
            }
            for (int i = 0; i&lt;DATAdata.length; i++){
                DATAdata[i] =  fileBytes[currentIndex];
                currentIndex++;
  
            }
  
            byte[] DATA = builder.getData(DATAdata, blockNumber);
  
  
             dataPacket = new DatagramPacket(DATA, DATA.length,
                       InetAddress.getByName(IP),ackPacket.getPort());         
                 try {
                     TFTPSocket.send(dataPacket);
                   } catch (IOException e) {
                      e.printStackTrace();
                      System.exit(1);
                   }   
          }
         TFTPSocket.close();
         System.out.println("Write sucessful");
  
  }
  /*
   * Method is used for reading a file
   */
  private void readFile() throws IOException{
      //Get RRQ packet
      byte[] RRQ =  builder.getRRQ(filename,mode);
      StringBuffer fileText = new StringBuffer();
  
       DatagramSocket TFTPSocket = new DatagramSocket();  
       TFTPSocket.setSoTimeout(5000);
       //create the packet and send to port 69 of the given IP 
       DatagramPacket rrqPacket = new DatagramPacket(RRQ, RRQ.length,
               InetAddress.getByName(IP), 69);
  
         try {
             TFTPSocket.send(rrqPacket);
           } catch (IOException e) {
              e.printStackTrace();
              System.exit(1);
           }
         byte[] dataByte = new byte[516];
         for (int i = 516;i&lt;516;i++){
             dataByte[i] = 0;
         }
         DatagramPacket dataPacket = new DatagramPacket(dataByte,
                 dataByte.length);
         System.out.println("Client: Waiting for packet.");
         DatagramPacket ackPacket;
         boolean error = false; 
         while(true)
          {
             TFTPSocket.receive(dataPacket);
             System.out.println(TFTPSocket.getLocalPort());
             if (dataByte[1] == 5){
                 error = true;
                 break;
             }
             fileText.append(new String(dataPacket.getData(),0,dataPacket.getLength()));
             byte blockNumbers[] = new byte[2];
             blockNumbers[0] = dataByte[2];
             blockNumbers[1] = dataByte[3];
             byte[] ACK =  builder.getACK(blockNumbers);
             ackPacket = new DatagramPacket(ACK, ACK.length,
                       InetAddress.getByName(IP),dataPacket.getPort());
  
                 try {
                     TFTPSocket.send(ackPacket);
                   } catch (IOException e) {
                      e.printStackTrace();
                      System.exit(1);
                   }   
  
             if (dataByte[515] == 0){
                 break;
             }
             dataByte[515] = 0;
          }
  
         if (!error){
            JOptionPane.showMessageDialog(frame, "Read Successful!");
         System.out.println(fileText);
         }
         else{
             JOptionPane.showMessageDialog(frame,"Error from server: " + new String(dataPacket.getData(),0,dataPacket.getLength()));
         }    
  }   
  }
  </code></pre>