Results 1 to 5 of 5

Thread: Trouble with QTcpSocket

  1. #1
    Join Date
    Jun 2010
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Trouble with QTcpSocket

    Good afternoon,
    I am using QTcpServer and QTcpSocket in my application to transfer data, mainly text.
    So, the problem appears when i try to send very fast multiple packets.

    As an example:

    The server needs to send text like this:

    stream << numberOfStringsToSend;
    for(int i = 0; i < numberOfStringsToSend; i++){
    stream << textForThisIteration(i);
    }

    It seems to be sending everything, but the client doesn't receive it properly. In the client i can get as much as no packet at all or i can get everything correctly, its almost random. Im using the readyRead signal to read from the socket with a QDataStream.

    Any hint to solve this problem would be apreciated :x The only way i can think to bypass this, would be sending all the packets in one, but cmon, there must be a way to send packets whenever i want and grant they arrive properly :x

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Trouble with QTcpSocket

    You can send whatever, whenever you want with QTcpSocket.

    From your post I understand you use the readyRead() signal. That's already a good idea. But how do you read the data in the slot connected to the readyRead signal?

    Do you use readAll and assume that it read the complete response sent by the server?

  3. #3
    Join Date
    Jun 2010
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with QTcpSocket

    My logic is:

    When i send a packet in the server, i do it by sending a QString through the QDataStream
    When i receive that packet in the client, readyRead is emitted and my slot gets a QString from the QDataStream

    This way i think i always fetch the last QString sent. Still what you said makes sense, because readyRead() could be triggered once, and a few QStrings were received, and by reading only one, the buffer is cleaned before i fetch the rest, am i right?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Trouble with QTcpSocket

    Quote Originally Posted by GabrielGray View Post
    This way i think i always fetch the last QString sent. Still what you said makes sense, because readyRead() could be triggered once, and a few QStrings were received, and by reading only one, the buffer is cleaned before i fetch the rest, am i right?
    You're on the right path :-)

    I remember that I was debugging a program that received data via a QTcpSocket and I didn't get any data in my own buffer.
    What happened was that the readyRead signal can be emitted without any data, in my case, it received something but it then got another readyRead signal with zero data. The way I implemented the slot I always reseted my own buffer. In the end, my own buffer was either empty or it contained something (if I was lucky enough to not get an empty readyRead).

    The protocol I implemented contained a control character to signal the end of a datastream.
    Thus to solve the problem I kept adding to my own buffer every time the readyRead() signal was fired until I got that end of stream character.

    You can do something similar. You know the number of strings you send (and the client too). I would take advantage of that information to see if I got everything.

  5. #5
    Join Date
    Jun 2010
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with QTcpSocket

    Thanks , it was very useful, i solved it, that was the problem exactly.
    readyRead() was emitted, and i went to get only one QString, the others were ignored, in order to solve it i did:

    while(!stream.atEnd() ){
    stream >> packet;
    handlePacket(packet);
    }

    Thanks!

Similar Threads

  1. Trouble using QTimeEdit
    By rforehand in forum Newbie
    Replies: 0
    Last Post: 17th October 2009, 01:03
  2. QGLWidget trouble
    By John82 in forum Qt Programming
    Replies: 9
    Last Post: 31st July 2009, 01:03
  3. SQL trouble
    By xmeister in forum Newbie
    Replies: 2
    Last Post: 25th March 2009, 11:53
  4. trouble with Qt-4 with kubuntu 8.04
    By impeteperry in forum Installation and Deployment
    Replies: 2
    Last Post: 25th July 2008, 16:21
  5. New to QT..need help guys..sorry for the trouble
    By neomax in forum General Discussion
    Replies: 2
    Last Post: 17th November 2006, 16:20

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.