【问题标题】:How to capture TCP packets in C#如何在 C# 中捕获 TCP 数据包
【发布时间】:2013-03-24 16:28:30
【问题描述】:

我正在尝试读取从客户端发送到服务器的数据包。但是,我收到一条错误消息:

无法加载 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?
  • 您是否以管理员身份执行此代码?
  • 检查堆栈跟踪错误!在这里过去,这是一个非常普遍的错误
  • 是的,我确实以管理员身份执行
  • 当我尝试添加到我的引用时,系统会提示我,“它是有效的程序集还是 COM 组件”。

标签: c# .net networking


【解决方案1】:

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

下载并安装:Windows 安装程序

重新运行您的应用程序。

没有更多:“无法加载 DLL 'wpcap':找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)”

【讨论】:

    【解决方案2】:

    对不起我的不好 en ;您应该安装 PCap 驱动程序,例如:winPCap。 我在win8机器上安装了WinPcap_4_1_3.exe,它可以工作了。

    我都试过了

    var devices = CaptureDeviceList.Instance;
    

    和

    var devices = SharpPcap.WinPcap.WinPcapDeviceList.Instance;
    

    改为

    CaptureDeviceList devices = CaptureDeviceList.Instance;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 2011-11-01
      • 1970-01-01
      • 2012-08-20
      • 2014-02-23
      相关资源
      最近更新 更多