gpt4 book ai didi

java - 无法使用 jpcap 打开设备

转载 作者:行者123 更新时间:2023-11-29 05:50:44 24 4
gpt4 key购买 nike

我在使用 jpcap 库打开找到的网络设备时遇到问题。我已经在 system32 和 syswow64 中安装了 winpcap 和 jpcap.dll。以下教程代码在尝试打开设备时崩溃。崩溃日志:

PacketCapture: loading native library jpcap.. ok
net.sourceforge.jpcap.capture.CaptureDeviceOpenException: Error opening adapter: The system cannot find the device specified. (20)
at net.sourceforge.jpcap.capture.PacketCapture.open(Native Method)
at net.sourceforge.jpcap.capture.PacketCapture.open(PacketCapture.java:57)
at networksnifferdesktop.NetworkSnifferDesktop.<init>(NetworkSnifferDesktop.java:26)
at networksnifferdesktop.NetworkSnifferDesktop.main(NetworkSnifferDesktop.java:40)
Java Result: 1

在调试中我可以看到 m_device 设置为:

"\Device\NPF_{EC5226CF-3F55-4148-B40E-1FC3F8BB3398}   Realtek PCIe GBE Family Controller"

在下面的代码中:

package networksnifferdesktop;

import net.sourceforge.jpcap.capture.*;
import net.sourceforge.jpcap.net.*;

public class NetworkSnifferDesktop
{
private static final int INFINITE = -1;
private static final int PACKET_COUNT = 10;

// BPF filter for capturing any packet
private static final String FILTER = "";

private PacketCapture m_pcap;
private String m_device;

public NetworkSnifferDesktop() throws Exception
{
// Step 1: Instantiate Capturing Engine
m_pcap = new PacketCapture();

// Step 2: Check for devices
m_device = m_pcap.findDevice();

// Step 3: Open Device for Capturing (requires root)
m_pcap.open(m_device, true);

// Step 4: Add a BPF Filter (see tcpdump documentation)
m_pcap.setFilter(FILTER, true);

// Step 5: Register a Listener for Raw Packets
m_pcap.addRawPacketListener(new RawPacketHandler());

// Step 6: Capture Data (max. PACKET_COUNT packets)
m_pcap.capture(PACKET_COUNT);
}

public static void main(String[] args)
{
try
{
NetworkSnifferDesktop example = new NetworkSnifferDesktop();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}

class RawPacketHandler implements RawPacketListener
{
private static int m_counter = 0;

public void rawPacketArrived(RawPacket data)
{
m_counter++;
System.out.println("Received packet (" + m_counter + ")");
}
}

最佳答案

"\Device\NPF_{EC5226CF-3F55-4148-B40E-1FC3F8BB3398} Realtek PCIe GBE Family Controller",如果您的字面意思是一个字符串,其第一个字符是“\Device”及其最后一个字符是“Controller”中的“r”,不是有效的 WinPcap 设备名称字符串。

"\Device\NPF_{EC5226CF-3F55-4148-B40E-1FC3F8BB3398}" 将是一个有效的设备名称字符串。

通过查看 Jpcap 源代码,findDevice 方法似乎NOT 返回有效的设备名称字符串。它被记录为返回“描述网络设备的字符串”;它返回的是一个字符串,其中包含设备名称字符串、一个换行符、两个空格和设备的供应商描述字符串。 This has been reported as a Jpcap bug .

我建议您扫描字符串以查找第一个空白字符(“空白”包括空格和换行符),并使用作为传递给打开例程的设备名称,直到但不是包括那个空白字符。 (如果找不到空白字符,请使用整个字符串。)

关于java - 无法使用 jpcap 打开设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com