在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#网络编程 → c#实现网络嗅探器

c#实现网络嗅探器

C#网络编程

下载此实例
  • 开发语言:C#
  • 实例大小:0.04M
  • 下载次数:57
  • 浏览次数:1524
  • 发布时间:2014-06-24
  • 实例类别:C#网络编程
  • 发 布 人:talip1106
  • 文件格式:.zip
  • 所需积分:2
 相关标签: C# 网络 c

实例介绍

【实例简介】

【实例截图】

【核心代码】

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;


namespace netpcap
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{

		ArrayList devlist=null;
		private System.Windows.Forms.Label labAdapters;
		private System.Windows.Forms.Label labDumpFile;
		private System.Windows.Forms.Button butStart;
		private System.Windows.Forms.Button butStartDump;
		private System.Windows.Forms.Button butStop;
		private System.Windows.Forms.ComboBox cbAdapters;
		private System.Windows.Forms.Button butBrowse;
		private System.Windows.Forms.TextBox txtDumpFile;
		private System.Windows.Forms.SaveFileDialog sfd;
		private System.Windows.Forms.Label labPacketCnt;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label labDump;
		private System.Windows.Forms.Button button1;
		long pack_count=0;
		private System.Windows.Forms.Button butSend;
		private System.Windows.Forms.RichTextBox rtb;
		private dotnetWinpCap.ReceivePacket rcvPack=null;

		[DllImport("advapi32.dll")]
		public static extern bool GetUserName( StringBuilder name, ref int size);

		dotnetWinpCap wpcap=null;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;



		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.butStart = new System.Windows.Forms.Button();
            this.butStartDump = new System.Windows.Forms.Button();
            this.cbAdapters = new System.Windows.Forms.ComboBox();
            this.butStop = new System.Windows.Forms.Button();
            this.labAdapters = new System.Windows.Forms.Label();
            this.txtDumpFile = new System.Windows.Forms.TextBox();
            this.labDumpFile = new System.Windows.Forms.Label();
            this.butBrowse = new System.Windows.Forms.Button();
            this.sfd = new System.Windows.Forms.SaveFileDialog();
            this.labPacketCnt = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.labDump = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.butSend = new System.Windows.Forms.Button();
            this.rtb = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // butStart
            // 
            this.butStart.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butStart.Location = new System.Drawing.Point(-17, 103);
            this.butStart.Name = "butStart";
            this.butStart.Size = new System.Drawing.Size(125, 26);
            this.butStart.TabIndex = 0;
            this.butStart.Text = "&Start capture";
            this.butStart.Click  = new System.EventHandler(this.button1_Click);
            // 
            // butStartDump
            // 
            this.butStartDump.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butStartDump.Location = new System.Drawing.Point(396, 103);
            this.butStartDump.Name = "butStartDump";
            this.butStartDump.Size = new System.Drawing.Size(106, 26);
            this.butStartDump.TabIndex = 1;
            this.butStartDump.Text = "Start &dump";
            this.butStartDump.Click  = new System.EventHandler(this.butStartDump_Click);
            // 
            // cbAdapters
            // 
            this.cbAdapters.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.cbAdapters.Location = new System.Drawing.Point(125, 17);
            this.cbAdapters.Name = "cbAdapters";
            this.cbAdapters.Size = new System.Drawing.Size(446, 20);
            this.cbAdapters.TabIndex = 2;
            // 
            // butStop
            // 
            this.butStop.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butStop.Location = new System.Drawing.Point(127, 103);
            this.butStop.Name = "butStop";
            this.butStop.Size = new System.Drawing.Size(125, 26);
            this.butStop.TabIndex = 3;
            this.butStop.Text = "S&top capture";
            this.butStop.Click  = new System.EventHandler(this.butStop_Click);
            // 
            // labAdapters
            // 
            this.labAdapters.Location = new System.Drawing.Point(19, 17);
            this.labAdapters.Name = "labAdapters";
            this.labAdapters.Size = new System.Drawing.Size(67, 17);
            this.labAdapters.TabIndex = 4;
            this.labAdapters.Text = "&Adapters";
            // 
            // txtDumpFile
            // 
            this.txtDumpFile.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtDumpFile.Location = new System.Drawing.Point(125, 52);
            this.txtDumpFile.Name = "txtDumpFile";
            this.txtDumpFile.Size = new System.Drawing.Size(350, 21);
            this.txtDumpFile.TabIndex = 5;
            // 
            // labDumpFile
            // 
            this.labDumpFile.Location = new System.Drawing.Point(19, 52);
            this.labDumpFile.Name = "labDumpFile";
            this.labDumpFile.Size = new System.Drawing.Size(67, 17);
            this.labDumpFile.TabIndex = 7;
            this.labDumpFile.Text = "&Dump file";
            // 
            // butBrowse
            // 
            this.butBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.butBrowse.Location = new System.Drawing.Point(499, 52);
            this.butBrowse.Name = "butBrowse";
            this.butBrowse.Size = new System.Drawing.Size(77, 26);
            this.butBrowse.TabIndex = 6;
            this.butBrowse.Text = "&Browse";
            this.butBrowse.Click  = new System.EventHandler(this.butBrowse_Click);
            // 
            // sfd
            // 
            this.sfd.FileName = "doc1";
            // 
            // labPacketCnt
            // 
            this.labPacketCnt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.labPacketCnt.Location = new System.Drawing.Point(115, 366);
            this.labPacketCnt.Name = "labPacketCnt";
            this.labPacketCnt.Size = new System.Drawing.Size(125, 18);
            this.labPacketCnt.TabIndex = 9;
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label1.Location = new System.Drawing.Point(10, 366);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(86, 18);
            this.label1.TabIndex = 10;
            this.label1.Text = "Packet count";
            // 
            // labDump
            // 
            this.labDump.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.labDump.Location = new System.Drawing.Point(379, 366);
            this.labDump.Name = "labDump";
            this.labDump.Size = new System.Drawing.Size(211, 18);
            this.labDump.TabIndex = 11;
            // 
            // button1
            // 
            this.button1.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.button1.Location = new System.Drawing.Point(521, 103);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(105, 26);
            this.button1.TabIndex = 12;
            this.button1.Text = "St&op dump";
            this.button1.Click  = new System.EventHandler(this.button1_Click_1);
            // 
            // butSend
            // 
            this.butSend.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butSend.Location = new System.Drawing.Point(271, 103);
            this.butSend.Name = "butSend";
            this.butSend.Size = new System.Drawing.Size(106, 26);
            this.butSend.TabIndex = 13;
            this.butSend.Text = "Sen&d stuff";
            this.butSend.Click  = new System.EventHandler(this.butSend_Click);
            // 
            // rtb
            // 
            this.rtb.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.rtb.Location = new System.Drawing.Point(10, 146);
            this.rtb.Name = "rtb";
            this.rtb.ReadOnly = true;
            this.rtb.Size = new System.Drawing.Size(580, 203);
            this.rtb.TabIndex = 14;
            this.rtb.Text = "";
            this.rtb.WordWrap = false;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(600, 391);
            this.Controls.Add(this.rtb);
            this.Controls.Add(this.butSend);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.labDump);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.labPacketCnt);
            this.Controls.Add(this.labDumpFile);
            this.Controls.Add(this.butBrowse);
            this.Controls.Add(this.txtDumpFile);
            this.Controls.Add(this.labAdapters);
            this.Controls.Add(this.butStop);
            this.Controls.Add(this.cbAdapters);
            this.Controls.Add(this.butStartDump);
            this.Controls.Add(this.butStart);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Closing  = new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            this.Load  = new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			if (wpcap==null) 
			{
				wpcap=new dotnetWinpCap();
			}
			if (wpcap.IsOpen) 
			{
				wpcap.Close();
			}
			if (!wpcap.Open( ((Device)devlist[cbAdapters.SelectedIndex]).Name,65536,1,0)) 
			{
				System.Diagnostics.Debug.WriteLine(wpcap.LastError);
				rtb.AppendText(wpcap.LastError "\n");
			}
			else
			{
				System.Diagnostics.Debug.WriteLine("opened successfully: " ((Device)devlist[0]).Name);
				rtb.AppendText("Opened successfully: " ((Device)devlist[cbAdapters.SelectedIndex]).Name "\n");
			}
		
		
			wpcap.SetMinToCopy(100);

			if (rcvPack==null) 
			{
				rcvPack=new dotnetWinpCap.ReceivePacket(this.ReceivePacket);
				wpcap.OnReceivePacket =rcvPack;
			}
			

			wpcap.StartListen();
			rtb.AppendText("Capturing. . .\n");

			
			

		}

		private void DumpEnded(object sender) 
		{
			System.Diagnostics.Debug.WriteLine("livedump ended");
			wpcap.Close();
		}


		private  void ReceivePacket(object sender, PacketHeader p, byte[] s) 
		
		{
			this.pack_count  ;
			rtb.Focus();
			rtb.AppendText("Content of p : \n");
			rtb.AppendText("	Caplength: " p.Caplength "\n");
			rtb.AppendText("	Length   : " p.Length "\n");
			rtb.AppendText("	Timestamp: "  (p.TimeStamp) "\n");
			labPacketCnt.Text=Convert.ToString(this.pack_count);

		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			if (wpcap==null) 
			{

				devlist=dotnetWinpCap.FindAllDevs();
				for (int i=0; i<=devlist.Count-1; i  ) 
				{
					cbAdapters.Items.Add( ((Device)devlist[i]).Name ":" ((Device)devlist[i]).Description);
				}
			}
			if (cbAdapters.Items.Count>0) 
			{
				cbAdapters.SelectedIndex=0;
			}

		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (this.wpcap!=null) 
			{
				wpcap.Close();				
			}

		}

		private void butBrowse_Click(object sender, System.EventArgs e)
		{
			sfd.FileName="";
			if (sfd.ShowDialog(this)==DialogResult.OK) 
			{
				txtDumpFile.Text=sfd.FileName;
			}

		}

		private void butStartDump_Click(object sender, System.EventArgs e)
		{
			if (wpcap!=null) 
			{
				if (wpcap.StartDump(txtDumpFile.Text)==true) 
				{
					labDump.Text="Dumping to " txtDumpFile.Text;
				} 

			}
		}

		private void butStop_Click(object sender, System.EventArgs e)
		{
			if (wpcap!=null) 
			{
				wpcap.StopListen();
				wpcap.Close();
			}
		}

		private void button1_Click_1(object sender, System.EventArgs e)
		{
			if (wpcap!=null) 
			{
				wpcap.StopDump();
				labDump.Text="Dump stopped";
			}
		}

		private void butSend_Click(object sender, System.EventArgs e)
		{
			if (wpcap==null) 
			{
				wpcap=new dotnetWinpCap();
			}
			if (!wpcap.IsOpen) 
			{
				if (!wpcap.Open(((Device)devlist[cbAdapters.SelectedIndex]).Name, 65536, 1,0)) 
				{
					MessageBox.Show(this, "Cannot open adapter " cbAdapters.Text);
					return;
				}
			}

			byte[] b=new byte [10];
			for (int i=0; i<b.Length; i  ) 
			{
				b[i]=(byte)i;
			}
			if (!wpcap.SendPacket(b))
			{
				rtb.AppendText("Send unsuccessful\n");
			} 
			else
			{
				rtb.AppendText("Sent garbage: " b.ToString() "\n");
			}
		}
	}
}

标签: C# 网络 c

实例下载地址

c#实现网络嗅探器

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警