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

How to implement the Qt daemon

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article focuses on "how to implement the Qt daemon". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the Qt daemon.

I. Preface

No one can guarantee that the program they write does not have any BUG, especially in commercial projects, the greater the amount of the program, the higher the complexity, the greater the probability of error, especially the site environment is very different, which may be different from the original local computer test environment, there are many special cases that do not take into account, if you need to ensure that the program runs 24 hours a day, you need to find some ways to make the program alive after the death. On the embedded linux, most of them will be handled in the form of watchdog. After the program opens the watchdog driver, it will feed the dog regularly. Once the specified time is exceeded, the hardware will be soft reset and so on. This method is relatively reliable, what if it needs to be run on an ordinary PC? This article provides a way for software to realize the daemon, the principle is udp communication, write a daemon program separately, specially responsible for detecting the existence of the main program, and start if it does not exist. The main program only needs to start the live listening port and reply to the ok when it receives the hello. In order to make it compatible with any program, the commonness is specially extracted and a variety of settings are added.

Main functions:

The name of the program for detection can be set.

The udp communication port can be set.

The number of timeouts can be set.

Automatically records the number of restarts.

Automatically record the last restart time.

Whether the desktop needs to be refreshed.

The current number of restarts and the last restart time can be reset.

Auto-hidden tray runs or runs in the background.

The name of the interface setter has been enabled and paused.

2. AppLive::AppLive (QObject * parent): QObject (parent) {udpServer = new QUdpSocket (this); QString name = qApp- > applicationFilePath (); QStringList list = name.split ("/"); appName = list.at (list.count ()-1). Split ("."). At (0);} void AppLive::readData () {QByteArray tempData; do {tempData.resize (udpServer- > pendingDatagramSize ()); QHostAddress sender Quint16 senderPort; udpServer- > readDatagram (tempData.data (), tempData.size (), & sender, & senderPort); QString data = QLatin1String (tempData); if (data = = "hello") {udpServer- > writeDatagram (QString ("% 1OK") .Arg (appName). ToLatin1 (), sender, senderPort);} while (udpServer- > hasPendingDatagrams ()) } bool AppLive::start (int port) {bool ok = udpServer- > bind (port); if (ok) {connect (udpServer, SIGNAL (readyRead ()), this, SLOT (readData (); qDebug ()

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