zoukankan      html  css  js  c++  java
  • 在 Visual Studio 2010 中配置SharpPcap

    最近需要在C#下写一个抓取ARP包的程序,网上找来找去,在C#下只能用SharpPcap来做了。SharpPcap是作者把winPcap用C#重新封装而来的,详细信息见如下的链接。

    SharpPcap教程

    我在配置的过程中遇到了一些问题,现在把这些问题的解决方法写下来,以免以后忘了,又开始各种痛苦的调试。

    先来看看我的环境:win7旗舰版 、VS2010旗舰版、WinPcap4.1.3、SharpPcap4.2.0。

    1.安装Winpcap4.1.3(WinPcap4.1.3下载)

    2.解压SharpPcap-4.2.0.bin.zip(SharpPcap4.2.0.bin.zip&&SharpPcap4.2.0.src.zip下载)解压后打开debug文件夹,可以看到里面有两个dll文件,这就是我们程序中要用到的东西。SharpPcap-4.2.0.src.zip压缩包中,包含SharpPcap的所有源代码和一些示例程序。

    3.打开VS2010,新建一个C#的控制台项目。

    4.然后单击“项目”下拉菜单,选择 “添加引用”,在弹出的对话框中单击 “浏览” 选项卡,然后选择第2步中SharpPcap-4.2.0.bin.zip解压后的路径,然后将debug中的SharpPcap.dll添加进去。

    5.将下面的代码,粘贴到你的项目中,测试配置是否成功,如果成功则会显示你的网络适配器的信息。

    using System;
    using System.Collections.Generic;
    
    using SharpPcap;
    
    namespace Example1
    {
        /// <summary>
        /// Obtaining the device list
        /// </summary>
        public class IfListAdv
        {
            /// <summary>
            /// Obtaining the device list
            /// </summary>
            public static void Main(string[] args)
            {
                // Print SharpPcap version
                string ver = SharpPcap.Version.VersionString;
                Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);
    
                // Retrieve the device list
                var devices = CaptureDeviceList.Instance;
    
                // If no devices were found print an error
                if(devices.Count < 1)
                {
                    Console.WriteLine("No devices were found on this machine");
                    return;
                }
    
                Console.WriteLine("
    The following devices are available on this machine:");
                Console.WriteLine("----------------------------------------------------
    ");
    
                /* Scan the list printing every entry */
                foreach(var dev in devices)
                    Console.WriteLine("{0}
    ",dev.ToString());
    
                Console.Write("Hit 'Enter' to exit...");
                Console.ReadLine();
            }
        }
    }

    需要注意的问题:

    1.如果你要用SharpPcap3.5,那么你在新建项目时.Net FrameWork 要选成3.5,(vs2010默认是4.0),否则运行时会出现错误。

    2.如果你用的是SharpPcap高版本的dll,测试示例程序时最好不要用低版本的,否则可能会出错。楼主测试的时候,dll用的是SharpPcap4.2的,示例程序用的是SharpPcap3.5的,生成时会出现错误。

  • 相关阅读:
    深入理解Java中停止线程
    浅入浅出JDBC————1分钟了解JDBC
    Java多线程入门中几个常用的方法
    创建Java多线程的两种方式和线程异常
    小白学习前端---第二天 HTML的基本属性————1
    Info类
    Control类
    demo 代码
    防作弊原理
    状态类
  • 原文地址:https://www.cnblogs.com/rainbowzc/p/4014439.html
Copyright © 2011-2022 走看看