using System.net;
using System.net.Socets ;
using System.windows.form; //需要引用的类
//----------显示出本机的网络名和IP
private void button1_Click(object sender, EventArgs e)
{
IPHostEntry ipdnshost = new IPHostEntry();//主机信息的容器
ipdnshost = Dns.GetHostByName(Dns.GetHostName()); //通用本机的网络名分析信息
for (int i = 0; i < ipdnshost.AddressList.Length; i++)
{
listBox1.Items.Add(ipdnshost.AddressList[i]); //在listbox1中显示出IP地址
}
}
//----------扫描一个网络的主机和IP
private void ScanTarget()
{
int max = Convert.ToInt32(this.text1.text); //要扫描网段的最大IP
int min = Convert.ToInt32(this.text2.text); //要扫描网段的最小IP
string strip = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + "." ; //IP前三位
for(int i=min;i < max ;i++) //循环扫描的IP
{
string bsip = strip + i.ToString(); //一个要扫描的完整的IP地址
IPAddress ip = IPAddress.Parse(bsip); //将字符的地址转换
try
{
IPHostEntry iphost = Dns.GetHostByAddress(ip); //通过IP获取主机信息
string hostname = iphost.HostName; //主机名
listBox1.Items.app( hostname.ToString()); //显示出扫描到的主机名和IP
}
catch (Exception ex) //网络上不存在IP会出错 跳过出错
{
}
}
}
//---------