所有代码地址:http://download.csdn.net/detail/u013529927/7182963

先扔几个基础的~

1. IfListAdv

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 versionstring ver = SharpPcap.Version.VersionString;Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);// Retrieve the device listvar devices = CaptureDeviceList.Instance;// If no devices were found print an errorif(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();}}
}

2.ArpResolve

using System;
using System.Collections.Generic;
using SharpPcap;
using SharpPcap.LibPcap;namespace Example2
{/// <summary>/// A sample showing how to use the Address Resolution Protocol (ARP)/// with the SharpPcap library./// </summary>public class ArpTest{public static void Main(string[] args){// Print SharpPcap versionstring ver = SharpPcap.Version.VersionString;Console.WriteLine("SharpPcap {0}, Example2.ArpResolve.cs\n", ver);// Retrieve the device listvar devices = LibPcapLiveDeviceList.Instance;// If no devices were found print an errorif(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("----------------------------------------------------");Console.WriteLine();int i = 0;// Print out the available devicesforeach(var dev in devices){Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);i++;}Console.WriteLine();Console.Write("-- Please choose a device for sending the ARP request: ");i = int.Parse( Console.ReadLine() );var device = devices[i];System.Net.IPAddress ip;// loop until a valid ip address is parsedwhile(true){Console.Write("-- Please enter IP address to be resolved by ARP: ");if(System.Net.IPAddress.TryParse(Console.ReadLine(), out ip))break;Console.WriteLine("Bad IP address format, please try again");}// Create a new ARP resolverARP arper = new ARP(device);// print the resolved address or indicate that none was foundvar resolvedMacAddress = arper.Resolve(ip);if(resolvedMacAddress == null){Console.WriteLine("Timeout, no mac address found for ip of " + ip);} else{Console.WriteLine(ip + " is at: " + arper.Resolve(ip));}}}
}

3.BasicCap

using System;
using System.Collections.Generic;
using SharpPcap;
using SharpPcap.LibPcap;
using SharpPcap.AirPcap;
using SharpPcap.WinPcap;namespace Example3
{/// <summary>/// Basic capture example/// </summary>public class BasicCap{public static void Main(string[] args){// Print SharpPcap versionstring ver = SharpPcap.Version.VersionString;Console.WriteLine("SharpPcap {0}, Example3.BasicCap.cs", ver);// Retrieve the device listvar devices = CaptureDeviceList.Instance;// If no devices were found print an errorif(devices.Count < 1){Console.WriteLine("No devices were found on this machine");return;}Console.WriteLine();Console.WriteLine("The following devices are available on this machine:");Console.WriteLine("----------------------------------------------------");Console.WriteLine();int i = 0;// Print out the devicesforeach(var dev in devices){/* Description */Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);i++;}Console.WriteLine();Console.Write("-- Please choose a device to capture: ");i = int.Parse( Console.ReadLine() );var device = devices[i];// Register our handler function to the 'packet arrival' eventdevice.OnPacketArrival += new PacketArrivalEventHandler( device_OnPacketArrival );// Open the device for capturingint readTimeoutMilliseconds = 1000;if (device is AirPcapDevice){// NOTE: AirPcap devices cannot disable local capturevar airPcap = device as AirPcapDevice;airPcap.Open(SharpPcap.WinPcap.OpenFlags.DataTransferUdp, readTimeoutMilliseconds);}else if(device is WinPcapDevice){var winPcap = device as WinPcapDevice;winPcap.Open(SharpPcap.WinPcap.OpenFlags.DataTransferUdp | SharpPcap.WinPcap.OpenFlags.NoCaptureLocal, readTimeoutMilliseconds);}else if (device is LibPcapLiveDevice){var livePcapDevice = device as LibPcapLiveDevice;livePcapDevice.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);}else{throw new System.InvalidOperationException("unknown device type of " + device.GetType().ToString());}Console.WriteLine();Console.WriteLine("-- Listening on {0} {1}, hit 'Enter' to stop...",device.Name, device.Description);// Start the capturing processdevice.StartCapture();// Wait for 'Enter' from the user.Console.ReadLine();// Stop the capturing processdevice.StopCapture();Console.WriteLine("-- Capture stopped.");// Print out the device statisticsConsole.WriteLine(device.Statistics.ToString());// Close the pcap devicedevice.Close();}/// <summary>/// Prints the time and length of each received packet/// </summary>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());}}
}

4.BasicCapNoCallback

using System;
using System.Collections.Generic;
using SharpPcap;namespace Example4
{/// <summary>/// Basic capture example with no callback/// </summary>public class BasicCapNoCallback{public static void Main(string[] args){// Print SharpPcap versionstring ver = SharpPcap.Version.VersionString;Console.WriteLine("SharpPcap {0}, Example4.BasicCapNoCallback.cs", ver);// Retrieve the device listvar devices = CaptureDeviceList.Instance;// If no devices were found print an errorif(devices.Count < 1){Console.WriteLine("No devices were found on this machine");return;}Console.WriteLine();Console.WriteLine("The following devices are available on this machine:");Console.WriteLine("----------------------------------------------------");Console.WriteLine();int i = 0;// Print out the devicesforeach(var dev in devices){Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);i++;}Console.WriteLine();Console.Write("-- Please choose a device to capture: ");i = int.Parse( Console.ReadLine() );var device = devices[i];// Open the device for capturingint readTimeoutMilliseconds = 1000;device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);Console.WriteLine();Console.WriteLine("-- Listening on {0}...",device.Description);RawCapture packet;// Capture packets using GetNextPacket()while( (packet = device.GetNextPacket()) != null ){// Prints the time and length of each received packetvar time = packet.Timeval.Date;var len = packet.Data.Length;Console.WriteLine("{0}:{1}:{2},{3} Len={4}", time.Hour, time.Minute, time.Second, time.Millisecond, len);}// Print out the device statisticsConsole.WriteLine(device.Statistics.ToString());//Close the pcap devicedevice.Close();Console.WriteLine("-- Timeout elapsed, capture stopped, device closed.");Console.Write("Hit 'Enter' to exit...");Console.ReadLine();}}
}

5.PcapFilter

using System;
using System.Collections.Generic;
using SharpPcap;namespace Example5
{public class PcapFilter{public static void Main(string[] args){// Print SharpPcap versionstring ver = SharpPcap.Version.VersionString;Console.WriteLine("SharpPcap {0}, Example5.PcapFilter.cs\n", ver);// Retrieve the device listvar devices = CaptureDeviceList.Instance;// If no devices were found print an errorif(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("----------------------------------------------------");Console.WriteLine();int i = 0;// Scan the list printing every entryforeach(var dev in devices){Console.WriteLine("{0}) {1}",i,dev.Description);i++;}Console.WriteLine();Console.Write("-- Please choose a device to capture: ");i = int.Parse( Console.ReadLine() );var device = devices[i];//Register our handler function to the 'packet arrival' eventdevice.OnPacketArrival += new PacketArrivalEventHandler( device_OnPacketArrival );//Open the device for capturingint readTimeoutMilliseconds = 1000;device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);// tcpdump filter to capture only TCP/IP packetsstring filter = "ip and tcp";device.Filter = filter;Console.WriteLine();Console.WriteLine("-- The following tcpdump filter will be applied: \"{0}\"", filter);Console.WriteLine("-- Listening on {0}, hit 'Ctrl-C' to exit...",device.Description);// Start capture packetsdevice.Capture();// Close the pcap device// (Note: this line will never be called since//  we're capturing infinite number of packetsdevice.Close();}/// <summary>/// Prints the time and length of each received packet/// </summary>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);}}
}

6.DumpTCP

using System;
using System.Collections.Generic;
using SharpPcap;namespace Example6
{public class DumpTCP{public static void Main(string[] args){string ver = SharpPcap.Version.VersionString;/* Print SharpPcap version */Console.WriteLine("SharpPcap {0}, Example6.DumpTCP.cs", ver);Console.WriteLine();/* Retrieve the device list */var devices = CaptureDeviceList.Instance;/*If no device exists, print error */if(devices.Count<1){Console.WriteLine("No device found on this machine");return;}Console.WriteLine("The following devices are available on this machine:");Console.WriteLine("----------------------------------------------------");Console.WriteLine();int i=0;/* Scan the list printing every entry */foreach(var dev in devices){/* Description */Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);i++;}Console.WriteLine();Console.Write("-- Please choose a device to capture: ");i = int.Parse( Console.ReadLine() );var device = devices[i];//Register our handler function to the 'packet arrival' eventdevice.OnPacketArrival += new PacketArrivalEventHandler( device_OnPacketArrival );// Open the device for capturingint readTimeoutMilliseconds = 1000;device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);//tcpdump filter to capture only TCP/IP packetsstring filter = "ip and tcp";device.Filter = filter;Console.WriteLine();Console.WriteLine("-- The following tcpdump filter will be applied: \"{0}\"", filter);Console.WriteLine("-- Listening on {0}, hit 'Ctrl-C' to exit...",device.Description);// Start capture 'INFINTE' number of packetsdevice.Capture();// Close the pcap device// (Note: this line will never be called since//  we're capturing infinite number of packetsdevice.Close();}/// <summary>/// Prints the time, length, src ip, src port, dst ip and dst port/// for each TCP/IP packet received on the network/// </summary>private static void device_OnPacketArrival(object sender, CaptureEventArgs e){           var time = e.Packet.Timeval.Date;var len = e.Packet.Data.Length;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);}}}
}

【搬运】SharpPcap的一些例子相关推荐

  1. g6-editor 使用

    项目中需要使用G6-Editor.现写个文章记录下. 思路: 到github上面将官网的例子下下来(官网使用react写,项目是使用vue写,但是两者差不多).接着结合官网的例子和api,弄个demo ...

  2. 3种场景下的相关性计算方式,热力图优化展示

    导语:相关系数衡量的是两个变量同时变化的程度和方向,比如身高和体重,体重一般随着身高增加而增加,在很多情况下,我们处理的对象都是连续变量与连续变量之间的关系,但是还有离散变量与离散变量,连续变量与离散 ...

  3. pythonocc view coordinate_pythonOCC例子搬运:4.经典瓶子造型

    这里返回总目录>>返回总目录 core_classic_occ_bottle.py 本例从https://github.com/tpaviot/pythonocc-demos搬运而来 运行 ...

  4. pythonocc_pythonOCC例子搬运:4.经典瓶子造型

    这里返回总目录>>返回总目录 core_classic_occ_bottle.py 本例从https://github.com/tpaviot/pythonocc-demos搬运而来 运行 ...

  5. bsd协议开源框架tcp服务器,搬运RT Thread中BSD Socket实现UDP及TCP例子

    /* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * ...

  6. 使用SharpPCap在C#下进行网络抓包

    转自http://www.cnblogs.com/billmo/archive/2008/11/09/1329972.html 在做大学最后的毕业设计了,无线局域网络远程安全监控策略 那么抓包是这个系 ...

  7. C# 利用SharpPcap实现网络包捕获嗅探

    本文是利用SharpPcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享. 什么是SharpPcap? SharpPcap 是一个.NET 环境下的网络包捕获框架,基于 ...

  8. 将ShaderToy中的Shader搬运到Unity

    一.ShaderToy作品 如果你对 Shader 有一定的了解,那么你或多或少听说过 shaderToy 这个网站,这个网站上有很多令人振奋的 shader 效果,而这些效果有可能只用了几行代码来实 ...

  9. 银行,金融行业的清分,结算,清算,核算到底是什么含义? 现金需要搬运么?

    有广义,狭义之分. 只要团队内部保持一致,互相沟通下,大家说的清核算啥意思就好了. 在狭义上说, 一句话定义:  结算是用户在银行账的处理,清算是机构在央行的账的处理. 1. 结算是银行作为中介,用户 ...

最新文章

  1. java patterncompiler_PatternCompiler是干什么用的?
  2. Leetcode:Intersection of Two Linked Lists
  3. [No0000111]java9环境变量配置bat
  4. 测试双打:模拟,假人和存根
  5. mysql建表的规则_MYSQL建表规则 - Love彼岸花开的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. 【踩坑记录】Tensorflow在Windows下使用
  7. apirestful php自动测试,PHP实现自动识别Restful API的返回内容类型
  8. 运算除法的计算机函数,2、Python基础--除法、常用数学函数(示例代码)
  9. 计算机导论学后感5000字,大学计算机导论论文3000字.docx
  10. 【吐血推荐】技术人员的发展之路
  11. PCB多层板设计总结-层的分布设置
  12. 汉思新材料:无人机控制板BGA芯片底部填充胶应用
  13. js URLEncode函数
  14. c#使用pop3服务器进行邮箱验证
  15. 谈谈如何建立价值驱动的数据战略
  16. 域名被封的表现域名微信不能访问该怎样处理
  17. win10计算机本地无法连接,win10系统电脑本地连接不见了解决方法
  18. 成功解决socket.timeout: The read operation timed out问题
  19. 答读者问:Kafka顺序消费吞吐量下降该如何优化?
  20. 对图像压缩自编码器的理解

热门文章

  1. Ladybug软件打开pgr格式并且输出jpg格式的全景图
  2. 电脑图片合成视频用什么软件?3分钟快速教程,多张图片做成精美视频!
  3. python设置windows桌面壁纸
  4. 因政策原因购房者违约的要如何处理
  5. 利用Python进行数据分析——数据载入、存储及文件格式(7)
  6. 全国大学生智能汽车大赛(一):摄像头识别赛道代码
  7. 安装CodeGear RAD Studio 2007 v11.0.2804.9245 升级至 2852.9797
  8. eclipse下载网址收藏
  9. 深度学习词汇 Developing Our Own Deep Learning Toolset
  10. 有个问题,win10系统,网络诊断,将来会自动连接到jinling,什么意思?