Results 1 to 5 of 5

Thread: keep Sending all pending data to the server after the QTcpSocket is closed

  1. #1
    Join Date
    Oct 2016
    Posts
    7
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Symbian S60

    Red face keep Sending all pending data to the server after the QTcpSocket is closed

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: keep Sending all pending data to the server after the QTcpSocket is closed

    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.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Christina flowers (11th October 2016)

  4. #3
    Join Date
    Oct 2016
    Posts
    7
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Symbian S60

    Default Re: keep Sending all pending data to the server after the QTcpSocket is closed

    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
    Qt Code:
    1. void FenetreRoom::closeEvent(QCloseEvent *event)
    2. {
    3. event->ignore() ;
    4. QDataStream out(&paquet, QIODevice::WriteOnly);
    5. QString messageAEnvoyer ="out"+nameVisiter+"/"+QString::number(idUser)+"/"+QString::number(posActuelle);
    6. out << (quint16) 0;
    7. out << messageAEnvoyer;
    8. out.device()->seek(0);
    9. out << (quint16) (paquet.size() - sizeof(quint16));
    10. socket->write(paquet);
    11. while(!(socket->waitForReadyRead()));
    12. event->accept();}
    To copy to clipboard, switch view to plain text mode 
    i added this line while(!(socket->waitForReadyRead()));
    if there is any better way i will be thankfull.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: keep Sending all pending data to the server after the QTcpSocket is closed

    Well, here you are waiting for "readyRead", i.e. for incoming data.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    Christina flowers (11th October 2016)

  7. #5
    Join Date
    Oct 2016
    Posts
    7
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Symbian S60

    Default Re: keep Sending all pending data to the server after the QTcpSocket is closed

    thanks a lot

Similar Threads

  1. saving data to server through QTcpSocket
    By toufic.dbouk in forum Qt Programming
    Replies: 3
    Last Post: 6th January 2013, 21:48
  2. Replies: 0
    Last Post: 28th October 2012, 20:01
  3. Replies: 7
    Last Post: 12th September 2011, 10:52
  4. QTcpSocket get disconnected when server is sending huge data
    By KernelCoder in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 08:45
  5. Replies: 8
    Last Post: 18th December 2010, 15:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.