How to revert to a static IP address when DHCP fails?

Hi,

My deployed device communicates with a PC over TCP/IP sockets. The device would be deployed with the standard DHCP option enabled. At some deployment sites though there is a possibility that the Host PC and the device would be connected one to one using a LAN cable with no DHCP server assigning IP address.

In this case I want that if DHCP fails the device should revert to a specific static IP and subnet mask. How do I do this?

Dear @kapeed

Fixed IP?

First I would like to bring up the question whether assigning a fixed IP address is a proper approach. There’s plenty of use cases where this leads to serious problems. To give you one example: If the DHCP server in a big network fails for a short period, you will assign a fixed IP which might conflict with another device in the same network (or even with multiple of your own devices).

Auto IP

Actually there’s already a standardized mechanism which probably fits for your what you want to do - each Ethernet device assigns itself an automatic IP address (Auto-IP, link-local address) in the range 169.254.x.x, if there is no DHCP server around.
All you need to do is broadcast your own IP on one side, and receive this broadcast on the other side, in order to learn the partner’s IP address and start the communication.
If your device has a unique name, you can even directly start a communication using this name.

Fixed IP

If you still want to assign a fixed IP address, you need to implement this on your own. The basic steps are:

  1. Use GetAdaptersInfo() function to query whether you got a valid IP address. After a timeout…
  2. Change the IP address, as described in the community post:

Make sure you don’t save the registry. Otherwise your next boot will directly start with the fixed IP!

Regards,
Andy

Hi Andy,

I might have forgotten to say thanks to you for your suggestion. I liked it we will be moving to using DHCP and AutoIP mechanism.

Could you just elaborate on this approach “If your device has a unique name, you can even directly start a communication using this name.” what Network protocol/mechanism would help me achieve this? As this would be even better.

Currently we are using the “All you need to do is broadcast your own IP on one side, and receive this broadcast on the other side, in order to learn the partner’s IP address and start the communication.” mechanism with the broadcast UDP containing information that helps identify the device But it would be really cool if I can have names for my devices on the network and the PC discovers the device by its name or type.

Dear @kapeed

The name resolution is done using the NetBIOS protocol. It is used whenever there’s no DNS server is available.
You can configure the Colibri’s NetBIOS name in the control panel → System → Device Name → Device name. Don’t forget to save the registry to keep the name over a reboot.
NetBIOS is active in the default installation of any Windows version, so there’s nothing to configure.

You can use the NetBIOS name like any other URL, e.g. type ping colibri on your PC.

Be aware NetBIOS is a Windows thing. Probably a Linux PC cannot resolve the device name using this method.

Regards, Andy