日期:2014-05-18 浏览次数:21103 次
public class Device:IDisposable { public Device() { //.Devices(System.Net.IPAddress.Any, 0, new Win.RemoteAuthentication(Win.AuthenticationTypes.Null, "", "")); } public void Run(SharpPcap.PacketArrivalEventHandler packetArrival,string filter) { OriDevice = OriDevices[1]; if (filter == string.Empty) { filter = "ip and tcp"; } OriDevice.OnPacketArrival += new PacketArrivalEventHandler(packetArrival);//设置异步拦截事件 OriDevice.OnPacketArrival += new PacketArrivalEventHandler(OriDevice_OnPacketArrival); OriDevice.Open(SharpPcap.DeviceMode.Promiscuous,0);//打开设备 OriDevice.Filter = filter;//设置拦截过滤器 OriDevice.Mode = CaptureMode.Packets; //OriDevice.LinkType OriDevice.StartCapture();//开始拦截 //OriDevice.StopCapture();//开始拦截 //OriDevice.Close(); } void OriDevice_OnPacketArrival(object sender, CaptureEventArgs e) { System.Diagnostics.Debug.Write("OK"); } public WinPcapDeviceList OriDevices { get { return WinPcapDeviceList.Instance; } } public void Stop() { if(OriDevice!=null) OriDevice.StopCapture(); } public void Close() { if (OriDevice != null) { OriDevice.StopCapture(); OriDevice.Close(); } } public WinPcapDevice OriDevice { get; set; } #region IDisposable 成员 public void Dispose() { Close(); OriDevice = null; }