2013-03-24 55 views
2

我正試圖讀取從客戶端發送到服務器的數據包。但是,我收到一條錯誤消息:如何在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); 
      } 



     } 
    } 
} 
+2

也許你沒有安裝WinPcap的? – wRAR 2013-03-24 16:33:09

+0

你是否以管理員身份執行此代碼? – MLeblanc 2013-03-24 16:33:44

+0

檢查錯誤的堆棧跟蹤!並通過它在這裏,這是如此通用的錯誤 – 2013-03-24 16:35:20

回答

0

對不起,是我不好帶,你應該安裝PCAP司機喜歡:WinPcap的 我win8的機器上安裝WinPcap_4_1_3.exe和它的作品

我都嘗試

var devices = CaptureDeviceList.Instance; 

var devices = SharpPcap.WinPcap.WinPcapDeviceList.Instance; 

代替

CaptureDeviceList devices = CaptureDeviceList.Instance; 
3

轉到:http://www.winpcap.org/install/default.htm

下載並安裝:安裝程序爲Windows

重新運行你的應用程序。

沒有更多:「無法加載DLL‘wpcap’:指定的模塊找不到(異常來自HRESULT:0x8007007E)」