mreichelt
mreichelt

Reputation: 12455

How to get TTL of a UDP packet in Java?

I am using a Java application to send UDP packets to an Android device. There I have another Java application that receives these UDP packets and displays its data - very simple.

Now I am working on some routing algorithms - therefore it would be nice to know how many hops a UDP packet did since it was send. My idea is to just read out the TTL (time-to-live) value of the packet and display it. Do you know if this is possible with pure Java? The class DatagramPacket doesn't give any hints at all.

I guess that this is not possible because this information might already have been removed at a lower layer, but I just want to be sure. :-)

Upvotes: 6

Views: 4859

Answers (2)

P Marecki
P Marecki

Reputation: 1138

For your purpose, if I get it correctly, it would be sufficient to manipulate the TTL of the sender; e.g. set TTL of the sending socket to 1,2,3,4,5 and for each send one message with content "1","2","3","4" or "5" etc. Some will likely be missing on the receiver...

Upvotes: 0

unwind
unwind

Reputation: 399833

The TTL field is, as you know, a feature of the underlying IP protocol (when used), not of UDP. So it makes sense for it not to be visible in the DatagramPacket API. However, I think you're right; it's not normally possible to get access to the IP packets through datagram-level API:s. You probably need to look into packet capture.

Upvotes: 3

Related Questions