Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Extending networking/socket programming functionality of a qml project
Forum Updated to NodeBB v4.3 + New Features

Extending networking/socket programming functionality of a qml project

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
92 Posts 5 Posters 18.0k Views 2 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.
  • S SGaist
    15 May 2022, 18:55

    The client just uses the hostname. That's all.

    Q Offline
    Q Offline
    qcoderpro
    wrote on 15 May 2022, 19:27 last edited by
    #43

    @SGaist

    1. So to sum it up: The client connects to my hostname which has been updated by the server to show its IP address and since this way the client can see the server's IP address it can connect to it. Right?
    2. If the above question is right, is this page the one I should study on how to make the server app update the hostname's record with its IP?
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 May 2022, 19:33 last edited by
      #44
      1. Yes
      2. Looks like it yes.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply 15 May 2022, 19:56
      1
      • S SGaist
        15 May 2022, 19:33
        1. Yes
        2. Looks like it yes.
        Q Offline
        Q Offline
        qcoderpro
        wrote on 15 May 2022, 19:56 last edited by
        #45

        @SGaist

        So in my server app there should be a line of code as an HTTP request, like this example (of that page):
        http://username:password@dynupdate.no-ip.com/nic/update?hostname=mytest.example.com&myip=192.0.2.25

        So if:
        my username at noip is: coderdevqt@gmail.com
        my password there is: passwordexample
        and my hostname there is: coderdev.ddns.net

        that HTTP request should be:
        coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net
        Right?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 May 2022, 06:36 last edited by
          #46

          Don't you think it would have been faster to just write and test the query ?

          Also: https.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply 17 May 2022, 07:40
          2
          • S SGaist
            17 May 2022, 06:36

            Don't you think it would have been faster to just write and test the query ?

            Also: https.

            Q Offline
            Q Offline
            qcoderpro
            wrote on 17 May 2022, 07:40 last edited by qcoderpro
            #47

            @SGaist

            I'm not sure how to make an HTTP request using QNetworkAccessManager by a single string. I tired this but neither of the qInfo() prints anything. The program also terminates at once! :|

             QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                const QNetworkRequest request;
                const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
                manager->put(request,data);
            
                connect(manager, &QNetworkAccessManager::finished, [] {
                    QNetworkReply* reply = nullptr;
                    if(reply->error())
                        qInfo() << "ERROR!: " + reply->errorString();
                    else
                        qInfo() << "Reply got back with no error!";
            
                    reply->deleteLater();
                });
            
            S J 2 Replies Last reply 17 May 2022, 09:06
            0
            • Q qcoderpro
              17 May 2022, 07:40

              @SGaist

              I'm not sure how to make an HTTP request using QNetworkAccessManager by a single string. I tired this but neither of the qInfo() prints anything. The program also terminates at once! :|

               QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                  const QNetworkRequest request;
                  const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
                  manager->put(request,data);
              
                  connect(manager, &QNetworkAccessManager::finished, [] {
                      QNetworkReply* reply = nullptr;
                      if(reply->error())
                          qInfo() << "ERROR!: " + reply->errorString();
                      else
                          qInfo() << "Reply got back with no error!";
              
                      reply->deleteLater();
                  });
              
              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 17 May 2022, 09:06 last edited by
              #48

              @qcoderpro said in Extending networking/socket programming functionality of a qml project:

              connect(manager, &QNetworkAccessManager::finished, [] {
              QNetworkReply* reply = nullptr;
              if(reply->error())

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • Q qcoderpro
                17 May 2022, 07:40

                @SGaist

                I'm not sure how to make an HTTP request using QNetworkAccessManager by a single string. I tired this but neither of the qInfo() prints anything. The program also terminates at once! :|

                 QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                    const QNetworkRequest request;
                    const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
                    manager->put(request,data);
                
                    connect(manager, &QNetworkAccessManager::finished, [] {
                        QNetworkReply* reply = nullptr;
                        if(reply->error())
                            qInfo() << "ERROR!: " + reply->errorString();
                        else
                            qInfo() << "Reply got back with no error!";
                
                        reply->deleteLater();
                    });
                
                J Offline
                J Offline
                JonB
                wrote on 17 May 2022, 09:36 last edited by
                #49

                @qcoderpro
                I would also suggest you do the connect(manager, ...) before manager->put() not afterwards.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qcoderpro
                  wrote on 17 May 2022, 20:58 last edited by qcoderpro
                  #50

                  @SGaist
                  What do you mean by copying part of the code I wrote above? I didn't get it. Do you mean those three lines of code are sufficient to make an Http request? Then how about the string!? :|

                  @JonB
                  You mean this way?

                  QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                  const QNetworkRequest request;
                  const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
                  
                  connect(manager, &QNetworkAccessManager::finished, [] {
                          QNetworkReply* reply = nullptr;
                          if(reply->error())
                              qInfo() << "ERROR!: " + reply->errorString();
                          else
                              qInfo() << "Reply got back with no error!";
                  
                          reply->deleteLater();
                      });
                  
                  manager->put(request,data);
                  

                  I'm confused! :(
                  Furthermore the put function uploads the contents of data to the destination request, while both my data and destination are packed into that data!

                  J 1 Reply Last reply 17 May 2022, 22:01
                  0
                  • Q qcoderpro
                    17 May 2022, 20:58

                    @SGaist
                    What do you mean by copying part of the code I wrote above? I didn't get it. Do you mean those three lines of code are sufficient to make an Http request? Then how about the string!? :|

                    @JonB
                    You mean this way?

                    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                    const QNetworkRequest request;
                    const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
                    
                    connect(manager, &QNetworkAccessManager::finished, [] {
                            QNetworkReply* reply = nullptr;
                            if(reply->error())
                                qInfo() << "ERROR!: " + reply->errorString();
                            else
                                qInfo() << "Reply got back with no error!";
                    
                            reply->deleteLater();
                        });
                    
                    manager->put(request,data);
                    

                    I'm confused! :(
                    Furthermore the put function uploads the contents of data to the destination request, while both my data and destination are packed into that data!

                    J Offline
                    J Offline
                    JonB
                    wrote on 17 May 2022, 22:01 last edited by
                    #51

                    @qcoderpro said in Extending networking/socket programming functionality of a qml project:

                    @JonB
                    You mean this way?

                    Yes, I think it's preferable to attach the slot to manager's signal before you call manager->put(). Though @VRonin pointed out it doesn't actually matter in this case, but it's good style anyway.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 17 May 2022, 23:20 last edited by VRonin
                      #52

                      Since I've been summoned... how is this not a segfault?

                      QNetworkReply* reply = nullptr;
                      if(reply->error())
                      

                      What you should do is connect the QNetworkReply, not the QNetworkAccessManager.

                      // the initialisation of these 2 looks wrong but I'll ignore it for now
                      QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                      const QNetworkRequest request;
                      const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
                      QNetworkReply* reply = manager->put(request,data);
                      connect(reply,&QNetworkReply::finished,this,[reply](){
                          if(reply->error())
                              qInfo() << "ERROR!: " + reply->errorString();
                          else
                              qInfo() << "Reply got back with no error!";
                      });
                      connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
                      

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      Q 1 Reply Last reply 18 May 2022, 06:09
                      2
                      • V VRonin
                        17 May 2022, 23:20

                        Since I've been summoned... how is this not a segfault?

                        QNetworkReply* reply = nullptr;
                        if(reply->error())
                        

                        What you should do is connect the QNetworkReply, not the QNetworkAccessManager.

                        // the initialisation of these 2 looks wrong but I'll ignore it for now
                        QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                        const QNetworkRequest request;
                        const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
                        QNetworkReply* reply = manager->put(request,data);
                        connect(reply,&QNetworkReply::finished,this,[reply](){
                            if(reply->error())
                                qInfo() << "ERROR!: " + reply->errorString();
                            else
                                qInfo() << "Reply got back with no error!";
                        });
                        connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
                        
                        Q Offline
                        Q Offline
                        qcoderpro
                        wrote on 18 May 2022, 06:09 last edited by
                        #53

                        @VRonin

                        That's better, thanks. The project doesn't itself terminate and works but I get this error!
                        fdgdfg.PNG
                        I've used the exact same string as yours, of course with my real info.

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          VRonin
                          wrote on 18 May 2022, 08:47 last edited by
                          #54

                          @qcoderpro said in Extending networking/socket programming functionality of a qml project:

                          I've used the exact same string as yours, of course with my real info.

                          Yeah, I expected something like that, hence my comment:

                          the initialisation of these 2 looks wrong but I'll ignore it for now

                          Your request has no url...
                          I have no idea how the server you are trying to connect to works so it's hard to tell you exactly what to do. Did you manage to do what you want with the server with another tool (like curl, or a browser-based rest client)?

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 18 May 2022, 09:00 last edited by
                            #55

                            @qcoderpro said in Extending networking/socket programming functionality of a qml project:

                            const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};

                            This is not the payload, it's the full query. The API has no payload.

                            Please read again the documentation.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            Q 1 Reply Last reply 18 May 2022, 11:04
                            1
                            • S SGaist
                              18 May 2022, 09:00

                              @qcoderpro said in Extending networking/socket programming functionality of a qml project:

                              const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};

                              This is not the payload, it's the full query. The API has no payload.

                              Please read again the documentation.

                              Q Offline
                              Q Offline
                              qcoderpro
                              wrote on 18 May 2022, 11:04 last edited by qcoderpro
                              #56

                              @SGaist @VRonin
                              I'm rather confused.
                              Do you mean to split the string into two parts, one for data, and the other for request like this?

                              const QByteArray data {"https://coderdevqt@gmail.com:passwordexample"};
                              const QNetworkRequest request;
                              request.rawHeader("@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net");
                              
                              QNetworkAccessManager* manager = new QNetworkAccessManager(this);
                              QNetworkReply* reply = manager->put(request,data);
                              
                              connect(reply,&QNetworkReply::finished,this,[reply](){
                                  if(reply->error())
                                      qInfo() << "ERROR!: " + reply->errorString();
                                  else
                                      qInfo() << "Reply got back with no error!";
                              });
                              connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
                              

                              The result shows the same error! :|

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 18 May 2022, 12:03 last edited by
                                #57

                                Here is an example that shows how to do it in a simple fashion with the various elements of the query separated (replace what is found between <> with adequate values):

                                QByteArray auth = "<username>:<password>";
                                QByteArray authHeaderData = "Basic " + auth.toBase64();
                                
                                QUrl requestUrl("https://dynupdate.no-ip.com/nic/update");
                                requestUrl.setQuery("hostname=<your-host-name>&myip=<your-ip-address>");
                                
                                QNetworkRequest request(requestUrl);
                                request.setRawHeader("Authorization", authHeaderData);
                                
                                QNetworkReply * reply = qnam.get(request);
                                

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                Q 1 Reply Last reply 18 May 2022, 12:18
                                1
                                • S SGaist
                                  18 May 2022, 12:03

                                  Here is an example that shows how to do it in a simple fashion with the various elements of the query separated (replace what is found between <> with adequate values):

                                  QByteArray auth = "<username>:<password>";
                                  QByteArray authHeaderData = "Basic " + auth.toBase64();
                                  
                                  QUrl requestUrl("https://dynupdate.no-ip.com/nic/update");
                                  requestUrl.setQuery("hostname=<your-host-name>&myip=<your-ip-address>");
                                  
                                  QNetworkRequest request(requestUrl);
                                  request.setRawHeader("Authorization", authHeaderData);
                                  
                                  QNetworkReply * reply = qnam.get(request);
                                  
                                  Q Offline
                                  Q Offline
                                  qcoderpro
                                  wrote on 18 May 2022, 12:18 last edited by qcoderpro
                                  #58

                                  @SGaist
                                  The part myip is optional because the server's IP address connecting to their system will be used. So I use the version below, if you agree:

                                      QByteArray auth = "myEmail:myPass";
                                      QByteArray authHeaderData = "Basic " + auth.toBase64();
                                      
                                      QUrl requestUrl("https://dynupdate.no-ip.com/nic/update");
                                      requestUrl.setQuery("hostname=coderdev.ddns.net");
                                      
                                      QNetworkRequest request(requestUrl);
                                      request.setRawHeader("Authorization", authHeaderData);
                                      
                                      QNetworkReply * reply = qnam.get(request);
                                  

                                  Two more questions:
                                  1- We've dropped the @ sign existing in the first string. Doesn't matter?
                                  2- What's qnam, please?

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 18 May 2022, 19:23 last edited by
                                    #59
                                    1. Sure it does matter, that's the authentication part. Re-read the code.
                                    2. a QNetwornAccessManager object.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    Q 1 Reply Last reply 18 May 2022, 19:51
                                    1
                                    • S SGaist
                                      18 May 2022, 19:23
                                      1. Sure it does matter, that's the authentication part. Re-read the code.
                                      2. a QNetwornAccessManager object.
                                      Q Offline
                                      Q Offline
                                      qcoderpro
                                      wrote on 18 May 2022, 19:51 last edited by
                                      #60

                                      @SGaist
                                      I meant the @ in the string:
                                      "...//coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"

                                      The message "Reply got back with no error!" is printed for this:

                                      QByteArray auth = "myEmail:myPass";
                                      QByteArray authHeaderData = "Basic " + auth.toBase64();
                                      
                                      QUrl requestUrl("https://dynupdate.no-ip.com/nic/update");
                                      requestUrl.setQuery("hostname=coderdev.ddns.net");
                                      
                                      QNetworkRequest request(requestUrl);
                                      request.setRawHeader("Authorization", authHeaderData);
                                      
                                      QNetworkAccessManager* qnam = new QNetworkAccessManager(this);
                                      QNetworkReply * reply = qnam->get(request);
                                       if(reply->error())
                                          qInfo() << "ERROR!: " + reply->errorString();
                                      else
                                          qInfo() << "Reply got back with no error!";
                                      

                                      So probably the record has been updated successfully.

                                      For the client app I used the code below but the messages don't appear to be exchanged between the apps!

                                         QHostInfo info = QHostInfo::fromName("coderdev.ddns.net");
                                        //  If updating over HTTPS, our system listens on Port 443
                                          sendAddress(info.addresses().front(), 443);
                                      
                                      // ...
                                      void Client::sendAddress(QHostAddress ip, unsigned int port)
                                      {
                                          tcpSocket->abort();
                                          tcpSocket->connectToHost(ip, port);
                                      }
                                      
                                      
                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 18 May 2022, 20:00 last edited by
                                        #61

                                        I know that you are talking about the "@". As I wrote, it's about authentication. There are several ways to do it.

                                        As for the client side, did you connect the error signals of your socket ?
                                        Does your server really answer on port 443 ?

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        Q 1 Reply Last reply 18 May 2022, 20:45
                                        1
                                        • S SGaist
                                          18 May 2022, 20:00

                                          I know that you are talking about the "@". As I wrote, it's about authentication. There are several ways to do it.

                                          As for the client side, did you connect the error signals of your socket ?
                                          Does your server really answer on port 443 ?

                                          Q Offline
                                          Q Offline
                                          qcoderpro
                                          wrote on 18 May 2022, 20:45 last edited by qcoderpro
                                          #62

                                          @SGaist

                                          I get this message for this error:

                                           case QAbstractSocket::ConnectionRefusedError:
                                                  qInfo() << "The connection was refused by the peer. "
                                                             "Make sure the server is running, "
                                                             "and check that the host name and port "
                                                             "settings are correct.";
                                          

                                          As for the port number, they say in Sending an Update in their website: If updating over HTTPS, our system listens on Port 443 It is not necessary to open any incoming ports for updating.

                                          Of course both apps are running on the same machine.

                                          1 Reply Last reply
                                          0

                                          52/92

                                          17 May 2022, 23:20

                                          • Login

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