View Full Version : keep Sending all pending data to the server after the QTcpSocket is closed

Christina flowers
11th October 2016, 07:07
Hi everyone, i developed a client-server programm using the great Qt Framework.my problem is: in the client side i have a QMainWindows in witch i defined a QTcpSocket in order to change dada with the server. before closing the QMainWindow i have to sent to the server that a specified client is disconnected. i use that by implementing the closeEvent methode. but when i closed the QmainWindows it seems that the socket is closed also and all pending data are lost.so how to check if all data were sent to do server before the QMainWIndows was closed or simply avoiding the close of the socket untill all remaining data are sent. can you please help me, thank you so much in advance

11th October 2016, 09:49
You could try calling waitForBytesWritten() on the socket when you've accepted the close.

In any case your server should probably also be able to handle a disconnecting client, as that could happen for other reasons than client exit.


Christina flowers
11th October 2016, 13:22
thank you so much for your reply. i tried this and it seems it works.
look what i have changed in my code. before calling the accepte methode in closeEvent Methode i do like that

void FenetreRoom::closeEvent(QCloseEvent *event)
event->ignore() ;
QDataStream out(&paquet, QIODevice::WriteOnly);
QString messageAEnvoyer ="out"+nameVisiter+"/"+QString::number(idUser)+"/"+QString::number(posActuelle);
out << (quint16) 0;
out << messageAEnvoyer;
out << (quint16) (paquet.size() - sizeof(quint16));

i added this line while(!(socket->waitForReadyRead()));
if there is any better way i will be thankfull.

11th October 2016, 14:08
Well, here you are waiting for "readyRead", i.e. for incoming data.


Christina flowers
11th October 2016, 17:02
thanks a lot