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 serverServer 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?
-
Hello.
I am writing network client application
and i want to request different kind of data from serverServer 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?
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.
-
-
@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 :(