首页 技术 正文
技术 2022年11月15日
0 收藏 426 点赞 2,324 浏览 1615 个字

最近需要在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("\nThe following devices are available on this machine:");
Console.WriteLine("----------------------------------------------------\n"); /* Scan the list printing every entry */
foreach(var dev in devices)
Console.WriteLine("{0}\n",dev.ToString()); Console.Write("Hit 'Enter' to exit...");
Console.ReadLine();
}
}
}

在 Visual Studio 2010 中配置SharpPcap

需要注意的问题:

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,498
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,911
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,745
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,499
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,138
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,302