Problems about a connection to a tcp server with domain!!
-
wrote on 29 Jan 2024, 01:38 last edited by
I‘m using QTcpSocket with Qt version 5.9.9. In my system,I have a 4G module ,I powered it on when my program started and wait for it attched to my system as a netcard by usb.When I try calling "connectToHost" to connect to the tcp server(a server with a domain) in three threads,I sometimes got "host not found" error,and this is not recoverable unless I restart my program, I was really confused for this for serveral days,any suggestions? thanks
-
I‘m using QTcpSocket with Qt version 5.9.9. In my system,I have a 4G module ,I powered it on when my program started and wait for it attched to my system as a netcard by usb.When I try calling "connectToHost" to connect to the tcp server(a server with a domain) in three threads,I sometimes got "host not found" error,and this is not recoverable unless I restart my program, I was really confused for this for serveral days,any suggestions? thanks
wrote on 29 Jan 2024, 01:50 last edited by@coderKnight please could you send your code?especially the connect part
-
@coderKnight please could you send your code?especially the connect part
wrote on 29 Jan 2024, 02:33 last edited by@Ronel_qtmaster
1. I just create a QTcpSocket like this
clientInstance = new QTcpSocket(this);
2. then create a timer ,in the timer slot,I try connect to
server periodicly if connect to server failed,or stop the
timer
bool result = false;
clientInstance->connectToHost("domain",10000);
result = clientInstance->waitForConnected(10000);
if(result == false){
qDebug() << "got error:" << clientInstance>error()
}
The procedure happens In three diffrent threads,did I do something wrong? -
@Ronel_qtmaster
1. I just create a QTcpSocket like this
clientInstance = new QTcpSocket(this);
2. then create a timer ,in the timer slot,I try connect to
server periodicly if connect to server failed,or stop the
timer
bool result = false;
clientInstance->connectToHost("domain",10000);
result = clientInstance->waitForConnected(10000);
if(result == false){
qDebug() << "got error:" << clientInstance>error()
}
The procedure happens In three diffrent threads,did I do something wrong?wrote on 29 Jan 2024, 03:58 last edited byWhat thread approach do you use?
And please use the code tags to format the code you post here.
-
What thread approach do you use?
And please use the code tags to format the code you post here.
wrote on 31 Jan 2024, 06:50 last edited by@Pl45m4
Sorry for reply late,here is the code#include "dnsparsetask.h" #include <QHostInfo> namespace Communication { DnsParseTask::DnsParseTask(QObject *parent) : QObject(parent) { } DnsParseTask *DnsParseTask::instance() { static QMutex mutex; static QScopedPointer<DnsParseTask> inst; QMutexLocker locker(&mutex); if (Q_UNLIKELY(!inst)) { if (!inst) { inst.reset(new DnsParseTask); } } return inst.data(); } void DnsParseTask::init(QString hostname,int port,int period) { _self = new QThread(); if(_self == nullptr){ qWarning() << "create thread failed"; return; } _hostname = hostname; _port = port; _periodTime = period; this->moveToThread(_self); connect(_self, &QThread::finished, _self, &QObject::deleteLater); connect(_self, &QThread::finished, this, &QObject::deleteLater); connect(_self, SIGNAL(started()), this, SLOT(startRun())); _self->start(); return; } int DnsParseTask::setDomainToParse(QString domain) { QMutexLocker locker(&_lock); if(domain.isEmpty()){ return -1; } if(_domainMap.contains(domain)){ qWarning() << "domain[" << domain << "] has been in map"; return 0; } _domainMap.insert(domain,""); if(_parseTimer->isActive() == false){ qWarning() << "start parse domain"; _parseTimer->start(); } return 0; } QString DnsParseTask::getDomainIpAddr(QString domain) { if(_domainMap.contains(domain) == false){ return QString(); } return _domainMap.value(domain); } void DnsParseTask::periodicParse() { QObject *obj = _socket; _socket->connectToHost(_hostname,_port); if(_socket->waitForConnected(5000)){ qWarning() << this->thread()->currentThreadId() << ",[connect to host" << _hostname << "] success!";; _parseTimer->stop(); return; } qWarning() << this->thread()->currentThreadId() << ",[connect to host" << _hostname << "] failed:" << _socket->errorString(); return; #if 0 QStringList hostNames = _domainMap.keys(); foreach (const QString &hostName, hostNames) { qWarning() << ">>>>>>>>>>>>>>>:" << this->thread()->currentThreadId() << "," << hostName; QHostInfo hostInfo = QHostInfo::fromName(hostName); if (hostInfo.error() == QHostInfo::NoError) { QList<QHostAddress> addresses = hostInfo.addresses(); foreach (const QHostAddress &address, addresses) { qDebug() << "Resolved IP address:" << address.toString(); } } else { qDebug() << "DNS lookup failed:" << hostInfo.errorString(); } } return; #endif #if 0 QStringList hostNames = _domainMap.keys(); foreach (const QString &hostName, hostNames) { QHostInfo::lookupHost(hostName, [hostName](const QHostInfo &hostInfo) { if (hostInfo.error() == QHostInfo::NoError) { QList<QHostAddress> addresses = hostInfo.addresses(); foreach (const QHostAddress &address, addresses) { qDebug() << "Resolved IP address for" << hostName << ":" << address.toString(); } } else { qDebug() << "DNS lookup failed for" << hostName << ":" << hostInfo.errorString(); } }); } #endif } void DnsParseTask::startRun() { qWarning() << this->thread()->currentThreadId() << "start run period time:" << _periodTime; _domainMap.clear(); _parseTimer = new QTimer(this); connect(_parseTimer,SIGNAL(timeout()),this,SLOT(periodicParse())); _parseTimer->setInterval(_periodTime); _parseTimer->start(); _socket = new QTcpSocket(this); } } And the main function like: int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); DnsParseTask *task = nullptr; task = new DnsParseTask; task->init("mqtts.lfzk.com",1883,500); task = new DnsParseTask; task->init("mqtt.lfzk.com",1883,500); task = new DnsParseTask; task->init("101.200.183.188",1883,100); task = new DnsParseTask; task->init("cb05b435-ba2e-41de-bec9-b9ef2254a1a5.mock.pstmn.io",80,0); a.exec(); return 0; }
In the code I create four QTcpSocket and try connect to some domains,the "init" function hold three params,the domain,the port and retry connection interval,and the "periodicParse" function try connect to the domain.On my borad, I use wired netcard "eth0" to connect to servers,”eth0“ configed as dhcp. Before startup the program,I shutdown the netcard by “ifdown eth0”,and then startup the program.The program works 1 minute and print ”Host not found“ error,then I open the netcard ,and my board got correct IP address,but the program still print ”Host not found“ error.I use the cmd "ping -i eth0 mqtts.lfzk.com" ,the cmd works well,I am really confused about this
1/5