简体   繁体   English

如何在Java中获取UDP数据包的TTL?

[英]How to get TTL of a UDP packet in Java?

I am using a Java application to send UDP packets to an Android device. 我正在使用Java应用程序将UDP数据包发送到Android设备。 There I have another Java application that receives these UDP packets and displays its data - very simple. 我有另一个Java应用程序接收这些UDP数据包并显示其数据 - 非常简单。

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. 现在我正在研究一些路由算法 - 因此,知道UDP数据包自发送以来的跳数有多好。 My idea is to just read out the TTL (time-to-live) value of the packet and display it. 我的想法是只读出数据包的TTL(生存时间)值并显示它。 Do you know if this is possible with pure Java? 你知道纯Java是否可行吗? The class DatagramPacket doesn't give any hints at all. DatagramPacket类根本不提供任何提示。

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. 我想这是不可能的,因为这些信息可能已经在较低层删除了,但我只想确定。 :-) :-)

The TTL field is, as you know, a feature of the underlying IP protocol (when used), not of UDP. 如您所知, TTL字段是底层IP协议(使用时)的一个特性,而不是UDP的特性。 So it makes sense for it not to be visible in the DatagramPacket API. 因此,它在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. 通常不可能通过数据报级API访问IP数据包:s。 You probably need to look into packet capture. 您可能需要查看数据包捕获。

This is part of IP header and is set from socket, apparently only for multicast sockets. 这是IP头的一部分,是从套接字设置的,显然只适用于多播套接字。 Check this answer: 检查这个答案:

Java control IP TTL? Java控制IP TTL?

For your purpose, if I get it correctly, it would be sufficient to manipulate the TTL of the sender; 为了您的目的,如果我正确地理解,那么操纵发送者的TTL就足够了; eg 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... 例如,将发送套接字的TTL设置为1,2,3,4,5,并为每个发送一条内容为“1”,“2”,“3”,“4”或“5”等的消息。有些可能是丢失在接收器上......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM