jpcapライブラリで見つかったネットワークデバイスを開くのに問題があります。私はwinpcapをインストールし、system32とsyswow64に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 + ")");
}
}