Results 1 to 2 of 2

Thread: QTcpSocket - try to send bunch of requests

  1. #1
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket - try to send bunch of requests

    I am trying to send 2 request one by one at same time. My code is following (this is example code):

    Qt Code:
    1. QTcpSocket client;
    2. ...
    3. client->write(block);
    4. client->write(block);
    To copy to clipboard, switch view to plain text mode 

    Problem is following. Server receives only first request. There is no second request. I sniffed using wireshark and see that there is no second request in tcp packets.

    What must i do to send many requests via QTcpSocket one by one?


    Added after 29 minutes:


    I inserted qDebug() << this->bytesAvailable() << "bytes"; to server in readyRead() and qDebug() << this->bytesToWrite(); after each client->write(block); in client. Also, I added this to client:

    Qt Code:
    1. connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
    2.  
    3. void Connection::bytesWritten(qint64 count)
    4. {
    5. qDebug() << count << "bytes written";
    6. }
    To copy to clipboard, switch view to plain text mode 

    I send ORDER_STATUS_GET_LIST constant in first request and ORDER_GET_LIST in second. I added data output in server. I received first command.

    There is output listing:

    Client:
    Qt Code:
    1. Sending ORDER_STATUS_GET_LIST
    2. 11 bytes to write
    3. Sending ORDER_GET_LIST
    4. 68 bytes to write
    5. 68 bytes written
    To copy to clipboard, switch view to plain text mode 

    Server:
    Qt Code:
    1. 68 bytes
    2. ORDER_STATUS_GET_LIST received
    To copy to clipboard, switch view to plain text mode 


    Added after 17 minutes:


    I found solution myself. I think that somebody needs this too.

    Solution is simple:
    Qt Code:
    1. QTcpSocket client;
    2. ...
    3. client->write(block);
    4. client->flush();
    5. client->write(block);
    6. client->flush();
    To copy to clipboard, switch view to plain text mode 

    This is happen because QT tcp buffer is not empty when second request is executed.
    Last edited by nickla; 15th March 2012 at 19:50.

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

    Default Re: QTcpSocket - try to send bunch of requests

    Quote Originally Posted by nickla View Post
    What must i do to send many requests via QTcpSocket one by one?
    You can't do that with TCP. TCP has no concept of "requests" or "datagrams" or any other kind of records. It is a contignuous flow of octets (bytes). It is up to the application to interpret those octets with the use of some kind of separators (e.g. HTTP uses newlines as separators). Even if you use flush() as in your code, you can't guarantee that the receiving end will get the data in two separate blocks. They can arrive as one block or as 1000 blocks. In your example you get two blocks only because the client and the server are close to each other (in term of network hops) and there is no congestion in the network.
    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.


Similar Threads

  1. how to use qtcpsocket send qimage data
    By tsuibin in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2012, 14:51
  2. QTcpSocket does not send data immediately
    By igorosberg in forum Qt Programming
    Replies: 11
    Last Post: 15th July 2011, 15:51
  3. Replies: 4
    Last Post: 22nd March 2010, 18:32
  4. Yet another bunch of doubts!
    By Nishant in forum Newbie
    Replies: 2
    Last Post: 23rd December 2009, 15:49
  5. How to send Pixmap through QTcpSocket?
    By live_07 in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2008, 16:35

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.