🎓 Do your best and obey the destiny. I am a postgraduate student in Southeast University and a summer intern in Java background development in Ctrip. I love fitness and basketball, and I am willing to share what I have seen and gained related to technology. I follow the public account @flying Veal and get the update of the article as soon as possible

🎁 This article has been included in the “CS-Wiki” Gitee official recommended project, has accumulated 1.7K + STAR, is committed to creating a perfect back-end knowledge system, in the road of technology to avoid detours, welcome friends to come to exchange and study

🍉 If you do not have a good project, you can refer to a project I wrote “Open source community system Echo” Gitee official recommended project, so far has accumulated 800+ star, SpringBoot + MyBatis + Redis + Kafka + Elasticsearch + Spring Security +… And provide detailed development documents and supporting tutorials. Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo

I met a DHCP

As we all know, every device on the Internet has its unique address all over the world, that is to say, “IP address”. It is because of IP address that users can efficiently and conveniently select the object they need from millions of computers when they operate on the connected computers.

An IP address is like an address: having someone’s phone number, you can talk to them. Similarly, with the IP address of a host, you can communicate with that host.

But it would be exhausting to design IP addresses for each and every host. Let alone the tens of billions of computers in the world, let alone a laptop computer that needs to be reconfigured every time it moves around the world.

Obviously, manually assigning IP addresses is not something that the top brains of us humans can do. In order to automatically configure IP addresses and uniformly manage IP address allocation, THE Dynamic Host Configuration Protocol (DHCP), Dynamic Host Configuration Protocol (DCM) was created, which is an application layer protocol.

In a network without DHCP service, IP addresses, subnet masks, default routes, etc., must be set before computers are connected to the network. In addition, to ensure that all host IP addresses are unique, the administrator must clearly know how to assign IP addresses to avoid conflicts.

In the network that provides DHCP services, the administrator’s burden is greatly reduced. As long as the DHCP server does some necessary Settings, once the computer is connected to the network, it will automatically obtain the necessary Settings for TCP/IP communication (IP address and other information). The DHCP server ensures that the IP address is unique.

It can be seen that DHCP actually provides a plug and play network, that is, as long as a computer is physically connected, you can directly use the physical device without special Settings.

How DHCP works

DHCP can not only configure IP addresses for computers connected to the Internet, but also configure subnet masks, default routes and other information. Of course, these information can not be magically created, they need to be configured in advance on the DHCP server.

The process of obtaining an IP address from DHCP is similar to THAT of ARP, but there are differences. The client host and DHCP server communicate with each other in broadcast mode based on UDP. The host sends a request message to port 67 of the DHCP server. The DHCP server responds the reply message to port 68 on the host. The specific process is as follows:

1) The client host broadcasts the DHCP Discover packet: the host requests the DHCP server to set its IP address and subnet mask

To put it more commonly, when a device joins a new network and only knows its MAC address, it will shout (broadcast, target address 255.255.255.255) to obtain some necessary information: “I’m new here, my MAC address is this, and I don’t have an IP address (or 0.0.0.0 for unknown), is there a DHCP server that can rent me an IP address?” , this process is called DHCP Discover.

I think you can see the importance of MAC addresses, they are born, IP addresses are not.

2) THE DHCP server broadcasts the DHCP Offer packet: the DHCP server notifies the host of which network Settings are available

If there are multiple DHCP servers on the network that hear the new person yelling and are willing to rent an IP address to the new person, the new host will receive multiple AVAILABLE IP addresses and other information. This process is called “DHCP Offer”.

3) The client host broadcasts the DHCP Request packet: the host notifies the server that it wants to use the Settings for notifications in (2)

The host selects one of the multiple offers it receives, usually the first one to arrive. In addition, it broadcasts a DHCP Request packet to the network, which contains the MAC address of the client host, the IP address of the Offer accepted, and the ADDRESS of the DHCP server that provides the Offer, and tells all DHCP servers, It tells the other DHCP servers to rescind their IP addresses in order to provide them to the next IP lease requester.

4) The DHCP server broadcasts DHCP ACK packets: The server notifies the host of the Settings (3) allowed, that is, officially allocates IP addresses and other information to the host

Before the completion of step (4), the host has not been confirmed by the DHCP server, so it does not have an IP address. When the DHCP server wants to send a message to the host, it can only broadcast the message, which contains information such as the IP address assigned to the host.

At this point, DHCP network Settings are complete, and normal TCP/IP communication can be carried out.


There is usually a time limit in DHCP Settings. When the lease expires, the allocated IP address will be reclaimed. Of course, just like renting a house, you can renew the lease as long as you say so in advance. The client host can send a DHCP Request packet to inform it of extending the lease before the time limit.

In addition, when the client host no longer needs the IP address, it can Release its OWN IP address by sending A DHCP Release message at any time. After receiving the DHCP Release message, the DHCP server reclaims the corresponding IP address and reallocates it.

There are three mechanisms of DHCP

The working mechanism mentioned above is a dynamic IP address allocation method. In fact, DHCP has three mechanisms to allocate IP addresses:

1) Automatic Allocation: The DHCP server assigns a permanent IP address to a host. The DHCP client can use the IP address permanently after renting an IP address from the DHCP server for the first time.

2) Dynamic Allocation: The DHCP server assigns a time-limited IP address to a host. If the IP address expires or the host explicitly abandons it, the IP address can be used by other hosts.

3) Manual Allocation: The IP address of the DHCP client is specified by the network administrator. The DHCP server only sends the SPECIFIED IP address to the client host.

Obviously, of the three address allocation methods, only dynamic allocation can reuse addresses that clients no longer need.

How does the DHCP server ensure that ASSIGNED IP addresses do not conflict

The DHCP service relies heavily on the DCHP server. If the DHCP server is faulty, IP addresses cannot be automatically assigned. That is, all hosts on the network segment cannot communicate with each other through TCP/IP.

Therefore, two or more DHCP servers are deployed on one network segment. This inevitably causes a problem: Each DHCP server records information about IP address allocation on this network segment, and IP addresses allocated to each DHCP server conflict with each other.

To avoid conflicts, the DHCP server provides the following functions:

  • An ICMP echo request packet is sent before an IP address is assigned. If an ICMP reply packet is received, it goes without saying that the IP address is already used.

Also, the client host must verify that the IP address assigned by the DCHP server is in use:

  • If a reply is received, it is clear that the MAC address can be found according to the IP address. Can I use this IP address?

| flying veal 🎉 pay close attention to the public, get updates immediately

  • I am a postgraduate student in Southeast University and a summer intern in Java background development of Ctrip. I run a public account “Flying Veal” in my spare time, which was opened on 2020/12/29. Focus on sharing computer fundamentals (data structure + algorithm + computer network + database + operating system + Linux), Java technology stack and other related original technology good articles. The purpose of this public account is to let you can quickly grasp the key knowledge, targeted. Pay attention to the public number for the first time to get the article update, we progress together on the way to growth
  • And recommend personal maintenance of open source tutorial project: CS-Wiki (Gitee recommended project, has accumulated 1.7K + STAR), committed to creating a perfect back-end knowledge system, in the road of technology to avoid detours, welcome friends to come to exchange learning ~ 😊
  • If you don’t have any outstanding projects, you can refer to a project I wrote “Open source community System Echo” Gitee official recommended project, which has accumulated 800+ star so far. SpringBoot + MyBatis + Redis + Kafka + Elasticsearch + Spring Security +… And provide detailed development documents and supporting tutorials. Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo