获得局域网中IP和计算机名称
2006-04-10 12:30
411 查看
本程序中采用多线程搜索方法.获得局域网网段为"192.168.0.1"中已开机的IP和计算机名称,并分别显示在tB1和tB2两个文本框中.具体的方法如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
namespace LanHostSerach
{
public partial class Form1 : Form
{
private Thread[] thread;
private string str = "192.168.0";
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 线程搜索
/// </summary>
private void SearchLink()
{
int i = Convert.ToUInt16(Thread.CurrentThread.Name);
IPAddress ScanIP = IPAddress.Parse(str + '.' + i.ToString()); //获得扫描IP地址
IPHostEntry ScanHost = null;
try
{
ScanHost = Dns.GetHostByAddress(ScanIP); //获得扫描IP地址主机信息
}
catch (Exception E)
{
}
if (ScanHost != null)
{
tB1.Text = tB1.Text + ScanHost.HostName + ":" + ScanIP.ToString() + "\r\n";
tB2.Text = tB2.Text + ScanHost.HostName + "\r\n";
}
}
private void button1_Click(object sender, EventArgs e)
{
thread = new Thread[255];
ThreadStart threadMethod;
for (int i = 0; i < 255; i++) //建立255个线程扫描IP
{
threadMethod = new ThreadStart(SearchLink);
thread[i] = new Thread(threadMethod);
thread[i].Name = i.ToString();
thread[i].Start();
if (!thread[i].Join(100))
{
thread[i].Abort();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
namespace LanHostSerach
{
public partial class Form1 : Form
{
private Thread[] thread;
private string str = "192.168.0";
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 线程搜索
/// </summary>
private void SearchLink()
{
int i = Convert.ToUInt16(Thread.CurrentThread.Name);
IPAddress ScanIP = IPAddress.Parse(str + '.' + i.ToString()); //获得扫描IP地址
IPHostEntry ScanHost = null;
try
{
ScanHost = Dns.GetHostByAddress(ScanIP); //获得扫描IP地址主机信息
}
catch (Exception E)
{
}
if (ScanHost != null)
{
tB1.Text = tB1.Text + ScanHost.HostName + ":" + ScanIP.ToString() + "\r\n";
tB2.Text = tB2.Text + ScanHost.HostName + "\r\n";
}
}
private void button1_Click(object sender, EventArgs e)
{
thread = new Thread[255];
ThreadStart threadMethod;
for (int i = 0; i < 255; i++) //建立255个线程扫描IP
{
threadMethod = new ThreadStart(SearchLink);
thread[i] = new Thread(threadMethod);
thread[i].Name = i.ToString();
thread[i].Start();
if (!thread[i].Join(100))
{
thread[i].Abort();
}
}
}
}
}
相关文章推荐
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- c#(WinForm)遍历局域网计算机(电脑)获取IP和计算机名称(转)
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- Asp.Net获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- c# 遍历局域网计算机(电脑)获取IP和计算机名称
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 获取局域网计算机名称和IP
- c#(WinForm)遍历局域网计算机(电脑)获取IP和计算机名称
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 获得局域网中计算机的列表(包括计算机名,ip和mac)的方法
- VBNET编程获得计算机IP及名称
- 获得局域网中计算机的列表(包括计算机名,ip和mac)的方法
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- c#(WinForm)遍历局域网计算机(电脑)获取IP和计算机名称(转)
- 获得本地计算机名称&获得本地IP