UDPパケットから情報を抽出しようとしていますが、ランダムな出力が得られています。時には私は私が欲しいものを正確に得て、それ以外の時は私を得ません。私は、サーバーへのUDPパケットを送信するためにコマンドdig @localhost -p 1299 test.mydomain.abc
を使用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();
}
:
は、ここに私のコードです。 6回実行した後の出力です。出力は、各時間は、以下の(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'
「これは完全にランダムであると私はドン理由を理解できない。私は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
失敗:parse
着工の'-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
'参照する)単に' packet.getData(に格納された生データをプリントアウトすることが有用であり得ますネットワークまたはパースアルゴリズムで問題が発生するかどうか。 (もちろん質問本体に追加すると便利かもしれません) – snickers10m
あなたが探していたものかどうかはわかりませんが、私が受け取ったビットをすべて追加しました。 –
正しいトラックに。私は、予期された出力を生成する試行の生データと、悪い出力を生成する試行の生データを求めています。次に、2つを比較して(そして、私たちが見つけた問題を処理するパースアルゴリズムを改善します)。 – snickers10m