Get unused IP address

I need to get an accessible IP address from DHCP. I tried to get any IP address and start pinging the next one until I get to the point that it doesn't respond.

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;
        }
    }
}

However, sometimes DHCP reserves a special address for some computers, so I need to get an accessible IP address from dhcp. How can I implement this in C #?

+4
source share
3 answers

I found this application that solves the problem http://www.centrel-solutions.com/support/tools.aspx?feature=dhcpapi

+1
source

This is not how you use it, you have to request a DHCP server with a new ip, and then accept it, read about communication with a DHCP server here

http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol

+3

DHCP- .

DHCP :

  • DHCPDISCOVER
  • DHCPREQUEST
  • DHCPDECLINE
  • DHCPRELEASE
  • DHCPINFORM

. RFC 2131 - .

Windows DHCP server, , Windows PowerShell Scripting DHCP.

Weekend Scripter: DHCP? !

  • : Microsoft Scripting Guy, Ed Wilson, Windows PowerShell DHCPServer, DHCP.
+3

All Articles