Results 1 to 4 of 4

Thread: QTcpSocket problem

  1. #1
    Join Date
    Nov 2009
    Posts
    39
    Thanks
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QTcpSocket problem

    I'm an experienced Qt developer, but this is first time I've tried to use QTcpSocket. I've got a server (not written in Qt) that sends me 40kB of data upon request. I'm pretty certain the server works ok because I have clients written in other languages that work with the server without any problems. However, I'm trying to write a client using QTcpSocket, and I'm having some trouble. I establish the connection, request the 40kB of data from the server, but I'm only getting 1460 bytes. I've verified that this data is, in fact, the first 1460 bytes of the 40kB packet. I've used different combinations of bytesAvailable(), waitForReadyRead(), etc. to wait for the rest of the data, but it never comes. If I tell the server to send 1455 bytes, the client indeed receives 1455 bytes. If I tell the server to send 1465 bytes, however, I still only get 1460. Just an FYI: I'm using a QByteArray to store data on the client end, not that that adds any useful information.

    Anyone know what could be causing this?

    Thanks,
    MSUdom5

  2. #2
    Join Date
    Feb 2010
    Location
    Hyderabad, India
    Posts
    15
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket problem

    Did you connect to the readyRead() signal which is emitted when the new data is available. Once the first packet of data is read, you should be receiving this signal again where in you can read the next data packet.

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket problem

    TCP protocol provides "stream decomposition" in packets with a maximum size; in your case 1460.
    In general you have to enter in a loop to receive entire packet.

    Can you post your Qt code?
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Nov 2009
    Posts
    39
    Thanks
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket problem

    Thanks for the feedback. Using the readyRead signal fixed my problem.

    I made the mistake of assuming that the read() function would read all incoming packets until it hit a timeout condition. (Which is how some other libraries work.) Basically, I fixed my problem by using:

    Qt Code:
    1. QByteArray packet;
    2. while(socket.waitForReadyRead(timeout))
    3. packet += socket.read(maxbytes);
    To copy to clipboard, switch view to plain text mode 

    Thanks again for the help,
    MSUdom5

Similar Threads

  1. Problem in QTcpSocket
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 13:12
  2. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 13:23
  3. QTcpSocket - Proxy problem
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2007, 00:48
  4. QTcpSocket speed problem
    By benelgiac in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2007, 14:50
  5. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 17:00

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.