⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs.bak

📁 用C#编写ping命令。并应用ICMP及IP协议数据包。
💻 BAK
字号:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Net ;
using System.Net.Sockets ;
namespace WindowsApplication23
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1 ;
		private System.Windows.Forms.Label label1 ;
		private System.Windows.Forms.ListBox listBox1 ;
		private System.Windows.Forms.Button button1 ;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null ;
		public Form1 ( )
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent ( ) ;
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose (  bool disposing  )
		{
			if (  disposing  )
			{
				if ( components != null ) 
				{
					components.Dispose( ) ;
				}
			}
			base.Dispose (  disposing  ) ;
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent ( )
		{
			this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
			this.label1 = new System.Windows.Forms.Label ( ) ;
			this.listBox1 = new System.Windows.Forms.ListBox ( ) ;
			this.button1 = new System.Windows.Forms.Button ( ) ;
			this.SuspendLayout ( ) ;
			this.textBox1.Location = new System.Drawing.Point ( 116 , 14 ) ;
			this.textBox1.Name = "textBox1" ;
			this.textBox1.Size = new System.Drawing.Size ( 148 , 21 ) ;
			this.textBox1.TabIndex = 0 ;
			this.textBox1.Text = "" ;
			this.textBox1.TextChanged +=  new System.EventHandler ( this.textBox1_TextChanged ) ;
			this.label1.Location = new System.Drawing.Point ( 12 , 14 ) ;
			this.label1.Name = "label1" ;
			this.label1.TabIndex = 1 ;
			this.label1.Text = "请输入主机名:" ;
			this.listBox1.BackColor = System.Drawing.SystemColors.WindowText ;
			this.listBox1.ForeColor = System.Drawing.SystemColors.Window ;
			this.listBox1.ItemHeight = 12 ;
			this.listBox1.Location = new System.Drawing.Point ( 6 , 42 ) ;
			this.listBox1.Name = "listBox1" ;
			this.listBox1.Size = new System.Drawing.Size ( 400 , 280 ) ;
			this.listBox1.TabIndex = 2 ;
			this.button1.Location = new System.Drawing.Point ( 274 , 12 ) ;
			this.button1.Name = "button1" ;
			this.button1.TabIndex = 3 ;
			this.button1.Text = "Ping" ;
			this.button1.Click +=  new System.EventHandler ( this.button1_Click ) ;
			this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
			this.ClientSize = new System.Drawing.Size ( 410 , 333 ) ;
			this.Controls.AddRange ( new System.Windows.Forms.Control[ ] {
				  this.button1 ,
				  this.listBox1 ,
				  this.label1 ,
				  this.textBox1 } ) ;
			this.MaximizeBox = false ;
			this.Name = "Form1" ;
			this.Text = "Visual C#实现Ping" ;
			this.ResumeLayout ( false ) ;
		}
		#endregion
		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[ STAThread ]
		static void Main ( ) 
		{
			Application.Run ( new Form1 ( ) ) ;
		}
		private void textBox1_TextChanged ( object sender , System.EventArgs e )
		{
		
		}
		private void button1_Click ( object sender , System.EventArgs e )
		{
			listBox1.Items.Clear ( ) ;
			string hostclient = textBox1.Text  ;
			int k ;
			for  ( k = 0 ; k < 3 ; k++ )
			{
				Socket socket = new Socket ( AddressFamily.InterNetwork , SocketType.Raw , ProtocolType.Icmp ) ;
				IPHostEntry hostInfo ;
				try
				{
					//解析主机IP入口
					hostInfo = Dns.GetHostByName ( hostclient ) ;  
				}
				catch ( Exception )
				{
					//解析主机名错误。
					listBox1.Items.Add ( "没有发现此主机!" ) ; 
					return  ;
				}
				// 取服务器端主机的30号端口
				EndPoint hostPoint =   ( EndPoint ) new IPEndPoint ( hostInfo.AddressList[ 0 ] , 30 ) ;  
				IPHostEntry clientInfo ;
				clientInfo  = Dns.GetHostByName ( hostclient ) ;
				// 取客户机端主机的30端口
				EndPoint clientPoint =  ( EndPoint ) new IPEndPoint ( clientInfo.AddressList[ 0 ] , 30 ) ;    
				//设置ICMP报文
				int DataSize = 4 ; // ICMP数据包大小 ;
				int PacketSize = DataSize + 8 ;//总报文长度
				const int ICMP_ECHO = 8 ;
				IcmpPacket packet = new IcmpPacket ( ICMP_ECHO , 0 , 0 , 45 , 0 , DataSize ) ;
         
				Byte [ ] Buffer = new Byte [  PacketSize  ] ; 
				int index = packet.CountByte ( Buffer ) ;
				//报文出错
				if (  index !=  PacketSize )
				{
					listBox1.Items.Add ( "报文出现问题!" ) ;
					return  ;
				}
				int cksum_buffer_length = ( int ) Math.Ceiling (  (  ( Double )index )/ 2 ) ;
				UInt16 [ ] cksum_buffer = new UInt16 [ cksum_buffer_length ] ;
				int icmp_header_buffer_index = 0 ;
				for (  int i = 0 ; i < cksum_buffer_length ; i++  ) 
				{
					//将两个byte转化为一个UInt16
					cksum_buffer[ i ] = BitConverter.ToUInt16 ( Buffer , icmp_header_buffer_index ) ;
					icmp_header_buffer_index +=  2 ;
				}
				//将校验和保存至报文里
				packet.CheckSum  = IcmpPacket.SumOfCheck ( cksum_buffer ) ;
				// 保存校验和后,再次将报文转化为数据包
				Byte [ ] SendData = new Byte [  PacketSize  ] ; 
				index =  packet.CountByte ( SendData ) ;
				//报文出错
				if (  index !=  PacketSize )
				{
					listBox1.Items.Add ( "报文出现问题!" ) ;
					return  ;
				}
				int nBytes = 0 ;
				//系统计时开始
				int startTime = Environment.TickCount ;
				//发送数据包
				if  (  ( nBytes = socket.SendTo ( SendData , PacketSize , SocketFlags.None , hostPoint ) ) ==  -1 ) 
				{    
					listBox1.Items.Add ( "无法传送报文!" ) ;
				}
				Byte [ ] ReceiveData = new Byte[ 256 ] ;   //接收数据
				nBytes = 0 ;
				int timeout = 0  ;
				int timeConsume = 0 ;
				while ( true )
				{
					nBytes = socket.ReceiveFrom ( ReceiveData , 256 , SocketFlags.None , ref clientPoint ) ;
					if  ( nBytes ==  -1 ) 
					{
						listBox1.Items.Add ( "主机没有响应!" )  ;
						break ;
					}
					else if ( nBytes > 0 )
					{
						timeConsume = System.Environment.TickCount - startTime ; 
						//得到发送报文到接收报文之间花费的时间
						listBox1.Items.Add  (  "Reply from " + hostInfo.AddressList[ 0 ].ToString ( ) + " in "
							+ timeConsume + "MS :Bytes Received " + nBytes  )  ;
					
						break ;
					}
					timeConsume = Environment.TickCount - startTime ;
					if ( timeout > 1000 )
					{
						listBox1.Items.Add ( "Time Out" )  ;
						break ;
					}
				}
				//关闭套接字
				socket.Close ( ) ;   
			}
		}
		public class IcmpPacket 
		{ 
			private Byte _type ;      // 类型
			private Byte _subCode ;    // 代码
			private UInt16 _checkSum ;    // 校验和
			private UInt16 _identifier ;   // 识别符
			private UInt16 _sequenceNumber ;   // 序列号 
			private Byte [ ] _data ;     //选项数据
			//初始化ICMP报文
			public IcmpPacket ( Byte type ,Byte subCode ,UInt16 checkSum ,UInt16 identifier ,UInt16 sequenceNumber ,int dataSize )
			{
				_type = type ;
				_subCode = subCode ;
				_checkSum = checkSum ;
				_identifier = identifier ;
				_sequenceNumber = sequenceNumber ;
				_data = new Byte[ dataSize ] ;
				//在选项数据中,写入指定的数据大小
				for  ( int i = 0 ; i < dataSize ; i++ )
				{
					//由于选项数据在此命令中并不重要,所以你可以改换任何你喜欢的字符	
					_data[ i ] =  ( byte )'x' ;
				}
			} 
			public UInt16 CheckSum
			{
				get
				{
					return _checkSum ;
				}
				set
				{
					_checkSum = value ;
				}
			}
			//得到将整个ICMP报文信息和数据转化为Byte时的Byte数目,主要是用来判断报文的完整性
			public int CountByte ( Byte[ ] buffer )
			{

				Byte [ ] b_type = new Byte[ 1 ]{_type} ;
				Byte [ ] b_code = new Byte[ 1 ]{_subCode} ;
				Byte [ ] b_cksum = BitConverter.GetBytes ( _checkSum ) ;
				Byte [ ] b_id = BitConverter.GetBytes ( _identifier ) ;
				Byte [ ] b_seq = BitConverter.GetBytes ( _sequenceNumber ) ;
				int i = 0 ;
				Array.Copy (  b_type , 0 , buffer , i , b_type.Length  ) ;
				i+=  b_type.Length ;
				Array.Copy (  b_code , 0 , buffer , i , b_code.Length  ) ;
				i+=  b_code.Length ;
				Array.Copy (  b_cksum , 0 , buffer , i , b_cksum.Length  ) ;
				i +=  b_cksum.Length ;
				Array.Copy (  b_id , 0 , buffer , i , b_id.Length  ) ;
				i+=  b_id.Length ;
				Array.Copy (  b_seq , 0 , buffer , i , b_seq.Length  ) ;
				i +=  b_seq.Length ;
				Array.Copy ( _data , 0 , buffer , i , _data.Length  ) ;
				i +=  _data.Length ;
				return i ;
			}
			public static UInt16 SumOfCheck (  UInt16 [ ] buffer  )
			{
				int cksum = 0 ;
				for ( int i = 0 ;i<buffer.Length ;i++ )
					cksum +=   ( int ) buffer[ i ] ;
				cksum =  ( cksum >> 16 ) +  ( cksum & 0xffff ) ;
				cksum +=   ( cksum >> 16 ) ;
				return  ( UInt16 ) ( ~cksum ) ;
			} 
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -