2016-04-01 29 views
1

我試圖從UDP數據包中提取信息,但不斷得到隨機輸出。有時候我會得到我想要的東西,有時我不會。解析UDP數據包後的垃圾輸出

這裏是我的代碼:

private static void receivePacket() 
{ 
    try { 
     DatagramSocket socket = new DatagramSocket(port); 
     System.out.println("\n Listening..."); 

     while(true) 
     { 
      // Create a packet 
      DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); 

      // Receive packet 
      socket.receive(packet); 
      byte[] data = packet.getData(); 

      // Parse the packet 
      parse(data, packet.getLength()); 

      socket.send(packet); 
     } 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
} 

// Parse packets 
private static void parse(byte [] data, int dataLength) 
{ 
    ArrayList<String> name = new ArrayList<String>() 
    String domain = ""; 

    // Get ID 
    int id = ((data[0] & 0xff) << 8) + (data[1] & 0xff); 
    System.out.println("\n ID:\t\t" + id); 

    // Get domain name and number of bits for each word in domain 
    for(int i = 0; i < dataLength; i++) 
    { 
     // If the next bit is a letter then we know the current bit is the number of bits 
     if((data[i] <= ' ') || (data[i] > '~')) 
     { 
      try { 
       if((String.format("%c", data[i+1]).matches("^[a-zA-Z]$"))) 
       { 
        int dSize = Integer.parseInt(String.format("%d", data[i])); 
        name.add(Integer.toString(dSize)); 
       } 

      } catch (Exception e) { 
       //e.printStackTrace(); 
      } 
     } 
     else 
     { 
      // If current bit is letter add to ArrayList 
      try { 
       if((String.format("%c", data[i]).matches("^[a-zA-Z]$"))) 
       { 
        name.add(String.format("%c", data[i])); 
        domain += String.format("%c", data[i]); 
       } 

      } catch (Exception e) { 
       //e.printStackTrace(); 
      } 
     } 
    } 

    System.out.println(" Domain:\t" + domain); 
    System.out.print(" Name:\t\t"); 
    name.add("0"); 

    for(int i = 0; i < name.size(); i++) 
     System.out.print("\'" + name.get(i) + "\' "); 

    System.out.println(); 
} 

我使用命令dig @localhost -p 1299 test.mydomain.abc的UDP數據包發送到服務器。這是運行六次後的輸出結果。輸出每個時間應爲以下(ID會有所不同):

ID:   64666 
Domain:  testmydomainabc 
Name:   '4' 't' 'e' 's' 't' '8' 'm' 'y' 'd' 'o' 'm' 'a' 'i' 'n' '3' 'a' 'b' 'c' '0' 

然而,它並不像你在這裏看到從運行#開始4: enter image description here

它是完全隨機的,我不不明白爲什麼。我在Java和Windows 10上編寫這個代碼。任何幫助將不勝感激,謝謝!

原始數據:

試製成功:'4' 't' 'e' 's' 't' '8' 'm' 'y' 'd' 'o' 'm' 'a' 'i' 'n' '3' 'a' 'b' 'c' '0'

b7 01 20 00 01 00 00 00 00 00 01 04 t e s t 08 m y d o m a i n 03 a b c 00 00 01 00 01 00 00) 10 00 00 00 00 00 00 00

不成功:'-127' 'f' '4' 't' 'e' 's' 't' '8' 'm' 'y' 'd' 'o' 'm' 'a' 'i' 'n' '3' 'a' 'b' 'c' '0'

81 f 01 20 00 01 00 00 00 00 00 01 04 t e s t 08 m y d o m a i n 03 a b c 00 00 01 00 01 00 00) 10 00 00 00 00 00 00 00

+1

這可能是有用的簡單地打印出存儲在'packet.getData()的原始數據'看無論問題發生在聯網還是解析算法中。 (當然這對於添加到問題主體也是有用的) – snickers10m

+0

不確定這是您尋找的內容,但我添加了所有我收到的位。 –

+1

在正確的軌道上。我要求提供產生預期產出的試驗的原始數據,以及產生不良產出的試驗的原始數據。然後我們可以比較兩者(並改進您的解析算法來處理我們發現的問題)。 – snickers10m

回答

1

你的循環從開始,可能應該從2開始。否則ID字節將被包括在解析(或投擲所silenty忽略異常)

+0

謝謝,snickers10m實際上幫助我弄清楚了上述情況。我將從'2'開始,而不是我使用的'12'。 –