winform 获得局域网内在线IP和计算机名,获取IP,多线程网络编程
转载请注明来源:https://www.cnblogs.com/hookjc/
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();
}
}
}