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. what is the best way to make a network app? multi-thread...heart-beating...
Forum Updated to NodeBB v4.3 + New Features

what is the best way to make a network app? multi-thread...heart-beating...

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 3.3k 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #1

    what is the best way to make a network app? multi-thread...heart-beating...
    i seems that there is no msg loop explicitly in main()...
    i want to read data from the server in a loop.

    is there any good example to create a thread and do this network thing(eg. heart-beating)?

    thank you!

    M 1 Reply Last reply
    0
    • sneubertS Offline
      sneubertS Offline
      sneubert
      wrote on last edited by
      #2

      Hi opengpu2,
      if you´re not tied to Qt have a look at boost::asio.

      1 Reply Last reply
      0
      • O opengpu2

        what is the best way to make a network app? multi-thread...heart-beating...
        i seems that there is no msg loop explicitly in main()...
        i want to read data from the server in a loop.

        is there any good example to create a thread and do this network thing(eg. heart-beating)?

        thank you!

        M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        @opengpu2 said:

        what is the best way to make a network app? multi-thread...heart-beating...

        The right answer is: "There isn't the BEST way". It depends of your application feauters and requirements

        i seems that there is no msg loop explicitly in main()...
        i want to read data from the server in a loop.

        Qt usually prefers the Asynchronous approach but you can read from a socket with blocking API like QUdpSocket::readDatagram()

        is there any good example to create a thread and do this network thing(eg. heart-beating)?

        thank you!

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SysTech
          wrote on last edited by SysTech
          #4

          Alternatively you can do threads and timers to sort of make your own loop.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            opengpu2
            wrote on last edited by
            #5

            yes, i don't want to use boost, and i want to use Qt.
            what is the API or demo that shows how to create a thread in Qt?
            and where should i create the timer? in the main thread or the new thread?
            and how to write the slot...thank you.
            and i think this timer method is sort of heart-beating, right?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by mcosta
              #6

              Hi,

              as I said before you don't necessarily need a Thread for this work; Do you need to receive data from server or request data to the server?

              1. Receive Data (f.i. using a UDP socket)
              void ServerConnection::initSocket()
              {
                  udpSocket = new QUdpSocket(this);
                  udpSocket->bind(QHostAddress::LocalHost, 7755);
              
                  connect(udpSocket, SIGNAL(readyRead()),
                          this, SLOT(readPendingDatagrams()));
              }
              
              void ServerConnection::readPendingDatagrams()
              {
                  while (udpSocket->hasPendingDatagrams()) {
                      QByteArray datagram;
                      datagram.resize(udpSocket->pendingDatagramSize());
                      QHostAddress sender;
                      quint16 senderPort;
              
                      udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
              
                      processTheDatagram(datagram);
                  }
              }
              
              1. Request Data
              myTimer = new QTimer(this);
              connect(myTimer, &QTimer::timeout, this, &MyClass::requestData);
              
              void MyClass::requestData()
              {
                 // Request data to the server
              }
              

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • O Offline
                O Offline
                opengpu2
                wrote on last edited by opengpu2
                #7

                thank you very much.
                i dont use QUdpSocket..i use my own network classes.
                right now i Tick the network io in the new thread. that's why i want to readData in msg loop, or heart-beating...

                maybe Tick the network io in the new thread, and use a timer as heart-beating in the main thread to readData() is a good method?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mcosta
                  wrote on last edited by
                  #8

                  Hi,

                  if you don't need to use high-speed communication or a lot of data you can do the whole processing (tick and readData) without threads

                  Once your problem is solved don't forget to:

                  • Mark the thread as SOLVED using the Topic Tool menu
                  • Vote up the answer(s) that helped you to solve the issue

                  You can embed images using (http://imgur.com/) or (http://postimage.org/)

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    opengpu2
                    wrote on last edited by
                    #9

                    u mean i can put all this in the Times' slot()?

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      opengpu2
                      wrote on last edited by
                      #10

                      and if i want high-speed communication or a lot of data, what should i do ?
                      it seems that u prefer not to create a new thread...why ? what is the disadvantage?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mcosta
                        wrote on last edited by
                        #11

                        Hi,

                        I think you should use something only if you really need it.
                        Qt is designed to work in asynchronous mode so you can handle multiple source of data in a single-thread application.

                        You should move to multi-threading when you need the handle long time processing or when you need to non block who's sending you data.

                        Once your problem is solved don't forget to:

                        • Mark the thread as SOLVED using the Topic Tool menu
                        • Vote up the answer(s) that helped you to solve the issue

                        You can embed images using (http://imgur.com/) or (http://postimage.org/)

                        1 Reply Last reply
                        0
                        • O Offline
                          O Offline
                          opengpu2
                          wrote on last edited by
                          #12

                          thank you
                          i need the network io always Tick().
                          and i also need readData from the server all the time...eg. in a msg loop in windows..
                          but there seems no msg loop in Qt, so i wanted to use a timer as heart-beat to put readData in it.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mcosta
                            wrote on last edited by
                            #13

                            Hi,

                            do you need to read data only at tick?? In that case use the timeout signal to read data

                            connect (timer, &QTimer::timeout, this, &MyClass::readData);
                            
                            void MyClass::readData() 
                            {
                               // Read from server
                            }
                            

                            Once your problem is solved don't forget to:

                            • Mark the thread as SOLVED using the Topic Tool menu
                            • Vote up the answer(s) that helped you to solve the issue

                            You can embed images using (http://imgur.com/) or (http://postimage.org/)

                            1 Reply Last reply
                            0
                            • O Offline
                              O Offline
                              opengpu2
                              wrote on last edited by
                              #14

                              yes,
                              in addition, i want to put the network io Tick into a new thread.
                              the readData() in the timeout slot.

                              1 Reply Last reply
                              0
                              • O Offline
                                O Offline
                                opengpu2
                                wrote on last edited by
                                #15

                                and what is the normal way to create a new thread? (i want sth like ::CreateThread in window)

                                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