qt can achieve thread creation by inheriting QThread.
broadcastthread.h
/*******************************************************************/
// Content : Open the secondary thread
// Update record:
// Date Author Comments
// ---------- ------------ -------------------------------
/*******************************************************************/
#ifndef BROADCASTTHREAD_H
#define BROADCASTTHREAD_H
#include "qthread.h"
/*******************************************************************/
// Function : Open the secondary thread to send UDP packets and monitor TCP connections
/*******************************************************************/
class BroadcastThread : public QThread
{
Q_OBJECT
public:
BroadcastThread();
~BroadcastThread();
private:
/*******************************************************************/
// Function : Get the IP address of this machine
// Parameters : LocalHost: This computer's computer name
// Return : ipaddr: This machine IP address
/*******************************************************************/
QString getIP(QString localHost);
signals:
/*******************************************************************/
// Function : Trigger starting monitoring signal
// Parameters : address: IP address of monitoring
// Return :
/*******************************************************************/
void startlisten(QString address);
/*******************************************************************/
// Function : trigger stop monitoring signal
// Parameters :
// Return :
/*******************************************************************/
void stoplisten();
protected:
/*******************************************************************/
// Function : Get the IP address of this machine and send UDP packets and monitor TCP connection
// Parameters :
// Return :
/*******************************************************************/
void run();
};
#endif // BROADCASTTHREAD_H
broadcastthread.cpp
#include "broadcastthread.h"
#include "asdu_define.h"
#include "commmodule.h"
#include "qhostaddress.h"
#include "qhostinfo.h"
#include "qnetworkinterface.h"
#include "dbmodule.h"
BroadcastThread::BroadcastThread()
: QThread()
{
}
BroadcastThread::~BroadcastThread()
{
}
/*******************************************************************/
// Function : Get the IP address of this machine
// Parameters : LocalHost: The computer name of this machine
// Return : ipaddr: This machine IP address
/*******************************************************************/
QString BroadcastThread::getIP(QString localHost)
{
QString ipAddr;
QHostInfo info = QHostInfo::fromName(localHost);
info.addresses();// Qhostinfo's address function to get the IP address of the machine
// If there are multiple IP addresses IPv4 and IPv6:
foreach(QHostAddress address,info.addresses())
{
if(address.protocol()==QAbstractSocket::IPv4Protocol){ // only the address of the IPv4 protocol
// qDebug()<<address.toString();
ipAddr = address.toString();
}
}
return ipAddr;
}
/*******************************************************************/
// Function : Get the IP address of this machine and send UDP packets and monitor TCP connection
// Parameters :
// Return :
/*******************************************************************/
void BroadcastThread::run()
{
CommInterface * cm = CommModule::GetInterface();
QByteArray socket_info;
QString localHost,localAddress,lastAddress;
localHost = QHostInfo::localHostName();
localAddress = getIP(localHost);
lastAddress = localAddress;
emit startlisten(localAddress);
while(1)
{
socket_info.clear();
localHost = QHostInfo::localHostName();
if(Temp::g_hostaddress=="")
{
localAddress = getIP(localHost);
}
else
{
localAddress = Temp::g_hostaddress;
}
QStringList iplist = localAddress.split(".");
if(iplist.count()!=4)
return;
QString ip1 = iplist.at(0);
QString ip2 = iplist.at(1);
QString ip3 = iplist.at(2);
QString ip4 = iplist.at(3);
quint8 type= 0;
socket_info.append(char(LSB(type)));
socket_info.append(char(LSB(type)));
socket_info.append(char(LSB(type)));
socket_info.append(char(LSB(type)));
socket_info.append(KUdpSetIP);
socket_info.append(ip1.toInt());
socket_info.append(ip2.toInt());
socket_info.append(ip3.toInt());
socket_info.append(ip4.toInt());
cm->DealUIOrder(socket_info);
if(lastAddress != localAddress)
{
emit stoplisten();
QThread::msleep(1000);
emit startlisten(localAddress);
}
lastAddress = localAddress;
QThread::msleep(5000);
}
}
Temp::G_HOSTADDDRESS is a global variable. If the global variable is empty, the IP address that the broadcast is automatically obtained. If it is not empty, the IP address in the global variable in the world.