這篇文章主要講解了C#如何實(shí)現(xiàn)抓包,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
工具:SharpPcap 4.2.0
vs工程:控制臺(tái)應(yīng)用程序
關(guān)于C#抓包,我只找到SharpPcap 這個(gè)dll,相關(guān)的資料不多,而且都是挺老的,所以就順手記一下自己的代碼,給有同樣需求的人一個(gè)參考吧。
當(dāng)然,代碼可能存在問(wèn)題,請(qǐng)見(jiàn)諒。
一、獲取連接設(shè)備
// 獲取連接列表 CaptureDeviceList devices = CaptureDeviceList.Instance; // 無(wú)連接 if (devices.Count < 1) { Console.WriteLine("No devices were found on this machine"); return; } Console.WriteLine("\n以下為本機(jī)連接:"); Console.WriteLine("--------------\n"); int j=0; string temp = ""; Regex r = new Regex("FriendlyName: .*\n"); //匹配連接的FriendlyName Match m; // 打印連接設(shè)備 foreach (ICaptureDevice dev in devices) { temp = dev.ToString(); m = r.Match(temp); Console.WriteLine("{0}:{1}\n", j++, m.ToString()); } Console.Write("輸入設(shè)備號(hào)"); string input = Console.ReadLine(); int i = 0; try { i = Int32.Parse(input); } catch (Exception e) { Console.WriteLine("非法輸入!"+e.Message); return; } if (devices.Count < 1 || i == -1) { Console.WriteLine("變量非法!"); return; } // 得到指定連接設(shè)備 ICaptureDevice device = devices[i];