How does DHCP react if i remove the LAN cable and re attach

I have used the following code as a template for my project:

      case DHCP_IP_ASSIGN:
      case DHCP_IP_CHANGED:
         /* If this block empty, act with default_ip_assign & default_ip_update  */
         // This example calls the registered 'my_ip_assign' in the two case.
         // Add to ...
      case DHCP_IP_LEASED:
         // TO DO YOUR NETWORK APPs.
      case DHCP_FAILED:
         /* ===== Example pseudo code =====  */
         // The below code can be replaced your code or omitted.
         // if omitted, retry to process DHCP
         if(my_dhcp_retry > MY_MAX_DHCP_RETRY)
         #if DEBUG_MODE != DEBUG_NO
            printf(">> DHCP %d Failed\r\n",my_dhcp_retry);
            my_dhcp_retry = 0;
            DHCP_stop();      // if restart, recall DHCP_init()
            network_init();   // apply the default static network
      /* User LED Toggle every 1s */
      if((msTicks - prevTick) > 1000)
      prevTick = msTicks;
      if ( GPIO_PinOutGet(LED_PORT, LED_PIN) )
        GPIO_PinOutClear(LED_PORT, LED_PIN);
        GPIO_PinOutSet(LED_PORT, LED_PIN);

But what should happen if I remove the LAN cable and re attach it to another router. Will this code detect this and reassign a new IP address?

Or should I detect the PHY_LINK_OFF condition and call DHCP_init() ?

You can have a single DHCP in a subnet and the DHCP server has a release time of the address ( also for a week ) … even if you change your router in the same subnet the address remains the same throughout the period of lease.
Is there a DHCP protocol message to release the address of the client but the devices usually do not know when user may unplug cable from the network and does not send a DHCP Release message.
If you want to try the condition must send this request when the cable is still attached :wink: .

In my opinion it is important to always respected the sequence:

 - discovery
 - offer
 - request
 - acknowledgment

In this way is the DHCP server that will continue to send you back the same address until the end of the lease time.
As long as your device is connected to the network the DHCP server will not give this address to other device.
The important thing is the conflict of addresses but in the source of W5500 is checked at lease time.


I think you should detect the PHY_LINK_OFF condition and call DHCP_init().
If W5500 get IP address already, although router is changed, W5500 doesn’t try to call DHCP_init().