我正試圖讀取從客戶端發送到服務器的數據包。但是,我收到一條錯誤消息:如何在C#中捕獲TCP數據包
無法加載DLL'wpcap':無法找到指定的模塊。 (異常來自HRESULT:0x8007007E)」
可能有人請指出我該如何解決這個錯誤
我的代碼:?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpPcap;
using SharpPcap.AirPcap;
using PacketDotNet;
namespace ConsoleApplication2MB
{
class Program
{
static void Main(string[] args)
{
//Extract the device list
CaptureDeviceList devices = CaptureDeviceList.Instance;
if (devices.Count < 1)
{
Console.WriteLine("No devices were found on this machine");
return;
}
Console.WriteLine("\nThe following devices are available on this machine:");
Console.WriteLine("----------------------------------------------------\n");
Console.WriteLine("Available AirPcap devices:");
for (var i = 0; i < devices.Count; i++)
{
Console.WriteLine("[{0}] - {1}", i, devices[i].ToString());
}
Console.WriteLine();
Console.Write("Please choose a device to capture: ");
var devIndex = int.Parse(Console.ReadLine());
var device = devices[devIndex];
device.Open(DeviceMode.Promiscuous);
string filter = "ip and tcp";
device.Filter = filter;
device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
device.StartCapture();
//Console.Write("Please press enter to exit...");
//Console.ReadLine();
}
private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
{
var time = e.Packet.Timeval.Date;
var len = e.Packet.Data.Length;
Console.WriteLine("{0}:{1}:{2},{3} Len={4}",
time.Hour, time.Minute, time.Second, time.Millisecond, len);
Console.WriteLine(e.Packet.ToString());
var packet = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
var tcpPacket = PacketDotNet.TcpPacket.GetEncapsulated(packet);
if (tcpPacket != null)
{
var ipPacket = (PacketDotNet.IpPacket)tcpPacket.ParentPacket;
System.Net.IPAddress srcIp = ipPacket.SourceAddress;
System.Net.IPAddress dstIp = ipPacket.DestinationAddress;
int srcPort = tcpPacket.SourcePort;
int dstPort = tcpPacket.DestinationPort;
Console.WriteLine("{0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}",
time.Hour, time.Minute, time.Second, time.Millisecond, len,
srcIp, srcPort, dstIp, dstPort);
}
}
}
}
也許你沒有安裝WinPcap的? – wRAR 2013-03-24 16:33:09
你是否以管理員身份執行此代碼? – MLeblanc 2013-03-24 16:33:44
檢查錯誤的堆棧跟蹤!並通過它在這裏,這是如此通用的錯誤 – 2013-03-24 16:35:20