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. Help with sockets
Forum Updated to NodeBB v4.3 + New Features

Help with sockets

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 803 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.
  • JonBJ JonB

    @SPlatten
    Are you intending to use the sockets between server & clients for anything other than the "heartbeat" message?

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #3

    @JonB , Yes, this is just the start, the sockets will be used for communications between processes.

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @JonB , Yes, this is just the start, the sockets will be used for communications between processes.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @SPlatten
      And to be 100% percent clear: you intend to have two sockets between each server<->client pair, one for the heartbeat and a different one for your "real" data?

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #5

        What connection? Udp or Tcp?
        Please show your server code.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #6

          The main application listens on port 8123, this is the constructor from the listening class:

          clsListener::clsListener(quint16 uint16Port, QObject* pParent)
                              : mpClient(nullptr)
                              , mpServer(nullptr)
                              , muint16Port(uint16Port) {
              if ( uint16Port == 0 ) {
                  throw "Port invalid!";
              }
              if ( clsListener::pGetListener(uint16Port) != nullptr ) {
                  throw "Port already in use by another listener!";
              }
              clsListener::msmpListeners.insert(std::make_pair(muint16Port, this));
              //Connect server signals
              mpServer = new QTcpServer(pParent);
              QObject::connect(mpServer, &QTcpServer::acceptError
                                  ,this, &clsListener::onAcceptError);
              QObject::connect(mpServer, &QTcpServer::newConnection
                                  ,this, &clsListener::onNewConnection);
              //Start listening to port
              if ( !mpServer->listen(QHostAddress::Any, muint16Port) ) {
                  qdbg() << "Unable to lisen to port " << muint16Port;
              }
              qdbg() << "This module listening on port: " << muint16Port;
          }
          

          Each process also uses the same class with a different port, the idea being that all process will send data to the main process on port 8123. The main process will send data to specific processes on the port number assigned to each.

          The onNewConnection slot:

          void clsListener::onNewConnection() {
              mpClient = mpServer->nextPendingConnection();
          
              if ( mpClient == nullptr ) {
                  return;
              }
              QAbstractSocket::SocketState sckState(mpClient->state());
          
              if ( sckState != QAbstractSocket::ConnectedState ) {
                  return;
              }
              //Connect signals and slots
              QObject::connect(mpClient, &QAbstractSocket::disconnected
                          ,mpClient, &QObject::deleteLater);
              QObject::connect(mpClient, &QTcpSocket::readyRead
                          ,this, &clsListener::onDataIn);
              QObject::connect(mpClient, &QAbstractSocket::errorOccurred
                          ,this, &clsListener::onErrorOccurred);
              qdbg() << "New Connection";
              QByteArray arybytMsg;
              QJsonObject objJSON;
              objJSON.insert(clsJSON::mscszMsgType, clsJSON::mscszCmdInitial);
              arybytMsg = QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
              mpClient->write(arybytMsg);
              //Signal new connection
              emit newConnection(*mpClient);
          }
          

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #7

            I wanted to see the tcp socket handling on the server side where you handle both incoming connections. Also it would be nice to get more information - e.g. how often onNewConnection() is called and so on.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            SPlattenS 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              I wanted to see the tcp socket handling on the server side where you handle both incoming connections. Also it would be nice to get more information - e.g. how often onNewConnection() is called and so on.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #8

              @Christian-Ehrlicher, I must have added it whilst you were posting?

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @SPlatten said in Help with sockets:

                mpClient

                Why do you need this? Are you aware it's getting overwritten each time a new connection is coming in?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                SPlattenS 1 Reply Last reply
                2
                • SPlattenS SPlatten

                  I have been working on an application which uses sockets to communicate between processes, the main application launches the other processes and then listens for heartbeat messages from the launch process. The main application then sends an acknowledgement message in response to the heartbeat. If the processes that are sending the heartbeat do not receive an acknowledge within a specified period of time then the process will self terminate.

                  The number of processes that are created by the main process is configurable and presently for testing I've set it to launch two processes, each process is assigned a unique port number to listen to.

                  • The main process listens to the local IP address and port 8123
                  • All created processes issue there heartbeat message to the local IP address on port 8123
                  • Child process 1 will listen for acknowledgment on the local IP address on port 8124
                  • Child process 2 will listen for acknowledgement on the local IP address on port 8125

                  The main application and each process have they're own log files which I log all traffic to. What I'm seeing is that both child processes are issuing heartbeat's to the local IP address on port 8123 which is correct, however only one of the child processes is receiving an acknowledgement and it seems that the main process is only receiving heartbeat messages from the second child process, which is launched last.

                  I can see when looking at the log file for Child process 1 that it is successfully writing the heartbeat to the socket, but no trace of ever receiving an acknowledgement and looking at the main process log file it is not receiving the heartbeat from child process 1.

                  I can see when looking at the log file for Child process 2 that is is successfully writing the heartbeat to the socket and is receiving an acknowledgement from the main process.

                  Can anyone suggest what could be wrong?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #10

                  @SPlatten said in Help with sockets:

                  Child process 1 will listen for acknowledgment on the local IP address on port 8124
                  Child process 2 will listen for acknowledgement on the local IP address on port 8125

                  I'll leave @Christian-Ehrlicher to sort out where you are. But I don't think you should be doing this anyway. Why are you allocating new, fixed ports for clients? You normally allow the client-side to pick a random, unused port number as its side of the connection to a fixed server port number.

                  • Your way, how will each child know which port number to pick?

                  • Even if you made this work somehow, how many server port numbers do you intend to reserve/use for your app? A reasonable rule is no more than 2, IMHO. You seem to intend 50 reserved ports for 50 clients....

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @SPlatten said in Help with sockets:

                    mpClient

                    Why do you need this? Are you aware it's getting overwritten each time a new connection is coming in?

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #11

                    @Christian-Ehrlicher , thank you, its stupid mistakes like that I needed pointing out, which you have, thanks, I'll attend to it.

                    Kind Regards,
                    Sy

                    SPlattenS 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @Christian-Ehrlicher , thank you, its stupid mistakes like that I needed pointing out, which you have, thanks, I'll attend to it.

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by SPlatten
                      #12

                      @Christian-Ehrlicher , I've added a class:

                          class clsClient : public QObject {
                          Q_OBJECT
                      
                          private:
                              QTcpSocket* mpClient;
                      
                          public:
                              clsClient(QTcpSocket* pConn);
                              ~clsClient();
                              QTcpSocket* psckClient() { return mpClient; }
                      
                          signals:
                              void newConnection(QTcpSocket& robjClient);
                      
                          public slots:
                              void onDataIn();
                              void onErrorOccurred(QAbstractSocket::SocketError socketError);
                          };
                      

                      The implementation:

                      /**
                       * @brief clsClient::clsClient
                       * @param pConn : Pointer to connection
                       */
                      clsClient::clsClient(QTcpSocket* pConn) : mpClient(pConn) {
                          Q_ASSERT_X(pConn!=nullptr, "clsClient::clsClient", "pConn is not valid!");
                          qdbg() << "New Connection";
                          QJsonObject objJSON;
                          objJSON.insert(clsJSON::mscszMsgType, clsJSON::mscszCmdInitial);
                          mpClient->write(QJsonDocument(objJSON).toJson(QJsonDocument::Compact));
                          //Signal new connection
                          emit newConnection(*pConn);
                          //Connect signals and slots
                          QObject::connect(pConn, &QTcpSocket::readyRead
                                      ,this, &clsClient::onDataIn);
                          QObject::connect(pConn, &QAbstractSocket::errorOccurred
                                      ,this, &clsClient::onErrorOccurred);
                      }
                      /**
                       * @brief clsClient::~clsClient
                       */
                      clsClient::~clsClient() {
                          if ( mpClient != nullptr ) {
                              QObject::disconnect(mpClient, &QAbstractSocket::disconnected
                                          ,mpClient, &QObject::deleteLater);
                              QObject::disconnect(mpClient, &QTcpSocket::readyRead
                                          ,this, &clsClient::onDataIn);
                              QObject::disconnect(mpClient, &QAbstractSocket::errorOccurred
                                          ,this, &clsClient::onErrorOccurred);
                              mpClient->close();
                              mpClient = nullptr;
                          }
                      }
                      /**
                       * @brief Slot to receive data in from client
                       */
                      void clsClient::onDataIn() {
                          if ( mpClient->bytesAvailable() == 0 ) {
                              return;
                          }
                          QByteArray arybytMsg(mpClient->readAll());
                          if ( arybytMsg.isEmpty() == true ) {
                              return;
                          }
                          //Does request start with a 'GET ' prefix?
                          bool blnHTTP = arybytMsg.startsWith(QString("GET ").toLatin1());
                          int intPos = 0;
                      
                          while( true ) {
                          //Find the start of the JSON object
                              int intOCB = arybytMsg.indexOf(clsJSON::msccOpenCurlyBracket, intPos);
                      
                              if ( intOCB == -1 ) {
                          //No opening curly bracket found, abort
                                  return;
                              }
                          //Update reference position
                              intPos = intOCB + 1;
                          //Find the end of the JSON object
                              int intCCB = intOCB, intOCBcnt = 1, intCCBcnt = 0;
                              bool blnFound = false;
                      
                              while( intCCB < arybytMsg.length() ) {
                          //Increment search position
                                  intCCB++;
                                  if ( arybytMsg[intCCB] == clsJSON::msccOpenCurlyBracket ) {
                          //Another opening curly found, increment count
                                      intOCBcnt++;
                                  } else if ( arybytMsg[intCCB] == clsJSON::msccCloseCurlyBracket ) {
                          //Increment closing curly count and compare with open count
                                      if ( ++intCCBcnt == intOCBcnt ) {
                          //Match found
                                          blnFound = true;
                          //Update reference position
                                          intPos = intCCB + 1;
                          //Stop this search and continue processing the rest of the packet
                                          break;
                                      }
                                  }
                              }
                              if ( blnFound != true ) {
                                  continue;
                              }
                          //Trancate the request to just the JSON
                              QString strRx(arybytMsg.mid(intOCB, intCCB - intOCB + 1));
                          //Advance to end of this JSON for next search
                              intOCB = intCCB + 1;
                          //Translate request, converting escaped characters
                              strRx = QUrl::fromPercentEncoding(strRx.toLatin1());
                      #if defined(DEBUG_SOCKETS)
                              qdbg() << QString("clsClient::onDataIn [%1]: %2").arg(strRx.length()).arg(strRx);
                      #endif
                          //Create JSON object
                              clsJSON objMyJSON(strRx);
                      
                              if ( objMyJSON.blnIsValid() != true ) {
                                  continue;
                              }
                          //JSON is valid
                              QJsonObject objJSON(objMyJSON.toQJsonObject());
                              clsJSON::blnDecodeAccordingToType(objJSON);
                      
                              if ( blnHTTP == true ) {
                          //Must be HTTP request, we need the module for the response
                                  QJsonObject::iterator itFind = objJSON.find(clsJSON::mscszModule);
                      
                                  if ( itFind != objJSON.end() ) {
                                      QString strCRLF(clsJSON::mscszCRLF), strModuleName = itFind.value().toString();
                                      QTextStream tsResponse(mpClient);
                                      tsResponse.setAutoDetectUnicode(true);
                          //Create thread to deal with request and send response
                                      tsResponse << "HTTP/1.0 200 Ok" + strCRLF +
                                                    "Content-Type: text/html; charset=\"utf-8\""
                                                    + strCRLF + strCRLF +
                                                    + "<h1>Request from " + strModuleName + "</h1>\n";
                                      mpClient->close();
                      
                                      if ( mpClient->state() == QAbstractSocket::UnconnectedState ) {
                                          delete this;
                                      }
                                  }
                              }
                          }
                      }
                      /**
                       * @brief This signal is emitted after an error occurred. The socketError
                       * parameter describes the type of error that occurred.
                       * @param socketError is not a registered metatype, so for queued connections,
                       * you will have to register it with Q_DECLARE_METATYPE() and
                       * qRegisterMetaType().
                       */
                      void clsClient::onErrorOccurred(QAbstractSocket::SocketError socketError) {
                          //Display error
                          qdbg() << "Socket error(" << socketError
                                 << ") https://www.google.com/search?rls=en&q=socket+error+code+"
                                 << socketError << "&ie=UTF-8&oe=UTF-8"
                                 << socketError << ".php";
                          delete this;
                      }
                      

                      And modified the class clsListener::onNewConnection:

                      void clsListener::onNewConnection() {
                          QTcpSocket* pConn = mpServer->nextPendingConnection();
                          QObject::connect(pConn, &QAbstractSocket::disconnected
                                          ,pConn, &QObject::deleteLater);
                          new clsClient(pConn);
                      }
                      

                      Its still not quite right, what else am I doing stupid?

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        Now you should add some debug output to see if two instances with different sockets are created and if you receive data.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        SPlattenS 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          Now you should add some debug output to see if two instances with different sockets are created and if you receive data.

                          SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by SPlatten
                          #14

                          @Christian-Ehrlicher , I have log files for each process, the logged data is to big to post...I can see that one process is receiving acknowledges to the heartbeats, the other isn't. From the main process log I can see that ack messages are being issued to each process but it appears that only one process is receiving the ack's.

                          Is there any queuing of packets received but the socket class or should I implement a queue when a packet is received? I'm wondering if the overhead of receiving and processing each packet is causing the server to miss receipts?

                          I've modified the onDataIn slot, so it just logs the received data to file then exits and I can see without processing the received data that it does look like it is receiving everything:

                          D00000000000000000001S000000000000:This module listening on port: 8123
                          D00000000000000000002S000000000307:L00000192Fsimon.js[void clsScriptHelper::log]:
                          D00000000000000000003S000000000307:************** setup ****************
                          D00000000000000000004S000000000307:L00000150Fsimon.js[void clsScriptHelper::log]:
                          D00000000000000000005S000000000307:************** connect ****************
                          D00000000000000000006S000000000312:Process: Arg[1]: 8123
                          D00000000000000000007S000000000312:Process: Arg[2]: 8124
                          D00000000000000000008S000000000312:Process: Arg[3]: 25332
                          D00000000000000000009S000000000312:Process: Arg[4]: f1
                          D00000000000000000010S000000000312:Process is NOT running, launching
                          D00000000000000000011S000000000314:L00000100Fsimon.js[void clsScriptHelper::log]:
                          D00000000000000000012S000000000314:************** setupFIO **********
                          D00000000000000000013S000000000314:clsModule::onSendJSON (later): {"commands":[{"check":"a","command":"read","length":1,"mode":"binary","type":"char"},{"byteorder":"0,1","check":"32767","command":"read","length":2,"mode":"binary","type":"short"},{"byteorder":"0,1,2,3","check":"2147483647","command":"read","length":4,"mode":"binary","type":"int"},{"byteorder":"0,1,2,3,4,5,6,7","check":"9223372036854775807","command":"read","length":8,"mode":"binary","type":"long"},{"byteorder":"0,1,2,3","check":"3.4028234663852886e+38","command":"read","length":4,"mode":"binary","type":"float"}],"file":"~/XMLMPAM/config/test.dat","msgType":"notify","port":8124,"source":"XMLMPAM"}
                          D00000000000000000014S000000000320:Process: Arg[1]: 8123
                          D00000000000000000015S000000000320:Process: Arg[2]: 8125
                          D00000000000000000016S000000000320:Process: Arg[3]: 25332
                          D00000000000000000017S000000000320:Process: Arg[4]: f2
                          D00000000000000000018S000000000320:Process is NOT running, launching
                          D00000000000000000019S000000000321:************** setupFIO **********
                          D00000000000000000020S000000000321:clsModule::onSendJSON (later): {"commands":[{"check":"a","command":"read","length":1,"mode":"binary","type":"char"},{"byteorder":"0,1","check":"32767","command":"read","length":2,"mode":"binary","type":"short"},{"byteorder":"0,1,2,3","check":"2147483647","command":"read","length":4,"mode":"binary","type":"int"},{"byteorder":"0,1,2,3,4,5,6,7","check":"9223372036854775807","command":"read","length":8,"mode":"binary","type":"long"},{"byteorder":"0,1,2,3","check":"3.4028234663852886e+38","command":"read","length":4,"mode":"binary","type":"float"}],"file":"~/XMLMPAM/config/test.dat","msgType":"notify","port":8125,"source":"XMLMPAM"}
                          D00000000000000000021S000000000572:New Connection
                          D00000000000000000022S000000000572:New Connection
                          D00000000000000000023S000000000573:{"msgID":"1","msgType":"ready","port":8123,"source":"f2"}
                          D00000000000000000024S000000000573:{"msgID":"1","msgType":"ready","port":8123,"source":"f1"}
                          D00000000000000000025S000000000825:Process: mdFileIO started, PID: 25347
                          D00000000000000000026S000000000825:Process: mdFileIO started, PID: 25343
                          D00000000000000000027S000000002473:{"msgID":"2","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000028S000000002473:{"msgID":"2","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000029S000000004385:{"msgID":"3","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000030S000000004385:{"msgID":"3","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000031S000000006382:{"msgID":"4","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000032S000000006382:{"msgID":"4","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000033S000000008382:{"msgID":"5","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000034S000000008382:{"msgID":"5","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000035S000000010383:{"msgID":"6","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000036S000000010386:{"msgID":"6","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000037S000000012382:{"msgID":"7","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000038S000000012382:{"msgID":"7","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000039S000000014387:{"msgID":"8","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000040S000000014387:{"msgID":"8","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000041S000000016382:{"msgID":"9","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000042S000000016382:{"msgID":"9","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000043S000000018384:{"msgID":"10","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000044S000000018384:{"msgID":"10","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000045S000000020383:{"msgID":"11","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000046S000000020383:{"msgID":"11","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000047S000000022382:{"msgID":"12","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000048S000000022382:{"msgID":"12","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000049S000000024383:{"msgID":"13","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000050S000000024383:{"msgID":"13","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000051S000000026382:{"msgID":"14","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000052S000000026383:{"msgID":"14","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000053S000000028383:{"msgID":"15","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000054S000000028383:{"msgID":"15","msgType":"hb","port":8123,"source":"f2"}
                          D00000000000000000055S000000030383:{"msgID":"16","msgType":"hb","port":8123,"source":"f1"}
                          D00000000000000000056S000000030383:{"msgID":"16","msgType":"hb","port":8123,"source":"f2"}
                          

                          Kind Regards,
                          Sy

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            I don't want to see your logfiles. I told you where you should add logs and what you should see there. It's up to you to do the debug work.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            1

                            • Login

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