欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

form1.cs

用C#编写ping命令。并应用ICMP及IP协议数据包。
CS
字号:
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 + -