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. QTcpServer that responds to multiple QTcpSockets at the same time.
Forum Updated to NodeBB v4.3 + New Features

QTcpServer that responds to multiple QTcpSockets at the same time.

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.8k Views 3 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.
  • BonganiB Offline
    BonganiB Offline
    Bongani
    wrote on last edited by
    #1

    I am developing a QTcpServer which will forward TCP data to another server which takes -/+ 1min to respond. The problem is when the data comes back, I have to send it to the correct socket which requested the data.

    What my server does is strip the authentication data or login details every time a request or data is sent from the client. after validating the data on the MYSQL database i have to forward it to another server as an HTTP request. When the response arrives 1 - 2 mins later, i have to write it back on the correct server.

    Is there any kind of approach i can take for my QTcpServer to be able to know which Socket requested what data and be able to respond to the correct one when the data return from another server.

    Please advise.

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

      Hi,

      What are you using to send the request to that other server ?

      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
      0
      • BonganiB Offline
        BonganiB Offline
        Bongani
        wrote on last edited by
        #3

        @SGaist , It's running AJAX so am using QNetworkRequest to make a XmlHttpRequest.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          you can use QAbstractSocket::socketDescriptor() as ID of the destination but you will need a way to link a request to a response i.e. when an answer comes back to you you have to find a way to identify what the question was

          Thread1

          • establish connection between server and HTTP server

          Thread2:

          • started when connection is enstablished between Server and Client
          • receives the requests, passes it to thread 1 together with its socketDescriptor

          Thread1

          • receives the request, stores the socketDescriptor to remember who asked the question
          • receives the answer, it finds out what the question was and who asked it
          • sends the response to the socket with the asker socketDescriptor

          Thread2:

          • receives the replay and forwards it to the 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
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Wouldn't a QMap<QNetworkReply *, QTcpSocket *> be enough ? That way it would be possible to associate the reply of the secondary request to the socket where the original stuff is coming from.

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

            VRoninV 1 Reply Last reply
            0
            • SGaistS SGaist

              Wouldn't a QMap<QNetworkReply *, QTcpSocket *> be enough ? That way it would be possible to associate the reply of the secondary request to the socket where the original stuff is coming from.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @SGaist said in QTcpServer that responds to multiple QTcpSockets at the same time.:

              Wouldn't a QMap<QNetworkReply *, QTcpSocket *> be enough ?

              Depends if the bottleneck is waiting for the response or "validating the data on the MYSQL database"

              "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
              • BonganiB Offline
                BonganiB Offline
                Bongani
                wrote on last edited by
                #7

                @VRonin using the Threading technique, how do i make sure that Thread 2 doesn't respond to two requests at the same time, which i think will crash the application. using QMutex ?

                VRoninV kshegunovK 2 Replies Last reply
                0
                • BonganiB Bongani

                  @VRonin using the Threading technique, how do i make sure that Thread 2 doesn't respond to two requests at the same time, which i think will crash the application. using QMutex ?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  You could build a queue. requests come in, they get queued together with who asked it and thread 2 will process the queue

                  "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
                  • BonganiB Bongani

                    @VRonin using the Threading technique, how do i make sure that Thread 2 doesn't respond to two requests at the same time, which i think will crash the application. using QMutex ?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    Use a session object. Look here for a full example of a threaded TCP server.

                    Read and abide by the Qt Code of Conduct

                    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