C # using SHARPPCAP to implement the network capture package details

Source: Internet
Author: User
Sharppcap dll Download Address:

Sourceforge.net/directory/os:windows/?q=sharppcap

Detailed usage:

Www.codeproject.com/KB/IP/sharppcap.aspx

To further illustrate how to use this, share a wrapper class that I wrote.

Using system;using system.collections.generic;using system.linq;using system.text;using System.IO;using    System.threading;using sharppcap;using packetdotnet;using sharppcap.libpcap;namespace ServerToolV0._1.Capture{        public class Wincaphelper {private static object syncobj = new Object ();        private static Wincaphelper _capinstance;                public static Wincaphelper Wincapinstance {get {if (null = = _capinstance)                        {Lock (Syncobj) {if (null = = _capinstance)                        {_capinstance = new wincaphelper ();            }}} return _capinstance;        }} private Thread _thread; <summary>//When get Pocket,callback//</summary> public action<string> _lo        Gaction; <summary&gT        Filter keywords///</summary> public string filter; Private Wincaphelper () {} public void Listen () {if (_thread! = null && _t Hread.            IsAlive) {return; } _thread = new Thread (new ThreadStart () = {////Traverse network card foreach (Pc                    Apdevice device in SharpPcap.CaptureDeviceList.Instance) {////start listening separately, specifying the handler function for the package Device.                    Onpacketarrival + = new Packetarrivaleventhandler (device_onpacketarrival); Device.                    Open (Devicemode.normal, 1000); Device.                    Capture (500); Device.                Startcapture ();            }            })); _thread.        Start (); }///<summary>///printing package information, the package is too complex, so directly to the hex string out of///</summary>//<param Nam E= "str" ></param>///<param name= "P" ></param> private void Printpacket (ref string str, Packet p) {if (P! = Nu                LL) {string s = p.tostring (); if (!string.                IsNullOrEmpty (filter) &&!s.contains (filter)) {return;                } str + = "\ r \ n" + S + "\ r \ n";            Try to create a new TCP/IP packet object,////the first parameter is the Ethernet header length, the second is the packet data block str + = P.printhex () + "\ r \ n"; }}///<summary>///Receive packet processing function///</summary>//<param name= "Sender" & gt;</param>//<param name= "E" ></param> private void Device_onpacketarrival (object sender , Captureeventargs e) {////resolves the basic package var packet = PacketDotNet.Packet.ParsePacket (e.packet.link            Layertype, E.packet.data); Protocol category//var dlpacket = PacketDotNet.DataLinkPacket.ParsePacket (e.packet.linklayertYpe, E.packet.data);            var ethernetpacket = PacketDotNet.EthernetPacket.GetEncapsulated (packet); var internetlinkpacket = PacketDotNet.InternetLinkLayerPacket.Parse (packet.            Byteshighperformance.bytes); var internetpacket = PacketDotNet.InternetPacket.Parse (packet.            Byteshighperformance.bytes); var sessionpacket = PacketDotNet.SessionPacket.Parse (packet.            Byteshighperformance.bytes); var apppacket = PacketDotNet.ApplicationPacket.Parse (packet.            Byteshighperformance.bytes); var pppoepacket = PacketDotNet.PPPoEPacket.Parse (packet.            Byteshighperformance.bytes);            var arppacket = PacketDotNet.ARPPacket.GetEncapsulated (packet); var ippacket = PacketDotNet.IpPacket.GetEncapsulated (packet);            IP packet//var udppacket = PacketDotNet.UdpPacket.GetEncapsulated (packet);            var tcppacket = PacketDotNet.TcpPacket.GetEncapsulated (packet);            string ret = ""; Printpacket (ref rET, packet);            Parsepacket (ref ret, ethernetpacket);            Parsepacket (ref ret, internetlinkpacket);            Parsepacket (ref ret, internetpacket);            Parsepacket (ref ret, sessionpacket);            Parsepacket (ref ret, apppacket);            Parsepacket (ref ret, pppoepacket);            Parsepacket (ref ret, arppacket);            Parsepacket (ref ret, ippacket);            Parsepacket (ref ret, udppacket);            Parsepacket (ref ret, tcppacket); if (!string. IsNullOrEmpty (ret)) {string rlt = "\ r \ n Time:" + DateTime.Now.ToLongTimeStrin                G () + "\ r \ n packet: \ r \ n" + ret;            _logaction (RLT); }} public void StopAll () {foreach (Pcapdevice device in SharpPcap.CaptureDeviceList.Inst ance) {if (device.                    Opened) {Thread.Sleep (500); Device. Stopcapture(); } _logaction ("Device:" + device.)            Description + "stoped.\r\n"); } _thread.        Abort (); }    }}

The above is the use of C # SHARPPCAP implementation of network capture details of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.