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 19.1k 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.
  • Q qcoderpro

    @SGaist
    Since I've not been dealt with projects like that, I'm seeking the easiest way. An easy free service like No-ip. I wish it could solve the issue with the port as well. :|
    What do you suggest, please?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #72

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

    What do you suggest, please?

    I suggest to use a fixed port number...

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    Q 1 Reply Last reply
    1
    • jsulmJ jsulm

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

      What do you suggest, please?

      I suggest to use a fixed port number...

      Q Offline
      Q Offline
      qcoderpro
      wrote on last edited by
      #73

      @jsulm
      Good. Could you also guide me how to do that? I mean if there's any reference or Docs page to help me learn how to do that I will be appreciative.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #74

        https://en.m.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

        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
        1
        • SGaistS SGaist

          https://en.m.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

          Q Offline
          Q Offline
          qcoderpro
          wrote on last edited by qcoderpro
          #75

          @SGaist
          Sorry for the delay. I was very busy. If you think it's better, I'm going to create a new thread for the problem.

          I checked the page for the ports numbers and tested a couple of them which are related to messaging based on TCP.

          My server.cpp has:

          Server::Server(QObject *parent)
              : QObject{parent}
              , tcpSocket(new QTcpSocket(this))
              , tcpServer(new QTcpServer(this))
          {
              initServer();
          }
          
          QString Server::initServer() {
          
              tcpServer = new QTcpServer(this);
              if(!tcpServer->listen())
                  return "Server Unable to start the server: " +
                          tcpServer->errorString();
          
              connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection);
          
              QByteArray auth = "myEmail@gmail.com: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!";
          }
          
          void Server::onNewConnection() {
          
              qInfo() << "New connection arrived!";
          
              tcpSocket = tcpServer->nextPendingConnection();
              connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Server::displayError);
              inOut.setDevice(tcpSocket);
              inOut.setVersion(QDataStream::QDataStream::Qt_5_10);
              connect(tcpSocket, &QAbstractSocket::readyRead, this, &Server::writeMessage);
          }
          ...
          

          First ran this app and got the message: Reply got back with no error!

          client.cpp contains:

          Client::Client(QObject *parent)
              : QObject{parent}
              , tcpSocket(new QTcpSocket(this))
          {
              inOut.setDevice(tcpSocket);
              inOut.setVersion(QDataStream::Qt_5_10);
              connect(tcpSocket, &QAbstractSocket::readyRead, this, &Client::writeMessage);
              connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Client::displayError);
          
              QHostInfo info = QHostInfo::fromName("coderdev.ddns.net");
              sendAddress(info.addresses().front(), 4662);
          
              /*   18	Message Send Protocol[19][20]
                   157	Assigned  KNET/VM Command/Message Protocol
                   218	Message posting protocol (MPP)
                   993	Internet Message Access Protocol over TLS/SSL (IMAPS)[11]
                   2123	GTP control messages (GTP-C)
                   2152	GTP user data messages (GTP-U)
                   2775	Short Message Peer-to-Peer (SMPP)[citation need
                   4662	OrbitNet Message Service
              */
          }
          
          void Client::sendAddress(QHostAddress ip, unsigned int port)
          {
              tcpSocket->abort();
              tcpSocket->connectToHost(ip, port);
          }
          ...
          

          After running the server (and hoping that the record on noip has been updated correctly), ran the client and tested all ports above!
          Yet no new connection nor an error from the displayError slot.
          But Qt Creator gives the error: Cannot retrieve debugging output. for all of the ports!

          Where is the problem, please? :(

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #76

            You do not set the port on the server.

            Also, you create the QTcpServer object twice. Not an issue per se but it does not make any sense.

            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
            1
            • SGaistS SGaist

              You do not set the port on the server.

              Also, you create the QTcpServer object twice. Not an issue per se but it does not make any sense.

              Q Offline
              Q Offline
              qcoderpro
              wrote on last edited by
              #77

              @SGaist
              Yeah thank you.

              In the previous version (when the two app run on the same machine) we don't have port setting. How to set the port on the server, please?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #78

                It's all in the documentation of the listen method.

                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
                1
                • SGaistS SGaist

                  It's all in the documentation of the listen method.

                  Q Offline
                  Q Offline
                  qcoderpro
                  wrote on last edited by qcoderpro
                  #79

                  I firstly run the server and get: Reply got back with no error!, for this code:

                  void Server::initServer() {
                  
                      if(!tcpServer->listen(QHostAddress::Any, 4662))
                          qInfo() << "Server Unable to start the server: " +
                                  tcpServer->errorString();
                  
                      connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection);
                  
                      QByteArray auth = "myEmail@gmail.com: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!";
                  }
                  

                  Then when running the client and texting, nothing is shown on the other side and get the error: QIODevice::write (QTcpSocket): device not open, on the server. :|

                  Q 1 Reply Last reply
                  0
                  • Q qcoderpro

                    I firstly run the server and get: Reply got back with no error!, for this code:

                    void Server::initServer() {
                    
                        if(!tcpServer->listen(QHostAddress::Any, 4662))
                            qInfo() << "Server Unable to start the server: " +
                                    tcpServer->errorString();
                    
                        connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection);
                    
                        QByteArray auth = "myEmail@gmail.com: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!";
                    }
                    

                    Then when running the client and texting, nothing is shown on the other side and get the error: QIODevice::write (QTcpSocket): device not open, on the server. :|

                    Q Offline
                    Q Offline
                    qcoderpro
                    wrote on last edited by
                    #80

                    My hostname is also active.

                    Capture.PNG

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #81

                      Do you realize that QNetworkAccessManager is asynchronous ?

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

                      Q 2 Replies Last reply
                      0
                      • SGaistS SGaist

                        Do you realize that QNetworkAccessManager is asynchronous ?

                        Q Offline
                        Q Offline
                        qcoderpro
                        wrote on last edited by
                        #82

                        @SGaist

                        I know that asynchronous tasks can work in parallel but don't know what it has to do with the error I get! :(
                        What is the problem that the projects can't connect together and work, please?

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Do you realize that QNetworkAccessManager is asynchronous ?

                          Q Offline
                          Q Offline
                          qcoderpro
                          wrote on last edited by qcoderpro
                          #83

                          There're many differences between synchronous and asynchronous data transmission. But despite many disadvantages asynchronous transmission involves it's still possible to use it for my two apps, otherwise you wouldn't suggest QNetworkAccessManager for that, I'm sure.
                          Do you mean that the data I send from the server to client or vice versa will be corrupted or dropped when traversing the routes? In asynchronous transmission, data is sent in form of byte or character. Five times I sent messages from client to the server and another five-time from server to the client. Waited for more than 15 minutes. Nothing, even one letter, arrived at the other side ! :(

                          What I get instead was error messages from both projects.
                          Client:
                          "The following error occurred: Connection timed out" QNativeSocketEngine::write() was not called in QAbstractSocket::ConnectedState "The following error occurred: Connection timed out"

                          Server:
                          Reply got back with no error! QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open qt.qpa.mime: Retrying to obtain clipboard. qt.qpa.mime: Retrying to obtain clipboard.

                          What can I do? :(

                          Q 1 Reply Last reply
                          0
                          • Q qcoderpro

                            There're many differences between synchronous and asynchronous data transmission. But despite many disadvantages asynchronous transmission involves it's still possible to use it for my two apps, otherwise you wouldn't suggest QNetworkAccessManager for that, I'm sure.
                            Do you mean that the data I send from the server to client or vice versa will be corrupted or dropped when traversing the routes? In asynchronous transmission, data is sent in form of byte or character. Five times I sent messages from client to the server and another five-time from server to the client. Waited for more than 15 minutes. Nothing, even one letter, arrived at the other side ! :(

                            What I get instead was error messages from both projects.
                            Client:
                            "The following error occurred: Connection timed out" QNativeSocketEngine::write() was not called in QAbstractSocket::ConnectedState "The following error occurred: Connection timed out"

                            Server:
                            Reply got back with no error! QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open qt.qpa.mime: Retrying to obtain clipboard. qt.qpa.mime: Retrying to obtain clipboard.

                            What can I do? :(

                            Q Offline
                            Q Offline
                            qcoderpro
                            wrote on last edited by
                            #84

                            @SGaist
                            I think QNetworkAccessManager is merely responsible for sending the request (or as written in the code geting the request) and since the reply brings no error so apparently there shouldn't be any more worries regarding that. The rest, however, is done by QTCPSoket/Server and other stuff which are connection oriented.

                            QNetworkReply * reply = qnam->get(request);
                            
                               if(reply->error())
                                   qInfo() << "ERROR!: " + reply->errorString();
                               else
                                   qInfo() << "Reply got back with no error!";
                            }
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #85

                              You are using your code sequentially and not asynchronously. First understand that.

                              Use properly signals and slots.

                              You are also not doing any error checks server side.

                              It's also not even clear how your are reading and writing to your sockets now.

                              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
                              0
                              • SGaistS SGaist

                                You are using your code sequentially and not asynchronously. First understand that.

                                Use properly signals and slots.

                                You are also not doing any error checks server side.

                                It's also not even clear how your are reading and writing to your sockets now.

                                Q Offline
                                Q Offline
                                qcoderpro
                                wrote on last edited by
                                #86

                                @SGaist

                                You are using your code sequentially and not asynchronously.

                                What do you mean by that? I don't get it!

                                Use properly signals and slots.
                                You are also not doing any error checks server side.

                                This is part of server's code embracing connections as well as error checks:

                                Server::Server(QObject *parent)
                                    : QObject{parent}
                                    , tcpSocket(new QTcpSocket(this))
                                    , tcpServer(new QTcpServer(this))
                                {
                                    initServer();
                                }
                                
                                void Server::initServer() {
                                
                                    if(!tcpServer->listen(QHostAddress::Any, 4662))
                                        qInfo() << "Server Unable to start the server: " +
                                                tcpServer->errorString();
                                
                                    connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection);
                                
                                    QByteArray auth = "myEmail@gmail.com: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!";
                                }
                                
                                void Server::onNewConnection() {
                                
                                    qInfo() << "New connection arrived!";
                                
                                    tcpSocket = tcpServer->nextPendingConnection();
                                    connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Server::displayError);
                                    inOut.setDevice(tcpSocket);
                                    inOut.setVersion(QDataStream::QDataStream::Qt_5_10);
                                    connect(tcpSocket, &QAbstractSocket::readyRead, this, &Server::writeMessage);
                                }
                                
                                void Server::displayError(QAbstractSocket::SocketError socketError) {
                                
                                    switch (socketError) {
                                    case QAbstractSocket::RemoteHostClosedError:
                                        break;
                                    case QAbstractSocket::HostNotFoundError:
                                        qInfo() <<"The host was not found. Please check the "
                                                  "host name and port settings.";
                                        break;
                                    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.";
                                        break;
                                    default:
                                        qInfo() << "The following error occurred: " + tcpSocket->errorString();
                                    }
                                }
                                

                                What other connections should I add?

                                It's also not even clear how your are reading and writing to your sockets now.

                                It's identical to the way both apps are running on the same machine.
                                It is writeMessage():

                                void Server::writeMessage()
                                {
                                    inOut.startTransaction();
                                
                                    QString message;
                                    inOut >> message;
                                
                                    if (!inOut.commitTransaction())
                                        setMessage("commitTransaction error");
                                   else
                                       setMessage(message);
                                }
                                
                                void Server::setMessage(const QString& newMessage)
                                {
                                    message += newMessage + '\n';
                                    emit messageChanged(message);
                                }
                                
                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #87

                                  What is inOut supposed to be ?

                                  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
                                  0
                                  • SGaistS SGaist

                                    What is inOut supposed to be ?

                                    Q Offline
                                    Q Offline
                                    qcoderpro
                                    wrote on last edited by
                                    #88

                                    @SGaist
                                    This is server's whole header:

                                    #ifndef SERVER_H
                                    #define SERVER_H
                                    
                                    #include <QObject>
                                    #include <QAbstractSocket>
                                    
                                    class QTcpServer;
                                    class QTcpSocket;
                                    class QNetworkReply;
                                    
                                    class Server : public QObject
                                    {
                                        Q_OBJECT
                                         Q_PROPERTY(QString message READ getMessage WRITE setMessage NOTIFY messageChanged)
                                    
                                    public:
                                        explicit Server(QObject *parent = nullptr);
                                    
                                        void setMessage(const QString&);
                                        QString getMessage() const;
                                    
                                    public slots:
                                        void initServer();
                                        void onNewConnection();
                                        void displayError(QAbstractSocket::SocketError);
                                        void sendMessage(const QString&);
                                        void writeMessage();
                                       
                                    signals:
                                        void messageChanged(QString);
                                    
                                    private:
                                        QTcpSocket* tcpSocket { nullptr };
                                        QTcpServer* tcpServer { nullptr };
                                        QDataStream inOut;
                                    };
                                    #endif // SERVER_H
                                    

                                    And sendMessage's definition:

                                    void Server::sendMessage(const QString& newMessage)
                                    {
                                        QByteArray block;
                                        QDataStream out(&block, QIODevice::WriteOnly);
                                        inOut << newMessage;
                                        tcpSocket->write(block);
                                    }
                                    
                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #89

                                      So it looks like inOut is used uninitialized.

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

                                      Q 2 Replies Last reply
                                      0
                                      • SGaistS SGaist

                                        So it looks like inOut is used uninitialized.

                                        Q Offline
                                        Q Offline
                                        qcoderpro
                                        wrote on last edited by qcoderpro
                                        #90

                                        @SGaist

                                        It's been initialized this way in void Server::onNewConnection(), that is, as soon as a new connection arrives:

                                        inOut.setDevice(tcpSocket);
                                        inOut.setVersion(QDataStream::QDataStream::Qt_5_10);
                                        

                                        PS: The projects, using inOut the way above, work completely as expected when run on the same machine!

                                        1 Reply Last reply
                                        0
                                        • SGaistS SGaist

                                          So it looks like inOut is used uninitialized.

                                          Q Offline
                                          Q Offline
                                          qcoderpro
                                          wrote on last edited by
                                          #91

                                          @SGaist

                                          Do you want me to send you both projects privately?

                                          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