c# winform 获取局域网内在线IP和计算机名,获取IP,多线程网络编程

发布时间:2010年08月12日      浏览次数:485 次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;//ArrayList用
using System.Threading;
using System.Net;

namespace AllTest
{
public partial class Form7 : Form
{
public Form7(string ipPrefix, int startIP, int endIP)
//public Form7()
{
InitializeComponent();

this.startIP = startIP;
this.endIP = endIP;
this.ipPrefix = ipPrefix;
computerList = new ArrayList();
}

private int startIP = 0;
private int endIP = 0;
private string ipPrefix = "";
private ArrayList computerList = null;

public void ScanComputers()
{
for(int i=startIP;i<=endIP;i++)
{
string scanIP = ipPrefix +"."+i.ToString();

IPAddress myScanIP = IPAddress.Parse(scanIP);

IPHostEntry myScanHost = null;

string[] arr = new string[2];

try
{
myScanHost = Dns.GetHostByAddress(myScanIP);
}

catch
{
continue;
}

if (myScanHost != null)
{
arr[0] = myScanHost.HostName;

arr[1] = scanIP;

computerList.Add(arr);
MessageBox.Show(myScanHost.HostName.ToString());
MessageBox.Show(scanIP.ToString());
}
}
}

private void button1_Click(object sender, EventArgs e)
{
Form7 cai = new Form7("192.168.1", 134, 135);

Thread thScan = new Thread(new ThreadStart(cai.ScanComputers));

thScan.Start();
}

}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!