0
Ich versuche, die Quell-und Zieladresse aus einem Paket zu erhalten. Dies ist, wie ich das Paket lese:Holen IP-Paketdaten von ByteBuffer
private void debugPacket(ByteBuffer packet)
{
int buffer = packet.get();
int version;
int headerlength;
version = buffer >> 4;
headerlength = buffer & 0x0F;
headerlength *= 4;
Log.d(TAG, "IP Version:"+version);
Log.d(TAG, "Header Length:"+headerlength);
String status = "";
status += "Header Length:"+headerlength;
buffer = packet.get(); //DSCP + EN
buffer = packet.getChar(); //Total Length
Log.d(TAG, "Total Length:"+buffer);
buffer = packet.getChar(); //Identification
buffer = packet.getChar(); //Flags + Fragment Offset
buffer = packet.get(); //Time to Live
buffer = packet.get(); //Protocol
Log.d(TAG, "Protocol:"+buffer);
status += " Protocol:"+buffer;
buffer = packet.getChar(); //Header checksum
String sourceIP = "";
buffer = packet.get(); //Source IP 1st Octet
sourceIP += ((int) buffer) & 0xFF;
sourceIP += ".";
buffer = packet.get(); //Source IP 2nd Octet
sourceIP += ((int) buffer) & 0xFF;
sourceIP += ".";
buffer = packet.get(); //Source IP 3rd Octet
sourceIP += ((int) buffer) & 0xFF;
sourceIP += ".";
buffer = packet.get(); //Source IP 4th Octet
sourceIP += ((int) buffer) & 0xFF;
Log.d(TAG, "Source IP:"+sourceIP);
status += " Source IP:"+sourceIP;
String destIP = "";
buffer = packet.get(); //Destination IP 1st Octet
destIP += ((int) buffer) & 0xFF;
destIP += ".";
buffer = packet.get(); //Destination IP 2nd Octet
destIP += ((int) buffer) & 0xFF;
destIP += ".";
buffer = packet.get(); //Destination IP 3rd Octet
destIP += ((int) buffer) & 0xFF;
destIP += ".";
buffer = packet.get(); //Destination IP 4th Octet
destIP += ((int) buffer) & 0xFF;
Log.d(TAG, "Destination IP:" + destIP);
status += " Destination IP:"+destIP;
try {
InetAddress addr = InetAddress.getByName(destIP);
String host = addr.getHostName();
Log.d(TAG, "Hostname:" + host);
}
catch (UnknownHostException e) {
Log.d(TAG,"Unknown host");
}
}
Es funktioniert für die ersten paar Pakete in Ordnung, aber dann manchmal bekomme ich ein BufferUnderflowException an einem der packet.get()
Linien. Wie kann ich das verhindern?
Sie sollten 'packet.compact()', nicht 'packet.clear()' aufrufen, außer Sie wissen, dass Sie mit dem Puffer fertig sind. – EJP