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. Multiclient TCP server
Qt 6.11 is out! See what's new in the release blog

Multiclient TCP server

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 1.9k 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.
  • B Bremenpl

    Hello there,
    I want to create a simple TCP server application that would handle multiple TCP clients. Normally I would create a separate thread for each connected client. In the past I have seen solutions concerning subclassing the QThread like this one.

    But I feel like this is an obsolete and an overkill approach (I do not sublass QThread for a long time now). What would be the recommended modern Qt way of tackling this topic? Is using QTcpServer and QTcpClient even the way to go, or is it unnecessarily to low level approach?

    I would appreciate all help.

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #2

    hi @Bremenpl

    In the past I have seen solutions concerning subclassing the QThread like this one.

    But I feel like this is an obsolete and an overkill approach

    it is, mostly born out of a convoluted documentation

    Take a look at the threaded fortune server example
    https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html

    should get you started


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    B 1 Reply Last reply
    1
    • J.HilkJ J.Hilk

      hi @Bremenpl

      In the past I have seen solutions concerning subclassing the QThread like this one.

      But I feel like this is an obsolete and an overkill approach

      it is, mostly born out of a convoluted documentation

      Take a look at the threaded fortune server example
      https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html

      should get you started

      B Offline
      B Offline
      Bremenpl
      wrote on last edited by
      #3

      @J-Hilk Thank you for answer. I have seen this example already- it is subclassing the QThread as well.

      lprzenioslo.zut.edu.pl

      J.HilkJ 1 Reply Last reply
      0
      • B Bremenpl

        @J-Hilk Thank you for answer. I have seen this example already- it is subclassing the QThread as well.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #4

        @Bremenpl
        you're right, I assumed it was updated 😡

        Well, how you're actually supposed to do it, is now in the documentation,
        See the detailed description of the QThread class
        https://doc.qt.io/qt-5/qthread.html#details

        The first section


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        B 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @Bremenpl
          you're right, I assumed it was updated 😡

          Well, how you're actually supposed to do it, is now in the documentation,
          See the detailed description of the QThread class
          https://doc.qt.io/qt-5/qthread.html#details

          The first section

          B Offline
          B Offline
          Bremenpl
          wrote on last edited by
          #5

          @J-Hilk Thank you for the link. In this case it is the way I am doing it for a while now. I thought maybe there are some new higher level approaches to this. Thank you for help!

          lprzenioslo.zut.edu.pl

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

            The only thing you have to take care when subclassing QThread is that the (possible) slots in your object derived from QThread are executed in the correct thread by moving the derived object into the thread. Otherwise there is no real difference.

            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
            • B Bremenpl

              @J-Hilk Thank you for the link. In this case it is the way I am doing it for a while now. I thought maybe there are some new higher level approaches to this. Thank you for help!

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

              Here, have a spin. Opinions are also appreciated.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              3
              • B Bremenpl

                Hello there,
                I want to create a simple TCP server application that would handle multiple TCP clients. Normally I would create a separate thread for each connected client. In the past I have seen solutions concerning subclassing the QThread like this one.

                But I feel like this is an obsolete and an overkill approach (I do not sublass QThread for a long time now). What would be the recommended modern Qt way of tackling this topic? Is using QTcpServer and QTcpClient even the way to go, or is it unnecessarily to low level approach?

                I would appreciate all help.

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

                @Bremenpl said in Multiclient TCP server:

                Normally I would create a separate thread for each connected client

                This is a myth. Follow @kshegunov 's link

                "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

                B 1 Reply Last reply
                3
                • VRoninV VRonin

                  @Bremenpl said in Multiclient TCP server:

                  Normally I would create a separate thread for each connected client

                  This is a myth. Follow @kshegunov 's link

                  B Offline
                  B Offline
                  Bremenpl
                  wrote on last edited by
                  #9

                  @VRonin Thank you for answer.
                  I did mean a generic approach, in which the separate thread indeed is needed. Maybe in this design it will be unnecessary, but that's is also why I wanted to hear some opinions.

                  lprzenioslo.zut.edu.pl

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

                    1 client = 1 thread is actually unsustainable once the number of connected clients start increasing.
                    In the code linked to the chat example, both in the simple and advanced projects, you can easily see the clients are actually distributed among QThread::idealThreadCount() threads rather than having 1 per client.
                    It also only makes sense if there is some actual work to do in that secondary thread

                    "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

                    B 1 Reply Last reply
                    1
                    • VRoninV VRonin

                      1 client = 1 thread is actually unsustainable once the number of connected clients start increasing.
                      In the code linked to the chat example, both in the simple and advanced projects, you can easily see the clients are actually distributed among QThread::idealThreadCount() threads rather than having 1 per client.
                      It also only makes sense if there is some actual work to do in that secondary thread

                      B Offline
                      B Offline
                      Bremenpl
                      wrote on last edited by
                      #11

                      @VRonin said in Multiclient TCP server:

                      idealThreadCount

                      I see, I will check this topic out, sounds reasonable. Thank you.

                      lprzenioslo.zut.edu.pl

                      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