检测IP地址冲突 [英] Detecting IP address conflicts

查看:132
本文介绍了检测IP地址冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有允许用户设置运行它的系统的IP地址的功能。在更新NIC设置之前,我的应用程序是否有办法检测用户输入的IP地址是否已被网络上的任何其他设备占用?如果发生IP冲突,我打算显示我自己的自定义消息框,如果发生IP冲突,我不想更新NIC卡设置。



我能想到解决此问题的两种方法:



1.对输入的IP地址执行网络ping并等待响应。但是,如果系统已被禁用以响应ping,此方法是否无法检测到IP地址冲突?



2.获取当前所有IP地址的列表网络并检查输入的IP地址是否已存在。在.Net中,如何确定网络中所有IP地址的列表?



请注意我正在寻找一种解决问题的程序化方法。

解决方案




参考此链接它将有所帮助





检索网络列表计算机名称使用C# - 点击这里



谢谢



Siva Rm K

试试这个:







 IPAddress [] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); 
string localComputerName = Dns.GetHostName();
public static bool IsLocalIpAddress ( string host)
{
try
{ // 获取主机IP地址
IPAddress [] hostIPs = Dns.GetHostAddresses(host);
// 获取本地IP地址
IPAddress [] localIPs = Dns.GetHostAddresses (Dns.GetHostName());

// 测试任何主机IP是否等于任何本地IP或localhost
foreach ( hostIPs中的IPAddress hostIP
{
< span class =code-comment> //
is localhost
if (IPAddress.IsLoopback(hostIP)) return true ;
// 是本地地址
foreach ( localIPs中的IPAddress localIP
{
if (hostIP.Equals(localIP)) return true ;
}
}
}
catch {}
返回 false ;
}


IsLocalIpAddress( localhost ); // true(环回名称)
IsLocalIpAddress( 127.0.0.1); // true(环回IP)
IsLocalIpAddress( MyNotebook); // true(我的电脑名称)
IsLocalIpAddress( 192.168.0.1); // true(我的IP)
IsLocalIpAddress( NonExistingName); // false(非现有计算机名称)
IsLocalIpAddress( 99.0.0.1); // false(我的网络中不存在的IP)


My app has a feature to allow users to set the IP address of the system on which it is running. Is there any way for my application to detect if the IP address entered by the users is already taken by any other device on the network before I update the NIC settings ? I intend to display my own custom message box if this IP conflict occurs and I dont want to update the NIC card settings if an IP conflict has occured.

I can think of 2 approaches for this issue:

1. Do a network ping for the entered IP address and wait for a response. But would this method fail to detect an IP address conflict if a system has been disabled to respond to pings ?

2. Get a list of all IP addresses in the current network and check if the entered IP address already exists. In .Net , how do I determine the list of all IP addresses in the network ?

Please see that I am looking for a programmatic approach to solving the issue.

解决方案

Hi
Refer this Link it will help


Retreiving a list of network computer names using C# - Click Here

Thanks

Siva Rm K


Try this:



IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
string localComputerName = Dns.GetHostName();
public static bool IsLocalIpAddress(string host)
{
  try
  { // get host IP addresses
    IPAddress[] hostIPs = Dns.GetHostAddresses(host);
    // get local IP addresses
    IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

    // test if any host IP equals to any local IP or to localhost
    foreach (IPAddress hostIP in hostIPs)
    {
      // is localhost
      if (IPAddress.IsLoopback(hostIP)) return true;
      // is local address
      foreach (IPAddress localIP in localIPs)
      {
        if (hostIP.Equals(localIP)) return true;
      }
    }
  }
  catch { }
  return false;
}


IsLocalIpAddress("localhost");        // true (loopback name)
IsLocalIpAddress("127.0.0.1");        // true (loopback IP)
IsLocalIpAddress("MyNotebook");       // true (my computer name)
IsLocalIpAddress("192.168.0.1");      // true (my IP)
IsLocalIpAddress("NonExistingName");  // false (non existing computer name)
IsLocalIpAddress("99.0.0.1");         // false (non existing IP in my net)


这篇关于检测IP地址冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆