C# 获得局域网主机列表实例

None.gif     using  System;
None.gif  
using  System.Drawing;
None.gif  
using  System.Collections;
None.gif  
using  System.ComponentModel;
None.gif  
using  System.Windows.Forms;
None.gif  
using  System.Data;
None.gif  
using  System.Net;
None.gif  
using  System.Threading;
None.gif  
None.gif  
namespace  WindowLanSearch
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
private System.Windows.Forms.TextBox textBox1;
InBlock.gif  
private System.Windows.Forms.Button button1;
InBlock.gif  
private string[,] LanHost;
InBlock.gif  
private System.Windows.Forms.ProgressBar progressBarSearch;
InBlock.gif  
private Thread[] thread;
InBlock.gif  
private System.Windows.Forms.ListView listView1;
InBlock.gif  
private System.Windows.Forms.ColumnHeader columnHeader1;
InBlock.gif  
private System.Windows.Forms.ColumnHeader columnHeader2;
InBlock.gif  
private string str;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private System.ComponentModel.Container components = null;
InBlock.gif  
InBlock.gif  
public Form1() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
//
InBlock.gif  
// Windows 窗体设计器支持所必需的
InBlock.gif  
//
InBlock.gif
  InitializeComponent();
InBlock.gif  InitLanHost();
InBlock.gif  progressBarSearch.Maximum 
= 255;
InBlock.gif  
InBlock.gif  
//
InBlock.gif  
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif  
//
ExpandedSubBlockEnd.gif
  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 数组初始化
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void InitLanHost()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  LanHost 
= new string[255,2];
InBlock.gif  
for (int i=0;i<255;i )
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  LanHost[i,
0= "";
InBlock.gif  LanHost[i,
1= "";
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
if (components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  components.Dispose();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
base.Dispose( disposing );
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ContractedSubBlock.gifExpandedSubBlockStart.gif  
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif  
/// 此方法的内容。
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void InitializeComponent() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
this.textBox1 = new System.Windows.Forms.TextBox();
InBlock.gif  
this.button1 = new System.Windows.Forms.Button();
InBlock.gif  
this.progressBarSearch = new System.Windows.Forms.ProgressBar();
InBlock.gif  
this.listView1 = new System.Windows.Forms.ListView();
InBlock.gif  
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
InBlock.gif  
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
InBlock.gif  
this.SuspendLayout();
InBlock.gif  
//
InBlock.gif  
// textBox1
InBlock.gif  
//
InBlock.gif
  this.textBox1.Location = new System.Drawing.Point(2440);
InBlock.gif  
this.textBox1.Multiline = true;
InBlock.gif  
this.textBox1.Name = "textBox1";
InBlock.gif  
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
InBlock.gif  
this.textBox1.Size = new System.Drawing.Size(176296);
InBlock.gif  
this.textBox1.TabIndex = 0;
InBlock.gif  
this.textBox1.Text = "";
InBlock.gif  
//
InBlock.gif  
// button1
InBlock.gif  
//
InBlock.gif
  this.button1.Location = new System.Drawing.Point(45640);
InBlock.gif  
this.button1.Name = "button1";
InBlock.gif  
this.button1.TabIndex = 1;
InBlock.gif  
this.button1.Text = "开始搜索"; bbs.2168.net国内最早的网管论坛 
InBlock.gif  
this.button1.Click = new System.EventHandler(this.button1_Click);
InBlock.gif  
//
InBlock.gif  
// progressBarSearch
InBlock.gif  
//
InBlock.gif
  this.progressBarSearch.Location = new System.Drawing.Point(32360);
InBlock.gif  
this.progressBarSearch.Name = "progressBarSearch";
InBlock.gif  
this.progressBarSearch.Size = new System.Drawing.Size(49024);
InBlock.gif  
this.progressBarSearch.TabIndex = 2;
InBlock.gif  
//
InBlock.gif  
// listView1
InBlock.gif  
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
  this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] dot.gif{
InBlock.gif  
this.columnHeader1,
ExpandedSubBlockEnd.gif  
this.columnHeader2}
);
InBlock.gif  
this.listView1.Location = new System.Drawing.Point(24840);
InBlock.gif  
this.listView1.Name = "listView1";
InBlock.gif  
this.listView1.Size = new System.Drawing.Size(184288);
InBlock.gif  
this.listView1.TabIndex = 5;
InBlock.gif  
//
InBlock.gif  
// columnHeader1
InBlock.gif  
//
InBlock.gif
  this.columnHeader1.Text = "dddd";
InBlock.gif  
//
InBlock.gif  
// columnHeader2
InBlock.gif  
//
InBlock.gif
  this.columnHeader2.Text = "sssss";
InBlock.gif  
//
InBlock.gif  
// Form1
InBlock.gif  
//
InBlock.gif
  this.AutoScaleBaseSize = new System.Drawing.Size(614); 
InBlock.gif  
this.ClientSize = new System.Drawing.Size(544413);
InBlock.gif  
this.Controls.Add(this.listView1);
InBlock.gif  
this.Controls.Add(this.progressBarSearch);
InBlock.gif  
this.Controls.Add(this.button1);
InBlock.gif  
this.Controls.Add(this.textBox1);
InBlock.gif  
this.Name = "Form1";
InBlock.gif  
this.Text = "Form1";
InBlock.gif  
this.ResumeLayout(false);
InBlock.gif  
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  [STAThread]
InBlock.gif  
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  Application.Run(
new Form1());
ExpandedSubBlockEnd.gif  }

InBlock.gif  
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
InBlock.gif  LanSearch();
InBlock.gif  
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 局域网搜索事件
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void LanSearch()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  thread 
= new Thread[255];
InBlock.gif  
InBlock.gif  ThreadStart threadMethod;
InBlock.gif  
InBlock.gif  Thread threadProgress 
= new Thread(new ThreadStart(progressSearch));
InBlock.gif  threadProgress.Start();
InBlock.gif  
InBlock.gif  
string localhost = (Dns.GetHostByName(Dns.GetHostName())).AddressList[0].ToString(); //本地主机IP地址 
InBlock.gif
  str = localhost.Substring(0,localhost.LastIndexOf("."));
InBlock.gif  
InBlock.gif  
for (int i=0;i<255;i ) //建立255个线程扫描IP
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{
InBlock.gif  threadMethod 
= new ThreadStart(LanSearchThreadMethod);
InBlock.gif  thread[i] 
= new Thread(threadMethod);
InBlock.gif  thread[i].Name 
= i.ToString();
InBlock.gif  thread[i].Start();
InBlock.gif  
if (!thread[i].Join(100))  //Thread.Join(100)不知道这处这么用对不对,感觉没什么效果一样
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{
InBlock.gif  thread[i].Abort();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  GetLanHost();
InBlock.gif  listLanHost();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 多线程搜索方法
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void LanSearchThreadMethod()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
int Currently_i = Convert.ToUInt16(Thread.CurrentThread.Name); //当前进程名称
InBlock.gif
  
InBlock.gif  IPAddress ScanIP 
= IPAddress.Parse( str "." Convert.ToString(Currently_i 1)); //获得扫描IP地址
InBlock.gif
  IPHostEntry ScanHost = null;
InBlock.gif  ScanHost 
= Dns.GetHostByAddress(ScanIP);  //获得扫描IP地址主机信息
InBlock.gif
  
InBlock.gif  
if (ScanHost != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  LanHost[Currently_i,
0= ScanIP.ToString(); 
InBlock.gif  LanHost[Currently_i,
1= ScanHost.HostName;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
//progressBarSearch.Value = progressBarSearch.Value 1;
InBlock.gif
  
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 文本框显示主机名与IP列表
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void GetLanHost()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
for (int i=0;i<255;i )
InBlock.gif  
if ( LanHost[i,0!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  textBox1.Text 
=textBox1.Text LanHost[i,1":" LanHost[i,0"\r\n";
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// listview1 显示搜索主机
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void listLanHost()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  listView1.View 
= View.List;
InBlock.gif  
InBlock.gif  ListViewItem aa ;
InBlock.gif  
for (int i=0;i<255;i )
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
if ( LanHost[i,0!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  aa
= new ListViewItem();
InBlock.gif  aa.Text 
= LanHost[i,1];
InBlock.gif  aa.Tag 
= LanHost[i,0];
InBlock.gif  listView1.Items.Add(aa);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 进度条处理线程
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void progressSearch()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  
//label1.Text = "进度条只是时间估计,不是真实搜索进度!"; 
InBlock.gif
  progressBarSearch.Value = 0;
InBlock.gif  
for (int i=0;i<255;i )
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  progressBarSearch.Value 
= progressBarSearch.Value 1;
InBlock.gif  Thread.Sleep(
100);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  }

None.gif  
None.gif  遗憾之处:因搜索较慢,没有实现真实的搜索进度。
None.gif  不懂之处:实现文字提示时,当在鼠标事件首尾插入
None.gif  
private   void  button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif  lab1.Text 
= “开始搜索”;   //新插入
InBlock.gif
  LanSearch();
InBlock.gif  lab1.Text 
= “结束搜索”;   //新插入
ExpandedBlockEnd.gif
  }

None.gif

  文本提示时,在lab1上始终不能及时显示,而是等所有线程结束后才显示“结束搜索“。



现在开始接触扫描远程计算机部分的代码。感觉c#在网络编程方面是简单而强大的,主要用namespace的System.Net和System.Net.Sockets,这两个namespace中包含丰富的类可以开发多种网络应用程序。

要使用这两个类,首先要加上对这两个类的引用:

None.gif using  System.Net;
None.gif
using  System.Net.Sockets;

而通过IP获得计算机名则是用以下语句:

None.gif IPAddress myIP = IPAddress.Parse(sIP); // 把IP字符串通过Parse转换为IP地址实例
None.gif

None.gif     IPHostEntry myHost
= Dns.GetHostByAddress(myIP); // 用GetHostByAddress方法根据IP获得主机名
None.gif
     sHostName = myHost.HostName.ToString(); // 将计算机名传给我自己定放的字符串变量

通过以上简单语句就能根据IP获得远程主机的计算机名。

转载于:https://www.cnblogs.com/suchenge/articles/888802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值