C#如何实现抓包-创新互联
这篇文章主要讲解了C#如何实现抓包,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

工具:SharpPcap 4.2.0
vs工程:控制台应用程序
关于C#抓包,我只找到SharpPcap 这个dll,相关的资料不多,而且都是挺老的,所以就顺手记一下自己的代码,给有同样需求的人一个参考吧。
当然,代码可能存在问题,请见谅。
一、获取连接设备
// 获取连接列表
CaptureDeviceList devices = CaptureDeviceList.Instance;
// 无连接
if (devices.Count < 1)
{
Console.WriteLine("No devices were found on this machine");
return;
}
Console.WriteLine("\n以下为本机连接:");
Console.WriteLine("--------------\n");
int j=0;
string temp = "";
Regex r = new Regex("FriendlyName: .*\n"); //匹配连接的FriendlyName
Match m;
// 打印连接设备
foreach (ICaptureDevice dev in devices)
{
temp = dev.ToString();
m = r.Match(temp);
Console.WriteLine("{0}:{1}\n", j++, m.ToString());
}
Console.Write("输入设备号");
string input = Console.ReadLine();
int i = 0;
try
{
i = Int32.Parse(input);
}
catch (Exception e)
{
Console.WriteLine("非法输入!"+e.Message);
return;
}
if (devices.Count < 1 || i == -1)
{
Console.WriteLine("变量非法!");
return;
}
// 得到指定连接设备
ICaptureDevice device = devices[i]; 分享名称:C#如何实现抓包-创新互联
链接分享:http://www.jxjierui.cn/article/djcgoj.html


咨询
建站咨询
