Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the heartbeat mechanism mean?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

What does the heartbeat mechanism mean? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The heartbeat mechanism is a mechanism that periodically sends a custom structure [heartbeat packet] to let the other person know that he or she is still alive to ensure the validity of the connection.

The heartbeat mechanism means:

Both receiving and sending data in the network are realized by using SOCKET in the operating system. But if the socket is disconnected, there must be problems sending and receiving data. But how can you tell if this socket can still be used? This requires the creation of a heartbeat mechanism in the system.

In fact, a mechanism called heartbeat has been implemented for us in TCP. If you set the heartbeat, TCP will send your heartbeat within a certain amount of time (for example, 3 seconds), and this message will not affect the protocol you define. The so-called "heartbeat" is to send a custom structure (heartbeat packet or heartbeat frame) regularly to let the other person know that he is "online". To ensure the validity of the link.

The so-called heartbeat packet is that the client sends a simple message to the server to tell it that I am still there. The code is to send a fixed message to the server every few minutes, and the server will reply with a fixed message if the server does not receive the client message within a few minutes, then the client will be disconnected.

For example, if some communication software is not used for a long time, if you want to know whether its status is online or offline, you need a heartbeat package and send and receive packets regularly. The contractor: it can be the customer or the server, depending on which side is convenient and reasonable. Usually on the client side. The server can also regularly poll the heartbeat.

A heartbeat is called a heartbeat because it is sent at regular intervals like a heartbeat to tell the server that the client is still alive. In fact, this is to maintain a long connection, as for the content of this package, there is no special stipulation, but it is usually a very small package, or only an empty packet containing a header.

In the mechanism of TCP, there is a mechanism of hearthopping, that is, the option of TCP. The system defaults to a 2-hour heartbeat rate. But it can not detect machine power outage, network cable unplug, firewall and so on. And the logical layer may not be so easy to deal with disconnection.

Generally speaking, it is OK if it is only used to keep alive. Heartbeat packets are generally implemented by sending empty packets at the logic layer. The next timer sends an empty packet to the client at a certain time interval, and then the client returns the same empty packet. If the server does not receive the feedback packet sent by the client within a certain period of time, it can only be determined to be offline. All you need is send or recv, and if the result is zero, it will be dropped.

However, under a long connection, there may be no data exchange for a long time. In theory, the connection is always connected, but in practice, it is difficult to know what happens to the intermediate node. What is more fatal is that some nodes (firewalls) will automatically disconnect the connections where there is no data interaction within a certain period of time.

At this time, we need our heartbeat to maintain long connection and keep alive. After learning about the disconnection, the server logic may need to do some things, such as data cleaning after disconnection, reconnection, of course, this is naturally done by the logic layer according to the requirements. Generally speaking, heartbeat packets are mainly used for keeping long connections alive and disconnecting. In general applications, it is better to determine the time between 30 and 40 seconds. If it is really demanding, it will be in 6-9 seconds.

After reading the above, have you mastered what the heartbeat mechanism means? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report