DatagramSocket将随机停止接收数据包(有时仍会接收)

时间:2014-04-03 19:12:10

标签: java sockets udp datagram tftp

我正在尝试用Java实现TFTP客户端。客户端在Localhost上运行良好,有时可以通过网络发送到TFTP服务器。但是,有时我的DatagramSocket会随机停止接收数据包。它将发送读/写请求,但它永远不会收到服务器尝试发回的下一条消息。我已经检查过Wireshark,服务器肯定会收到并尝试发送。防火墙在需要的地方被关闭。无法弄清问题是什么。这是我正在使用的代码:

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 > 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<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<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()));
       }    
}   
}

1 个答案:

答案 0 :(得分:0)

该问题与Mac OS X有关。该程序在Windows上运行良好。但不完全确定原因。