开发者

Easiest way to find free IPAddress in current subnet?

What is t开发者_开发技巧he easiest way to find the next unused IP address on a local adaptors subnet?

IPAddress GetNextFreeIP(NetworkInterface intface)
{
    ...?
}

Update

I've written a BOOTP/TFTP server that could be used in many different scenarios (cross-over cable, small private network, large corporate network). I have to give the small embedded system I'm updating an IP address and would like to find a spare one for it to use...


If you have DHCP-managed IP ranges, you have an instance you can talk to - the DHCP server (like David recommended)

If you don't have managed IP ranges and no other instance you can talk to, there is no reliable way to tell if an IP is used or unused. IP does not offer such a service itself.


Apart from asking the DHCP-server, I guess you could do a broadcast ping on the subnet and collect the responses from all computers and simply sort the IP-addresses to find the next one. This assumes, of course, that all devices are on-line and responds to broadcasts. So in a pretty controlled environment, this could work.


The short answer is you can't, as Thorsten79 just said.

A slightly longer answer:

It depends on your network's configuration: how the admin has decided to allocate ip addresses. Usually there's a mix of manually assigned ip addresses for servers, routers etc, and a set of dhcp assigned ip addresses for workstations and such.

If you can talk to the dhcp server you might find out which addresses in the reserved range are free, but for the rest of the addresses you cannot find out.

The more interesting question is what you are trying to accomplish? Perhaps it can be realized in a different manner?


From what I've seen of embedded devices with a TFTP server, they boot up with a hard coded IP Address that is documented. After a set period of time (~3-10 seconds), the boot loader transfers control to the application that reads its configuration and sets the IP Address.

If someone wants to use the boot loader to load new firmware, then they need to read the documentation, make sure that IP Address is reachable (on the same subnet), reboot the device, and TFTP the application to the device.


You are all technically right when you say there is no way to ensure an IP isn't being used. But I've decided to rely on this for now...

    public static IPAddress FindNextFree(this IPAddress address)
    {
        IPAddress workingAddress = address;
        Ping pingSender = new Ping();

        while (true)
        {
            byte[] localBytes = workingAddress.GetAddressBytes();

            localBytes[3]++;
            if (localBytes[3] > 254)
                localBytes[3] = 1;

            workingAddress = new IPAddress(localBytes);

            if (workingAddress.Equals(address))
                throw new TimeoutException("Could not find free IP address");

            PingReply reply = pingSender.Send(workingAddress, 1000);
            if (reply.Status != IPStatus.Success)
            {
                return workingAddress;
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜