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. QTcpSocket most correct way to handle different incoming data
QtWS25 Last Chance

QTcpSocket most correct way to handle different incoming data

Scheduled Pinned Locked Moved General and Desktop
network
7 Posts 3 Posters 2.0k Views
  • 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.
  • S Offline
    S Offline
    sergboec
    wrote on last edited by
    #1

    Hello.
    I am writing network client application
    and i want to request different kind of data from server

    Server will return me different data for every kind of "Request".
    Currently i making something like:

    void requestFunction(QString request){
       if (request == "1"){
         disconnect(socket,SIGNAL(readyRead),this,SLOT(fun1))
         connect(socket,SIGNAL(readyRead),this,SLOT(fun2))
         socket.write("1")
       }
       if (request == "2"){
         disconnect(socket,SIGNAL(readyRead),this,SLOT(fun2))
         connect(socket,SIGNAL(readyRead),this,SLOT(fun1))
         socket.write("2")
       }
    }
    
    void fun1(){
     /*handle data with type 1*/
    }
    
    void fun2(){
      /* handle data with type 2*/
    }
    

    Is my code have correct behavior?

    K 1 Reply Last reply
    0
    • S sergboec

      Hello.
      I am writing network client application
      and i want to request different kind of data from server

      Server will return me different data for every kind of "Request".
      Currently i making something like:

      void requestFunction(QString request){
         if (request == "1"){
           disconnect(socket,SIGNAL(readyRead),this,SLOT(fun1))
           connect(socket,SIGNAL(readyRead),this,SLOT(fun2))
           socket.write("1")
         }
         if (request == "2"){
           disconnect(socket,SIGNAL(readyRead),this,SLOT(fun2))
           connect(socket,SIGNAL(readyRead),this,SLOT(fun1))
           socket.write("2")
         }
      }
      
      void fun1(){
       /*handle data with type 1*/
      }
      
      void fun2(){
        /* handle data with type 2*/
      }
      

      Is my code have correct behavior?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @sergboec

      This is possible to do.
      However, I am wondering you like to change the connect each time depending on your request sent? You know the request sent and store it. Whenever you receive a signal from readyRead you will enter your slot function. There you can use the information of the request sent and divert.

      Vote the answer(s) that helped you to solve your issue(s)

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

        Why not just have one ready read function that you keep connected at all times. Upon data receive, examine it and hand off to specific handlers?

        My concern would be that during your disconnect periods you could potentially miss something. I'm not sure how big your packets of data are but ready read is not guaranteed to return everything in a single call.

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

          @koahnig
          thanks. idea with the flag is cool. i will test it.
          @SysTech
          there is no ability to examine data. data is just a float massive.

          S 1 Reply Last reply
          0
          • S sergboec

            @koahnig
            thanks. idea with the flag is cool. i will test it.
            @SysTech
            there is no ability to examine data. data is just a float massive.

            S Offline
            S Offline
            SysTech
            wrote on last edited by
            #5

            @sergboec

            Ok yes that would make it hard. Is the server of your design or someone else? I mean just transmitting a float without any details could be confusing.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sergboec
              wrote on last edited by
              #6

              @SysTech
              server isn't designed by me.
              i understand that with header it will be easier, but main prerequisite when server was designed was low network bandwidth :(

              S 1 Reply Last reply
              0
              • S sergboec

                @SysTech
                server isn't designed by me.
                i understand that with header it will be easier, but main prerequisite when server was designed was low network bandwidth :(

                S Offline
                S Offline
                SysTech
                wrote on last edited by
                #7

                @sergboec

                Do the numbers come back in a "known" sequence upon each request?

                IE the first request gives #1, second #2 etc? If so you could still do a single ready read and just have a counter that serves to vector your received data to different handlers.

                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