Transferred from: http://hi.baidu.com/boyxgb/blog/item/89ac86fbdff5f82c4e4aea2e.html
Due to the need of the project, to obtain the terminal hardware status from the terminal and server communication data, so the use of the widely circulated C # grab Sharppcap library.
Sharppcap is currently the latest version of 3.1.0, based on. Net 3.5 and WinPcap. Please note that if the version you use is too different from my version, there is no need to waste time reading this article. For example, you are using the old version of. Net 2.0, the library is completely different, please search Sharppcap, the old version of Sharppcap article is very much, or you use the latest version, Please refer directly to the contents of the examples in the latest version of the Sharppcap download website in the source package.
First need to install winpcap,:http://www.winpcap.org/install/default.htm before use
sharppcap:http://sourceforge.net/projects/sharppcap/, download Sharppcap DLL Library package Sharppcap-3.1.0.bin.zip, you can also find the corresponding source package Sharppcap-3.1.0.src.zip and Sharppcap historical versions in files.
Sharppcap library download, directly in the project reference SharpPcap.dll and PacketDotNet.dll can be used.
The following is a sample of the Sharppcap I have compiled, in fact, that is, the source package examples in the official example of what I used to integrate the content (not including ARP, dumpfile and multiplefilters):
Code
using System;
using System.Collections.Generic;
Using system. LINQ;; / / you need to add a reference to system. Core (you can find it in the. Net item by right clicking the reference addition)
using System.Text;
Using SharpPcap; / / you need to add references to sharppcap.dll and packetdotnet.dll
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
//Show SharpPcap version
string ver = SharpPcap.Version.VersionString;
Console.WriteLine("SharpPcap {0}", ver);
//Get network devices
LivePcapDeviceList devices = LivePcapDeviceList.Instance;
if (devices.Count < 1)
{
Console.writeline ("network device not found");
Return;
}
Console.WriteLine();
Console.writeline ("the following are currently the active network devices on this computer:");
Console.WriteLine("----------------------------------------------------");
Console.WriteLine();
Int i = 0;
foreach (LivePcapDevice dev in devices)
{
Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
I++;
}
//Select network device to listen to
Console.WriteLine();
Console. Write ("-- please select a network device to listen to:");
i = int.Parse(Console.ReadLine());
LivePcapDevice device = devices[i];
Console. Write ("-- please select operation: Monitor Communication [C / C], multithread monitor communication [T / T], monitor statistics [f / F], send random packets [S / S]?");
string resp = Console.ReadLine().ToUpper();
while (!(resp.StartsWith("C") || resp.StartsWith("F") || resp.StartsWith("T") || resp.StartsWith("S")))
{
resp = Console.ReadLine().ToUpper();
}
Try
{
if (resp.StartsWith("C") || resp.StartsWith("F") || resp.StartsWith("T"))
{
//Monitor filter conditions
string filter = "ip and tcp";
//Connecting devices
System.Threading.Thread backgroundThread = null;
int readTimeoutMilliseconds = 1000;
if (resp.StartsWith("F"))
{
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
device.SetFilter(filter);
Device.mode = capturemode. Statistics; / / packet capturing statistics
Device. Onpcapstatistics + = new statisticsmode EventHandler (device_onpcapstatistics); / / packet capturing statistics callback events
}
else if (resp.StartsWith("C"))
{
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
device.SetFilter(filter);
Device.mode = capturemode.packets; / / grab packets
Showdetails = resp.endswith ("- a"); / / when grabbing packets, check whether you want to view the details
Device. Onpacketarrival + = new packetarrivaleventhandler (device_onpacketarrival); / / grab packet callback event
}
Else
{
backgroundThread = new System.Threading.Thread(BackgroundThread);
backgroundThread.Start();
device.Open();
device.SetFilter(filter);
Device.mode = capturemode.packets; / / grab packets
Showdetails = resp.endswith ("- a"); / / when grabbing packets, check whether you want to view the details
Device. Onpacketarrival + = new packetarrivaleventhandler (device "onthreadpacketarrival); / / grab packet callback event
}
Console.WriteLine();
Console. Writeline ("-- current tcpdump filter condition: \" {0} \ "", filter);
Console. Writeline ("-- listening to device {0}, press enter to stop listening...", device. Description);
//Start monitoring
device.StartCapture();
//Stop listening
Console.ReadLine();
device.StopCapture();
Console. Writeline ("-- stop listening.");
if (backgroundThread != null)
{
BackgroundThreadStop = true;
backgroundThread.Join();
}
}
else if (resp.StartsWith("S"))
{
//Connecting devices
device.Open();
//Generate random packets
byte[] bytes = GetRandomPacket();
Try
{
//Send data
device.SendPacket(bytes);
SendQueue squeue = new SendQueue(2000);
Console. Writeline ("-- single packet sent successfully.");
for (int j = 0; j < 10; j++)
{
if (!squeue.Add(bytes))
{
Console. Writeline ("-- warning: the queue size is not enough to store all packets, only some packets will be sent.");
Break;
}
}
device.SendQueue(squeue, SendQueueTransmitModes.Synchronized);
Console. Writeline ("-- packet queue sending completed.");
}
catch (Exception e)
{
Console.WriteLine("-- " + e.Message);
}
}
}
catch (Exception e)
{
Console.WriteLine("-- " + e.Message);
}
Finally
{
if (device.Opened)
{
//Disconnect device
Console.WriteLine(device.Statistics().ToString());
device.Close();
Console. Writeline ("-- disconnect device.");
Console. Write ("press enter to exit...);
Console.Read();
}
}
}
Sharppcap use of the network packet capture framework-the instance code is passed through the VS2005 debug