c# 获得局域网主机列表实例
程序员文章站
2022-06-08 23:09:57
using system; using system.drawing; using system.collections; using&nbs...
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.net;
using system.threading;
namespace windowlansearch
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.textbox textbox1;
private system.windows.forms.button button1;
private string[,] lanhost;
private system.windows.forms.progressbar progressbarsearch;
private thread[] thread;
private system.windows.forms.listview listview1;
private system.windows.forms.columnheader columnheader1;
private system.windows.forms.columnheader columnheader2;
private string str;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
initlanhost();
progressbarsearch.maximum = 255;
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 数组初始化
/// </summary>
private void initlanhost()
{
lanhost = new string[255,2];
for (int i=0;i<255;i++)
{
lanhost[i,0] = "";
lanhost[i,1] = "";
}
}
/// <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.textbox1 = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.progressbarsearch = new system.windows.forms.progressbar();
this.listview1 = new system.windows.forms.listview();
this.columnheader1 = new system.windows.forms.columnheader();
this.columnheader2 = new system.windows.forms.columnheader();
this.suspendlayout();
//
// textbox1
//
this.textbox1.location = new system.drawing.point(24, 40);
this.textbox1.multiline = true;
this.textbox1.name = "textbox1";
this.textbox1.scrollbars = system.windows.forms.scrollbars.both;
this.textbox1.size = new system.drawing.size(176, 296);
this.textbox1.tabindex = 0;
this.textbox1.text = "";
//
// button1
//
this.button1.location = new system.drawing.point(456, 40);
this.button1.name = "button1";
this.button1.tabindex = 1;
this.button1.text = "开始搜索";
this.button1.click += new system.eventhandler(this.button1_click);
//
// progressbarsearch
//
this.progressbarsearch.location = new system.drawing.point(32, 360);
this.progressbarsearch.name = "progressbarsearch";
this.progressbarsearch.size = new system.drawing.size(490, 24);
this.progressbarsearch.tabindex = 2;
//
// listview1
//
this.listview1.columns.addrange(new system.windows.forms.columnheader[] {
this.columnheader1,
this.columnheader2});
this.listview1.location = new system.drawing.point(248, 40);
this.listview1.name = "listview1";
this.listview1.size = new system.drawing.size(184, 288);
this.listview1.tabindex = 5;
//
// columnheader1
//
this.columnheader1.text = "dddd";
//
// columnheader2
//
this.columnheader2.text = "sssss";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(544, 413);
this.controls.add(this.listview1);
this.controls.add(this.progressbarsearch);
this.controls.add(this.button1);
this.controls.add(this.textbox1);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button1_click(object sender, system.eventargs e)
{
lansearch();
}
/// <summary>
/// 局域网搜索事件
/// </summary>
private void lansearch()
{
thread = new thread[255];
threadstart threadmethod;
thread threadprogress = new thread(new threadstart(progresssearch));
threadprogress.start();
string localhost = (dns.gethostbyname(dns.gethostname())).addresslist[0].tostring(); //本地主机ip地址
str = localhost.substring(0,localhost.lastindexof("."));
for (int i=0;i<255;i++) //建立255个线程扫描ip
{
threadmethod = new threadstart(lansearchthreadmethod);
thread[i] = new thread(threadmethod);
thread[i].name = i.tostring();
thread[i].start();
if (!thread[i].join(100)) //thread.join(100)不知道这处这么用对不对,感觉没什么效果一样
{
thread[i].abort();
}
}
getlanhost();
listlanhost();
}
/// <summary>
/// 多线程搜索方法
/// </summary>
private void lansearchthreadmethod()
{
int currently_i = convert.touint16(thread.currentthread.name); //当前进程名称
ipaddress scanip = ipaddress.parse( str + "."+convert.tostring(currently_i +1)); //获得扫描ip地址
iphostentry scanhost = null;
scanhost = dns.gethostbyaddress(scanip); //获得扫描ip地址主机信息
if (scanhost != null)
{
lanhost[currently_i,0] = scanip.tostring();
lanhost[currently_i,1] = scanhost.hostname;
}
//progressbarsearch.value = progressbarsearch.value +1;
}
/// <summary>
/// 文本框显示主机名与ip列表
/// </summary>
private void getlanhost()
{
for (int i=0;i<255;i++)
if ( lanhost[i,0] !="")
{
textbox1.text =textbox1.text + lanhost[i,1] +":" +lanhost[i,0] + "\r\n";
}
}
/// <summary>
/// listview1 显示搜索主机
/// </summary>
private void listlanhost()
{
listview1.view = view.list;
listviewitem aa ;
for (int i=0;i<255;i++)
{
if ( lanhost[i,0] !="")
{
aa= new listviewitem();
aa.text = lanhost[i,1];
aa.tag = lanhost[i,0];
listview1.items.add(aa);
}
}
}
/// <summary>
/// 进度条处理线程
/// </summary>
private void progresssearch()
{
//label1.text = "进度条只是时间估计,不是真实搜索进度!";
progressbarsearch.value = 0;
for (int i=0;i<255;i++)
{
progressbarsearch.value = progressbarsearch.value + 1;
thread.sleep(100);
}
}
}
}
遗憾之处:因搜索较慢,没有实现真实的搜索进度。
不懂之处:实现文字提示时,当在鼠标事件首尾插入
private void button1_click(object sender, system.eventargs e)
{
lab1.text = “开始搜索”; //新插入
lansearch();
lab1.text = “结束搜索”; //新插入
}
文本提示时,在lab1上始终不能及时显示,而是等所有线程结束后才显示“结束搜索“。
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.net;
using system.threading;
namespace windowlansearch
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.textbox textbox1;
private system.windows.forms.button button1;
private string[,] lanhost;
private system.windows.forms.progressbar progressbarsearch;
private thread[] thread;
private system.windows.forms.listview listview1;
private system.windows.forms.columnheader columnheader1;
private system.windows.forms.columnheader columnheader2;
private string str;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
initlanhost();
progressbarsearch.maximum = 255;
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 数组初始化
/// </summary>
private void initlanhost()
{
lanhost = new string[255,2];
for (int i=0;i<255;i++)
{
lanhost[i,0] = "";
lanhost[i,1] = "";
}
}
/// <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.textbox1 = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.progressbarsearch = new system.windows.forms.progressbar();
this.listview1 = new system.windows.forms.listview();
this.columnheader1 = new system.windows.forms.columnheader();
this.columnheader2 = new system.windows.forms.columnheader();
this.suspendlayout();
//
// textbox1
//
this.textbox1.location = new system.drawing.point(24, 40);
this.textbox1.multiline = true;
this.textbox1.name = "textbox1";
this.textbox1.scrollbars = system.windows.forms.scrollbars.both;
this.textbox1.size = new system.drawing.size(176, 296);
this.textbox1.tabindex = 0;
this.textbox1.text = "";
//
// button1
//
this.button1.location = new system.drawing.point(456, 40);
this.button1.name = "button1";
this.button1.tabindex = 1;
this.button1.text = "开始搜索";
this.button1.click += new system.eventhandler(this.button1_click);
//
// progressbarsearch
//
this.progressbarsearch.location = new system.drawing.point(32, 360);
this.progressbarsearch.name = "progressbarsearch";
this.progressbarsearch.size = new system.drawing.size(490, 24);
this.progressbarsearch.tabindex = 2;
//
// listview1
//
this.listview1.columns.addrange(new system.windows.forms.columnheader[] {
this.columnheader1,
this.columnheader2});
this.listview1.location = new system.drawing.point(248, 40);
this.listview1.name = "listview1";
this.listview1.size = new system.drawing.size(184, 288);
this.listview1.tabindex = 5;
//
// columnheader1
//
this.columnheader1.text = "dddd";
//
// columnheader2
//
this.columnheader2.text = "sssss";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(544, 413);
this.controls.add(this.listview1);
this.controls.add(this.progressbarsearch);
this.controls.add(this.button1);
this.controls.add(this.textbox1);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button1_click(object sender, system.eventargs e)
{
lansearch();
}
/// <summary>
/// 局域网搜索事件
/// </summary>
private void lansearch()
{
thread = new thread[255];
threadstart threadmethod;
thread threadprogress = new thread(new threadstart(progresssearch));
threadprogress.start();
string localhost = (dns.gethostbyname(dns.gethostname())).addresslist[0].tostring(); //本地主机ip地址
str = localhost.substring(0,localhost.lastindexof("."));
for (int i=0;i<255;i++) //建立255个线程扫描ip
{
threadmethod = new threadstart(lansearchthreadmethod);
thread[i] = new thread(threadmethod);
thread[i].name = i.tostring();
thread[i].start();
if (!thread[i].join(100)) //thread.join(100)不知道这处这么用对不对,感觉没什么效果一样
{
thread[i].abort();
}
}
getlanhost();
listlanhost();
}
/// <summary>
/// 多线程搜索方法
/// </summary>
private void lansearchthreadmethod()
{
int currently_i = convert.touint16(thread.currentthread.name); //当前进程名称
ipaddress scanip = ipaddress.parse( str + "."+convert.tostring(currently_i +1)); //获得扫描ip地址
iphostentry scanhost = null;
scanhost = dns.gethostbyaddress(scanip); //获得扫描ip地址主机信息
if (scanhost != null)
{
lanhost[currently_i,0] = scanip.tostring();
lanhost[currently_i,1] = scanhost.hostname;
}
//progressbarsearch.value = progressbarsearch.value +1;
}
/// <summary>
/// 文本框显示主机名与ip列表
/// </summary>
private void getlanhost()
{
for (int i=0;i<255;i++)
if ( lanhost[i,0] !="")
{
textbox1.text =textbox1.text + lanhost[i,1] +":" +lanhost[i,0] + "\r\n";
}
}
/// <summary>
/// listview1 显示搜索主机
/// </summary>
private void listlanhost()
{
listview1.view = view.list;
listviewitem aa ;
for (int i=0;i<255;i++)
{
if ( lanhost[i,0] !="")
{
aa= new listviewitem();
aa.text = lanhost[i,1];
aa.tag = lanhost[i,0];
listview1.items.add(aa);
}
}
}
/// <summary>
/// 进度条处理线程
/// </summary>
private void progresssearch()
{
//label1.text = "进度条只是时间估计,不是真实搜索进度!";
progressbarsearch.value = 0;
for (int i=0;i<255;i++)
{
progressbarsearch.value = progressbarsearch.value + 1;
thread.sleep(100);
}
}
}
}
遗憾之处:因搜索较慢,没有实现真实的搜索进度。
不懂之处:实现文字提示时,当在鼠标事件首尾插入
private void button1_click(object sender, system.eventargs e)
{
lab1.text = “开始搜索”; //新插入
lansearch();
lab1.text = “结束搜索”; //新插入
}
文本提示时,在lab1上始终不能及时显示,而是等所有线程结束后才显示“结束搜索“。
推荐阅读
-
C#获得程序的根目录以及判断文件是否存在的实例讲解
-
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例
-
winform C#获得Mac地址,IP地址,子网掩码,默认网关的实例
-
C# WebApi+Webrtc局域网音视频通话实例
-
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例_PHP
-
c# 获得局域网主机列表实例
-
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例_php技巧
-
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例_php技巧
-
zabbix根据主机和端口列表自动发现监控远程MongoDB实例
-
C#获得程序的根目录以及判断文件是否存在的实例讲解