簡體   English   中英

使用jpcap獲取完整的TCP數據包數據

[英]Get full TCP packet data using jpcap

我使用jpcap教程中的一個簡單程序。 我想在端口4444上偵聽以檢查我的其他客戶端服務器應用程序。 我有一個問題:方法TCPPacket.getTCPData()返回byte []數組,其限制為30個元素。 我知道數據包包含30字節以上的有用數據,不包括TCP頭字節。

如何獲取超過30個字節的數據包數據?

我檢查了一下,方法tcpPacket.getPayloadDataLength()返回的值大於500,而TCPPacket.getTCPData()返回的數組為30個字節……為什么只有30個?

代碼在這里

public class Test {
    public static void main(String[] args) {
        try {
            Test test = new Test(PacketCapture.lookupDevices()[5].trim().split("\\s")[0]);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Test(String device) throws Exception {
        // Initialize jpcap
        PacketCapture pcap = new PacketCapture();
        System.out.println("Using device '" + device + "'");
        pcap.open(device, true);
        pcap.setFilter("port 4444", true);
        pcap.addPacketListener(new PacketHandler());

        System.out.println("Capturing packets...");
        pcap.capture(-1); // -1 is infinite capturing
    }
}


class PacketHandler implements PacketListener {
    BufferedOutputStream stream;

    public PacketHandler() throws IOException {
        Path path = Paths.get("out.txt");
        stream = new BufferedOutputStream(
                Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.APPEND));
    }

    public void packetArrived(Packet packet) {
        try {
            // only handle TCP packets

            if(packet instanceof TCPPacket) {
                TCPPacket tcpPacket = (TCPPacket)packet;
                byte[] data;
                data = tcpPacket.getTCPData();
                stream.write(data);
                stream.write("\r\n----------\r\n".getBytes());
                stream.flush();
            }
        } catch( Exception e ) {
            e.printStackTrace(System.out);
        }
    }
}

代替pcap.open(device, true); ,請嘗試pcap.open(device, 65535, true, 1000); jpcap的默認快照長度為96字節,這意味着,如果僅使用pcap.open(device, true);打開,則僅會獲得數據包的前96個字節pcap.open(device, true);

暫無
暫無

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

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