Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Multithreaded server GUI program crashing as soon as client connects

Multithreaded server GUI program crashing as soon as client connects

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
13 Posts 5 Posters 2.2k 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.
  • F Offline
    F Offline
    fari35
    wrote on last edited by fari35
    #1
    This post is deleted!
    JonBJ 1 Reply Last reply
    0
    • F fari35

      This post is deleted!

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

      @fari35
      What is the stack trace when it crashes running inside debugger? Or, what qDebug() statements did you put in to see where it was getting to?

      F 1 Reply Last reply
      0
      • JonBJ JonB

        @fari35
        What is the stack trace when it crashes running inside debugger? Or, what qDebug() statements did you put in to see where it was getting to?

        F Offline
        F Offline
        fari35
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @fari35 said in Multithreaded server GUI program crashing as soon as client connects:

          QTcpSocket *clientSocket = tcpServer->nextPendingConnection();

          Because your member variable is uninitialized. Apart from this it's completely wrong. The threaded fortune example shows you how to create a QTcpSocket out of a socketDescriptor.

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

          F 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @fari35 said in Multithreaded server GUI program crashing as soon as client connects:

            QTcpSocket *clientSocket = tcpServer->nextPendingConnection();

            Because your member variable is uninitialized. Apart from this it's completely wrong. The threaded fortune example shows you how to create a QTcpSocket out of a socketDescriptor.

            F Offline
            F Offline
            fari35
            wrote on last edited by fari35
            #5

            @Christian-Ehrlicher I've removed these lines completely but it is still crashing. I'm new to Qt and I'm not getting it what is wrong here.

             QTcpSocket tcpSocket;
            
                if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
                    emit error(tcpSocket.error());
                    return;
            

            and after initializing the tcpserver like this :

            mythread::mythread(int socketDescriptor, const QString &fortune, QObject *parent)
                : QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
            {
            
            
                tcpServer = new QTcpServer(this);
            }
            

            I'm getting this error:

            qt.core.qobject.connect: QObject::connect(QAbstractSocket, Unknown): invalid nullptr parameter
            qt.core.qobject.connect: QObject::connect(QIODevice, mythread): invalid nullptr parameter
            qt.core.qobject.connect: QObject::connect(QAbstractSocket, mythread): invalid nullptr parameter
            
            kshegunovK 1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Please follow and use the multi threaded QTcpServer example as I already told you.

              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
              • F fari35

                @Christian-Ehrlicher I've removed these lines completely but it is still crashing. I'm new to Qt and I'm not getting it what is wrong here.

                 QTcpSocket tcpSocket;
                
                    if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
                        emit error(tcpSocket.error());
                        return;
                

                and after initializing the tcpserver like this :

                mythread::mythread(int socketDescriptor, const QString &fortune, QObject *parent)
                    : QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
                {
                
                
                    tcpServer = new QTcpServer(this);
                }
                

                I'm getting this error:

                qt.core.qobject.connect: QObject::connect(QAbstractSocket, Unknown): invalid nullptr parameter
                qt.core.qobject.connect: QObject::connect(QIODevice, mythread): invalid nullptr parameter
                qt.core.qobject.connect: QObject::connect(QAbstractSocket, mythread): invalid nullptr parameter
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @fari35 said in Multithreaded server GUI program crashing as soon as client connects:

                @Christian-Ehrlicher I've removed these lines completely but it is still crashing. I'm new to Qt and I'm not getting it what is wrong here.

                If you're not well versed in Qt, then you really shouldn't jump into the deep dark ocean that is threading.
                Take it slow instead; make the server work without threads first, Qt's sockets are already asynchronous so you often don't need threads to begin with.

                Read and abide by the Qt Code of Conduct

                F 1 Reply Last reply
                4
                • kshegunovK kshegunov

                  @fari35 said in Multithreaded server GUI program crashing as soon as client connects:

                  @Christian-Ehrlicher I've removed these lines completely but it is still crashing. I'm new to Qt and I'm not getting it what is wrong here.

                  If you're not well versed in Qt, then you really shouldn't jump into the deep dark ocean that is threading.
                  Take it slow instead; make the server work without threads first, Qt's sockets are already asynchronous so you often don't need threads to begin with.

                  F Offline
                  F Offline
                  fari35
                  wrote on last edited by fari35
                  #8

                  @kshegunov If I assign threads to each client when they are connected so is it the same if I use tcp sockets without threads? all the clients can connect at the same time? I mean it will behave in the same way?

                  aha_1980A JonBJ kshegunovK 3 Replies Last reply
                  0
                  • F fari35

                    @kshegunov If I assign threads to each client when they are connected so is it the same if I use tcp sockets without threads? all the clients can connect at the same time? I mean it will behave in the same way?

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @fari35 most Qt classes are async, so no threads are needed.

                    so yes, you can create a server without threads and it can handle multiple clients parallel.

                    threads only help if you have many clients as you can distribute the work on sevral cores.

                    Regards

                    Qt has to stay free or it will die.

                    F 1 Reply Last reply
                    0
                    • F fari35

                      @kshegunov If I assign threads to each client when they are connected so is it the same if I use tcp sockets without threads? all the clients can connect at the same time? I mean it will behave in the same way?

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

                      @fari35
                      @Christian-Ehrlicher referred you earlier to the multi-threaded server example https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html . You should look at the single-threaded one https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html . The latter is simpler, and should suffice unless you have further needs. At least try out it out, without multiple threads, to understand and see how you go.

                      1 Reply Last reply
                      0
                      • aha_1980A aha_1980

                        @fari35 most Qt classes are async, so no threads are needed.

                        so yes, you can create a server without threads and it can handle multiple clients parallel.

                        threads only help if you have many clients as you can distribute the work on sevral cores.

                        Regards

                        F Offline
                        F Offline
                        fari35
                        wrote on last edited by fari35
                        #11

                        @aha_1980 yes so basically I want to handle many clients that why I'm doing multi threading

                        JonBJ 1 Reply Last reply
                        0
                        • F fari35

                          @aha_1980 yes so basically I want to handle many clients that why I'm doing multi threading

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

                          @fari35
                          And did you bother to read the post I just made above referring you to the standard single-threaded server which handles multiple clients without threads?

                          1 Reply Last reply
                          0
                          • F fari35

                            @kshegunov If I assign threads to each client when they are connected so is it the same if I use tcp sockets without threads? all the clients can connect at the same time? I mean it will behave in the same way?

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

                            @fari35 said in Multithreaded server GUI program crashing as soon as client connects:

                            If I assign threads to each client when they are connected so is it the same if I use tcp sockets without threads? all the clients can connect at the same time? I mean it will behave in the same way?

                            That's what I wrote, yes. Unless you decide to do something very long in that main thread (which then you'd decide to offload to a worker thread instead), then I see no reason for you to thread the TCP needlessly. As a matter of fact last year I deployed a decently sized project that had TCP communication (server + clients) without making it threaded, sometimes there's just no reason to complicate it.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            4

                            • Login

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