📄 scanfriendip.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 textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label3;
public System.Windows.Forms.ProgressBar progressBar1;
private System.DateTime StartTime ;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.Label label5;
/// <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.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.progressBar1 = 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.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 32);
this.label1.TabIndex = 0;
this.label1.Text = "IP地址前三:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(72, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(72, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "202.115.22";
//
// button1
//
this.button1.Location = new System.Drawing.Point(400, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 64);
this.button1.TabIndex = 4;
this.button1.Text = "扫描";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxUrl_KeyDown);
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 104);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(464, 232);
this.listBox1.TabIndex = 8;
//
// label4
//
this.label4.Location = new System.Drawing.Point(296, 8);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(88, 24);
this.label4.TabIndex = 4;
this.label4.Text = "用时";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(216, 8);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(40, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "0";
//
// label2
//
this.label2.Location = new System.Drawing.Point(176, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 16);
this.label2.TabIndex = 6;
this.label2.Text = "IP起:";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(216, 40);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(40, 21);
this.textBox3.TabIndex = 3;
this.textBox3.Text = "255";
this.textBox3.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxUrl_KeyDown);
//
// label3
//
this.label3.Location = new System.Drawing.Point(176, 48);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(32, 16);
this.label3.TabIndex = 8;
this.label3.Text = "IP终:";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(8, 80);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(464, 16);
this.progressBar1.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 = "退出";
//
// label5
//
this.label5.Location = new System.Drawing.Point(144, 40);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(16, 16);
this.label5.TabIndex = 10;
this.label5.Text = "。";
//
// ScanIP
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 342);
this.Controls.Add(this.label5);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label4);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "ScanIP";
this.Text = "主机名扫描";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxUrl_KeyDown);
this.ResumeLayout(false);
}
#endregion
public class ping
{
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);
listBox1.Items.Add ( sIP + " " + sHostName ) ;
if(progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value++ ;
}
if ( progressBar1.Value == progressBar1.Maximum )
{
//MessageBox.Show ( "成功完成检测!" , "提示" ) ;
DateTime EndTime = DateTime.Now ;
TimeSpan ts = EndTime-StartTime ;
label4.Text = ts.Seconds.ToString ( ) + "秒" ;
//显示扫描计算机所需要的时间
progressBar1.Value = progressBar1.Minimum;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
button1.Enabled=false;
listBox1.Items.Clear();
progressBar1.Minimum=0;
//清楚扫描结果显示区域
StartTime = DateTime.Now ;
//获取当前时间
string mask1 =textBox1.Text;
string mask2 =textBox2.Text;
string mask3 =textBox3.Text;
try
{
progressBar1.Maximum=Convert.ToInt32(mask3)-Convert.ToInt32(mask2);
}
catch
{
MessageBox.Show("请输入正确IP","你懂IP吗?");
button1.Enabled=true;
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
return;
}
int i;
for(i=Convert.ToInt32(mask2,10);i<=Convert.ToInt32(mask3,10);i++)
{
progressBar1.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吗?");
button1.Enabled=true;
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
return;
}
//根据给定的IP地址字符串,处境IPAddress实例
int k =Convert.ToInt32(mask3)-Convert.ToInt32(mask2) ;
ping HostPing = new ping ( ) ;
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 ( "成功完成检测!" , "提示" ) ;
button1.Enabled=true;
}
private void tbxUrl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ( e.KeyData == Keys.Enter && button1.Enabled == true )
{
button1_Click( sender, null );
}
}
private void right(object sender, System.Windows.Forms.KeyEventArgs e)
{
Point pos=new Point(100,100);
contextMenu1.Show(button1,pos);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -