QTcpSocket most correct way to handle different incoming data



  • 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?


  • Moderators

    @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.



  • 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.



  • @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.



  • @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.



  • @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 :(



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.