Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems about a connection to a tcp server with domain!!
Forum Updated to NodeBB v4.3 + New Features

Problems about a connection to a tcp server with domain!!

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 310 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coderKnight
    wrote on last edited by
    #1

    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

    Ronel_qtmasterR 1 Reply Last reply
    0
    • C coderKnight

      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

      Ronel_qtmasterR Offline
      Ronel_qtmasterR Offline
      Ronel_qtmaster
      wrote on last edited by
      #2

      @coderKnight please could you send your code?especially the connect part

      C 1 Reply Last reply
      0
      • Ronel_qtmasterR Ronel_qtmaster

        @coderKnight please could you send your code?especially the connect part

        C Offline
        C Offline
        coderKnight
        wrote on last edited by
        #3

        @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?

        Pl45m4P 1 Reply Last reply
        0
        • C coderKnight

          @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?

          Pl45m4P Online
          Pl45m4P Online
          Pl45m4
          wrote on last edited by
          #4

          @coderKnight

          What thread approach do you use?

          And please use the code tags to format the code you post here.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          C 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @coderKnight

            What thread approach do you use?

            And please use the code tags to format the code you post here.

            C Offline
            C Offline
            coderKnight
            wrote on last edited by
            #5

            @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 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved