簡體   English   中英

Java中是否有辦法監聽UDP廣播消息並獲取消息發送源IP和Mac

[英]Is there a way in java to listen to UDP broadcast msgs AND get the source IP address and Mac that the message was sent from

我需要創建一個功能與WireShark類似的Java應用程序,因為它能夠偵聽UDP通信。 但是我不僅需要了解數據報,還需要了解發送方的IP和mac地址。 有沒有辦法用Java做到這一點?

這是我在Wireshark中談論的內容的屏幕截圖

WireShark圖片

請注意,以太網II堆棧具有mac,而Internet協議版本4具有Src Ip。

這種情況的用例是網絡上有多個設備在發射數據(例如,此消息為STS:ANT:OK:8)。 但是我需要知道此發送者的mac和IP是什么,以便我可以按發送者和mac對msg進行分類。 (從技術上講,我可以在網絡上有重復的IP。)因此兩者都是必需的。 這也使我可以顯示發生這種情況的錯誤情況。

IP協議是UDP / TCP或其他層的基礎工具。 您可能需要捕獲數據包並偵聽特定端口(為UDP端口過濾IP消息)

否則,您可以使用外部服務器命令來更有效地完成它。

這是一個示例( 如何Java中嗅探網絡流量? ):

public static void main(String[] args) throws Exception {
    File f = new File("sample.pcap");

    EthernetDecoder eth = new EthernetDecoder();
    IpDecoder ip = new IpDecoder();
    TcpDecoder tcp = new TcpDecoder(new TcpPortProtocolMapper());
    UdpDecoder udp = new UdpDecoder(new UdpPortProtocolMapper());

    eth.register(EthernetType.IPV4, ip);
    ip.register(InternetProtocol.TCP, tcp);
    ip.register(InternetProtocol.UDP, udp);

    PcapInputStream is = new PcapFileInputStream(f);
    while (true) {
        // getPacket() will throws EOFException and you should call is.close() 
        PcapPacket packet = is.getPacket();
        eth.decode(packet);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM