using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Tamir.IPLib;using Tamir.IPLib.Packets;namespace PcapExceptionTest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { /* Retrieve the device list */ PcapDeviceList devices = SharpPcap.GetAllDevices(); /*If no device exists, print error */ if (devices.Count < 1) { MessageBox.Show("No device found on this machine"); return; } PcapDevice device = devices[0]; //Open the device for capturing //true -- means promiscuous mode //1000 -- means a read wait of 1000ms device.PcapOpen(true, 1000); //tcpdump filter to capture only TCP/IP packets //string filter = "host or net 127.0.0.1 and tcp and port 80and len> 50"; string filter = textBox1.Text; //Associate the filter with this capture try { device.PcapSetFilter(filter); } catch (PcapException ex) { textBox2.Text = ex.Message; return; } finally { device.PcapClose(); } textBox2.Text = "Success"; } }}
评论