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

📄 scanip.cs

📁 visual c# 网络编程技术与实践实例包括几个源码
💻 CS
字号:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Net.Sockets ;
using System.Net ;
using System.Threading; 

namespace 五子棋
{
		public delegate void UpdateList ( string sIP , string sHostName )  ;
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class ScanIP : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox ipaddtbx;
		private System.Windows.Forms.Button scanipbtn;
		private System.Windows.Forms.ListBox scandetaillbx;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox beginPorttbx;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox EndPorttbx;
		private System.Windows.Forms.Label label3;
		public System.Windows.Forms.ProgressBar timeprobar;
		private System.DateTime StartTime  ;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.MenuItem menuItem2;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ScanIP()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
            this.label1 = new System.Windows.Forms.Label();
            this.ipaddtbx = new System.Windows.Forms.TextBox();
            this.scanipbtn = new System.Windows.Forms.Button();
            this.scandetaillbx = new System.Windows.Forms.ListBox();
            this.label4 = new System.Windows.Forms.Label();
            this.beginPorttbx = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.EndPorttbx = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.timeprobar = new System.Windows.Forms.ProgressBar();
            this.contextMenu1 = new System.Windows.Forms.ContextMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(12, 10);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(48, 32);
            this.label1.TabIndex = 0;
            this.label1.Text = "网段:";
            // 
            // ipaddtbx
            // 
            this.ipaddtbx.Location = new System.Drawing.Point(54, 9);
            this.ipaddtbx.Name = "ipaddtbx";
            this.ipaddtbx.Size = new System.Drawing.Size(72, 21);
            this.ipaddtbx.TabIndex = 1;
            // 
            // scanipbtn
            // 
            this.scanipbtn.Location = new System.Drawing.Point(396, 8);
            this.scanipbtn.Name = "scanipbtn";
            this.scanipbtn.Size = new System.Drawing.Size(72, 34);
            this.scanipbtn.TabIndex = 4;
            this.scanipbtn.Text = "扫描主机";
            this.scanipbtn.Click += new System.EventHandler(this.button1_Click);
            this.scanipbtn.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxUrl_KeyDown);
            // 
            // scandetaillbx
            // 
            this.scandetaillbx.ItemHeight = 12;
            this.scandetaillbx.Location = new System.Drawing.Point(8, 104);
            this.scandetaillbx.Name = "scandetaillbx";
            this.scandetaillbx.Size = new System.Drawing.Size(464, 232);
            this.scandetaillbx.TabIndex = 8;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(150, 42);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(88, 24);
            this.label4.TabIndex = 4;
            this.label4.Text = "用时";
            // 
            // beginPorttbx
            // 
            this.beginPorttbx.Location = new System.Drawing.Point(188, 8);
            this.beginPorttbx.Name = "beginPorttbx";
            this.beginPorttbx.Size = new System.Drawing.Size(40, 21);
            this.beginPorttbx.TabIndex = 2;
            this.beginPorttbx.Text = "0";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(150, 9);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 16);
            this.label2.TabIndex = 6;
            this.label2.Text = "端口起:";
            // 
            // EndPorttbx
            // 
            this.EndPorttbx.Location = new System.Drawing.Point(282, 8);
            this.EndPorttbx.Name = "EndPorttbx";
            this.EndPorttbx.Size = new System.Drawing.Size(40, 21);
            this.EndPorttbx.TabIndex = 3;
            this.EndPorttbx.Text = "255";
            this.EndPorttbx.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxUrl_KeyDown);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(244, 8);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(32, 16);
            this.label3.TabIndex = 8;
            this.label3.Text = "端口终:";
            // 
            // timeprobar
            // 
            this.timeprobar.Location = new System.Drawing.Point(8, 80);
            this.timeprobar.Name = "timeprobar";
            this.timeprobar.Size = new System.Drawing.Size(464, 16);
            this.timeprobar.TabIndex = 9;
            // 
            // contextMenu1
            // 
            this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1,
            this.menuItem2});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.Text = "停止";
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 1;
            this.menuItem2.Text = "退出";
            // 
            // ScanIP
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(480, 342);
            this.Controls.Add(this.timeprobar);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.EndPorttbx);
            this.Controls.Add(this.beginPorttbx);
            this.Controls.Add(this.ipaddtbx);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.scandetaillbx);
            this.Controls.Add(this.scanipbtn);
            this.Controls.Add(this.label1);
            this.Name = "ScanIP";
            this.Text = "扫描主机";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxUrl_KeyDown);
            this.Load += new System.EventHandler(this.ScanIP_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion
		public class findip
		{
 		 public UpdateList ul; 
 		 public string ip ;
 			//定义一个变量,用以接收传送来的IP地址字符串
 		 public string HostName ;
 		 //定义一个变量,用以向主进展传递对应IP地址是否在线数据
 		 public void scan()
		 {
  		IPAddress myIP = IPAddress.Parse ( ip ) ;
  		try 
  		{
   		IPHostEntry myHost = Dns.GetHostByAddress ( myIP );
   		HostName = myHost.HostName.ToString ( ) ;
  		}
  		catch 
  		{
   		HostName = "" ;
  		}
  		if (HostName == "")
   		HostName = " 主机没有响应!";
  		if ( ul != null)
   			ul ( ip , HostName );
		 }
 			//定义一个过程(也可以看出为方法),用以判断传送来的IP地址对应计算机是否在线
		}

		void UpdateMyList ( string sIP , string sHostName )
		{
 			//lock(textBox1);
 		   scandetaillbx.Items.Add ( sIP + " " + sHostName ) ;
 			 if(timeprobar.Value != timeprobar.Maximum)
			 {
   		 timeprobar.Value++ ;
			 }
  		if ( timeprobar.Value == timeprobar.Maximum )
  		{
   		//MessageBox.Show ( "成功完成检测!" , "提示" ) ;
    		DateTime EndTime = DateTime.Now ;
   		TimeSpan ts = EndTime-StartTime ; 
   		label4.Text = ts.Seconds.ToString ( ) + "秒" ;
   		 //显示扫描计算机所需要的时间
   		timeprobar.Value = timeprobar.Minimum;
		}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			scanipbtn.Enabled=false;
			scandetaillbx.Items.Clear();
			timeprobar.Minimum=0;
			//清楚扫描结果显示区域
           StartTime = DateTime.Now ;
			//获取当前时间
           string mask1 =ipaddtbx.Text;
			string mask2 =beginPorttbx.Text;
           string mask3 =EndPorttbx.Text;
			try
			{
				timeprobar.Maximum=Convert.ToInt32(mask3)-Convert.ToInt32(mask2);
			}
			catch
			{
				MessageBox.Show("请输入正确IP","你懂IP吗?");
				scanipbtn.Enabled=true;
				ipaddtbx.Text="";
				beginPorttbx.Text="";
				EndPorttbx.Text="";
				return;
			}
			int i;
			for(i=Convert.ToInt32(mask2,10);i<=Convert.ToInt32(mask3,10);i++)
			{
 			timeprobar.Value=i-Convert.ToInt32(mask2,10);
  	    string ip= mask1.ToString()+"."+i.ToString() ;
				IPAddress myIP=IPAddress.Parse("202.115.22.136");
          try
	        {
		        myIP = IPAddress.Parse ( ip ) ;
 	        }
	        catch
	        {
		   	MessageBox.Show("IP有误","你懂IP吗?");
		        scanipbtn.Enabled=true;
		        ipaddtbx.Text="";
		        beginPorttbx.Text="";
		        EndPorttbx.Text="";
		        return;
	        }
				//根据给定的IP地址字符串,处境IPAddress实例
				int k =Convert.ToInt32(mask3)-Convert.ToInt32(mask2) ;
              findip HostPing = new findip ( ) ;
				Thread[] mythread = new Thread [k+1] ;
              //创建一个ping实例
              HostPing.ip =ip ;
              HostPing.ul = new UpdateList ( UpdateMyList ) ;
              //向这个ping实例中传递IP地址字符串
              mythread[k] = new Thread ( new ThreadStart ( HostPing.scan ) ) ;
              //初始化一个线程实例
              mythread[k].Start ( ) ;
              //启动线程

			}
      
		
			//扫描给定IP地址对应的计算机是否在线
           
			
			DateTime EndTime = DateTime.Now ;
            TimeSpan ts = EndTime-StartTime ;
			//获得扫描网络计算机所使用的时间
		
           //listBox1.Items.Add( ts.Seconds.ToString()+ "秒");
			label4.Text="用时"+ts.Seconds.ToString()+"秒";
           //MessageBox.Show ( "成功完成检测!" , "提示" ) ;
			scanipbtn.Enabled=true;
		
		}
		private void tbxUrl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if ( e.KeyData == Keys.Enter && scanipbtn.Enabled == true )
			{
				button1_Click( sender, null );
			}
		}
		private void right(object sender, System.Windows.Forms.KeyEventArgs e)
		{
				Point pos=new Point(100,100);
			contextMenu1.Show(scanipbtn,pos);
		}

        private void ScanIP_Load(object sender, EventArgs e)
        {

        }
	}
}

⌨️ 快捷键说明

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