Results 1 to 6 of 6

Thread: QTcpSocket server keeps reading the messages from the client

  1. #1
    Join Date
    Jul 2012
    Location
    UK
    Posts
    17
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket server keeps reading the messages from the client

    Qt Code:
    1. void FortuneThread::run()
    2. {
    3. QTcpSocket tcpSocket;
    4. if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
    5. emit error(tcpSocket.error());
    6. return;
    7. }
    8.  
    9. QByteArray block;
    10. QDataStream out(&block, QIODevice::WriteOnly);
    11. out.setVersion(QDataStream::Qt_4_0);
    12. out << (quint16)0;
    13. out << text;
    14. out.device()->seek(0);
    15. out << (quint16)(block.size() - sizeof(quint16));
    16.  
    17. tcpSocket.write(block);
    18.  
    19. // Here !!
    20.  
    21. tcpSocket.disconnectFromHost();
    22. tcpSocket.waitForDisconnected();
    23. }
    To copy to clipboard, switch view to plain text mode 


    Say, in the fortune thread example, after sending (write) the text to the client, I want this particular thread to keep alive and keep reading different messages sent from the client, until client closes the connection (I guess by when client will invoke QTcpSocket ::abort() or QTcpSocket::disconnectfromHost()). Can you please suggest me how can I do this?

    Thanks

  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: QTcpSocket server keeps reading the messages from the client

    waitForReadyRead?

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

    saifulkhan (24th October 2013)

  4. #3
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QTcpSocket server keeps reading the messages from the client

    I would use this http://www.bogotobogo.com/Qt/Qt5_QTc...ent_Server.php
    And do
    tcpSocket.disconnectFromHost();
    tcpSocket.waitForDisconnected();
    from client

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket server keeps reading the messages from the client

    Quote Originally Posted by scarecr0w132 View Post
    I would use this http://www.bogotobogo.com/Qt/Qt5_QTc...ent_Server.php
    And do
    tcpSocket.disconnectFromHost();
    tcpSocket.waitForDisconnected();
    from client
    I wouldn't. The tutorial is wrong.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    saifulkhan (24th October 2013)

  7. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket server keeps reading the messages from the client

    Quote Originally Posted by wysota View Post
    I wouldn't. The tutorial is wrong.
    So agree with you.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. The following user says thank you to spirit for this useful post:

    saifulkhan (24th October 2013)

  9. #6
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket server keeps reading the messages from the client

    Quote Originally Posted by wysota
    The tutorial is wrong.
    Quote Originally Posted by spirit
    So agree with you.
    May you explain why is that tutorial wrong ?

  10. The following user says thank you to toufic.dbouk for this useful post:

    scarecr0w132 (25th October 2013)

Similar Threads

  1. Replies: 8
    Last Post: 7th June 2011, 11:39
  2. TCP Client / Server
    By bmarsden in forum Qt Programming
    Replies: 4
    Last Post: 8th October 2010, 10:18
  3. Client Server
    By electronicboy in forum General Programming
    Replies: 3
    Last Post: 29th October 2009, 10:10
  4. Server is not reading from client
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 30th November 2006, 16:45
  5. client-server how?
    By nongentesimus in forum Newbie
    Replies: 6
    Last Post: 28th November 2006, 09:25

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
  •  
Qt is a trademark of The Qt Company.