CSDN里相关的问题很多,但是我都没有找到我要的答案。我想搜索出当前局域网中所有在使用的IP和相关的计算机名。我用遍历IP的方法,如自己的IP是192.168.1.101,我就在192.168.1的基础上遍历最后一部分得到一个完整的ipaddress。然后
IPHostEntry ScanHost = null;
ScanHost = Dns.Resolve(ipaddress);是否可以用if (ScanHost != null)来判断局域网内是否有使用这样的IP的机器呢?如果有,就用一个数组保存机器的IP和计算机名。
我看到网上有人这样用,但是我用在有的电脑上可以正确找到所有的机器,在其他有的电脑上就只能找到自己。而且我把ScanHost = Dns.Resolve(ipaddress);换成
ScanHost = Dns.GetHostEntry(ipaddress)后,用if (ScanHost != null)的方法就无法判断了。
而且那个API函数是有点慢,局域网IP要搜索254个,开254个线程就很快。但是会被有的安全机制误认为蠕虫病毒。请问有没有其他快一点方法?为题有点多,希望大家耐心回答!谢谢!
IPHostEntry ScanHost = null;
ScanHost = Dns.Resolve(ipaddress);是否可以用if (ScanHost != null)来判断局域网内是否有使用这样的IP的机器呢?如果有,就用一个数组保存机器的IP和计算机名。
我看到网上有人这样用,但是我用在有的电脑上可以正确找到所有的机器,在其他有的电脑上就只能找到自己。而且我把ScanHost = Dns.Resolve(ipaddress);换成
ScanHost = Dns.GetHostEntry(ipaddress)后,用if (ScanHost != null)的方法就无法判断了。
而且那个API函数是有点慢,局域网IP要搜索254个,开254个线程就很快。但是会被有的安全机制误认为蠕虫病毒。请问有没有其他快一点方法?为题有点多,希望大家耐心回答!谢谢!
private void button3_click(object sender, System.EventArgs e)
{
//Thread 类: 创建并控制线程
Thread thScan = new thread(new ThreadStrart(ScanTarget));
//Thread.Start 方法:启动线程
thScan.Strart();
}
private void ScanTarget()
{
//构造IP地址的31-8BIT 位,也就是固定的IP地址的前段
// numericUpDown1是定义的System.Windows.Forms.NumericUpDown控件
string strIPAddress=numericUpDown1.Text+”.”+numericUpDown2.Text+”.”+numericUpDown3.Text+”.”;
//开始扫描地址
int nStrat = Int32.Parse(numericUpDown4.Text);
//终止扫描地址
int nEnd =Int32.Parse(numericUpDown5.Text);
//扫描的操作
for(int i=nStrat;i<=nEnd;i++)
{
string strScanIPAdd = strIPAddress +i.ToString();
//转换成IP地址
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd);
try
{
//你可以加入自已的,增强功能
// Dns.GetHostByAddress 方法: 根据 IP 地
//址获取 DNS 主机信息。
IPHostEntry myScanHost =
Dns.GetHostByAddress(myScanIP);
//获取主机的名
string strHostName =myScanHost.HostName.ToString();
richTextBox1.AppendText(strScanIPAdd+”à”+strHostName+”\r”);
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}//for
}//private
{
IPHostEntry myHost = new IPHostEntry();
try
{
myHost = Dns.GetHostEntry(IPAddress.Parse("127.0.0.1"));
textBox1.Text = myHost.HostName;
this.richTextBox1.AppendText("localhost ipaddress:");
foreach (IPAddress ip in myHost.AddressList)
{
this.richTextBox1.AppendText(ip.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} private void button2_Click(object sender, EventArgs e)
{
IPHostEntry host = new IPHostEntry();
host = Dns.Resolve(this.textBox2.Text.Trim()); this.richTextBox2.AppendText("remoting host ipaddress:");
foreach (IPAddress ip in host.AddressList)
{
this.richTextBox2.AppendText(ip.ToString());
}
} private void button3_Click(object sender, EventArgs e)
{
//Thread scan = new Thread(new ThreadStart(ScanTarget));
//scan.Start();
ScanTarget();
}
private void ScanTarget()
{
string ip = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + ".";
int start =Convert.ToInt32( this.numericUpDown4.Value);
int end = Convert.ToInt32(this.numericUpDown5.Value);
for (int i = start; i < end; i++)
{
string strScanIPAdd = ip + i.ToString();
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd);
try
{
IPHostEntry myScanHost = Dns.GetHostEntry(myScanIP);
string hostName = myScanHost.HostName;
this.richTextBox3.AppendText(strScanIPAdd + " " + hostName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
你这样不是把搜索的IP地址和对应的主机名输出了吗?我想要的只是在使用的IP。
比如我的IP是192.168.1.101。而192.168.1网段只接了5台电脑,这5台电脑自动获取IP。我要的就是输出这5台电脑的IP和对应的计算机上名。IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP);如果这个IP没有人在用,myScanHost返回的是什么的值呢?