Results 1 to 12 of 12

Thread: QTcpSocket does not send data immediately

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTcpSocket does not send data immediately

    Hello everyone,

    could someone tell me the reason for the code below does not send the data? I think that the data is being buffered, rather than sent

    Qt Code:
    1.  
    2. int type_msg = 6;
    3. data.append( (char*)&type_msg, sizeof(int) );
    4.  
    5. string video_server_address;
    6. in >> video_server_address;
    7.  
    8. int num_char_video_server_address = video_server_address.size();
    9. data.append( (char*)&num_char_video_server_address, sizeof(int) );
    10.  
    11. data.append( QString(video_server_address.c_str()) );
    12.  
    13. int num_bytes_write = clientSocket->write( data.data(), data.size() );
    14. clientSocket->flush();
    15.  
    16. while( clientSocket->bytesToWrite() > 0 ){
    17. num_bytes_write += clientSocket->write( data.data()+num_bytes_write, data.size()-num_bytes_write );
    18. clientSocket->flush();
    19. clientSocket->waitForBytesWritten();
    20. }
    To copy to clipboard, switch view to plain text mode 

    before this code a lot of data has been sent successfully the same way, but these last 17 bytes were not received by the client

    clientSocket is a variable whose type is QTcpSocket.

    Thanks

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTcpSocket does not send data immediately

    I guess you should be checking at the receiving end, the data may not be sent in single packet, make sure that that the receiving end socket is read until no data is left

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

    igorosberg (15th July 2011)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTcpSocket does not send data immediately

    There's no such beast as an unbuffered TCP socket, a point made by the QTcpSocket docs:
    Note: TCP sockets cannot be opened in QIODevice::Unbuffered mode.
    You queue stuff to send and the TCP/IP stack looks after sending it, pipeline optimisation, acknowledgement, sequencing, and integrity. It gets there when it gets there... and you can be sure it will.

  5. The following user says thank you to ChrisW67 for this useful post:

    igorosberg (15th July 2011)

  6. #4
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket does not send data immediately

    Quote Originally Posted by Santosh Reddy View Post
    I guess you should be checking at the receiving end, the data may not be sent in single packet, make sure that that the receiving end socket is read until no data is left
    the problem is that the shown code do not emit the readyRead() signal on client side, because of this i can conclude that the data was not sent

    Thanks by replies

  7. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket does not send data immediately

    Show more of your code, maybe something is happening before the data is actually sent (such as closing the socket or invalidating the socket)

    You can only guarantee with waitForBytesWritten that Qt transfers the data to OS buffers. There is no guarantee that the OS has made any attempts to transfer the data at that point.

  8. #6
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket does not send data immediately

    Quote Originally Posted by squidge View Post
    Show more of your code, maybe something is happening before the data is actually sent (such as closing the socket or invalidating the socket)

    You can only guarantee with waitForBytesWritten that Qt transfers the data to OS buffers. There is no guarantee that the OS has made any attempts to transfer the data at that point.
    I thought the reason for the server does not send the data is due to the size of the package, that is, the package is very small, then the OS prefers buffered and wait for more data to send everything.

    to confirm this fact, after the code shown, I sent another package with size 473 356 bytes and then the first packet arrived at the client, followed by the larger package.

    So my belief that this data was buffered instead sent was confirmed.

    Could someone tell me how to avoid this type of buffering for small packages? I thought the function flush() had just this goal.

  9. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket does not send data immediately

    Your small package will be sent, eventually. It depends on the QOS on the host. Typically it will be less than a second however.

  10. #8
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket does not send data immediately

    My server handle many connections using multiple threads. i create the threads using the code

    Qt Code:
    1. HandlerConnection *thread = new HandlerConnection( socketDescriptor, &user_list, this );
    2. connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
    3. thread->start();
    To copy to clipboard, switch view to plain text mode 

    on the constructor method of class HandleConnection there is the follow code

    Qt Code:
    1. clientSocket = new QTcpSocket();
    2.  
    3. if(!clientSocket->setSocketDescriptor(socketDescriptor)){
    4. emit error( clientSocket->error() );
    5. return;
    6. }
    7.  
    8. connect(clientSocket, SIGNAL(readyRead()), this, SLOT(receiveData()), Qt::DirectConnection );
    9.  
    10. forever{
    11. QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
    12. }
    13. exec();
    To copy to clipboard, switch view to plain text mode 

    after that, the server receive and send data. however, that code in the first post does not work.

    Thanks. Waiting for more replies.

  11. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket does not send data immediately

    So, your are creating a QTcpSocket in one thread and then expecting to read from it in another thread. That is not going to be reliable.

    In fact, if you have "exec" in your constructor, I'm surprised it even returns

    You don't even need threads for multiple connections. All connections can be handled in the main thread.

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. Replies: 4
    Last Post: 22nd March 2010, 18:32
  3. How to send Pixmap through QTcpSocket?
    By live_07 in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2008, 16:35
  4. Send Base64 encoded data
    By rmagro in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 16:58
  5. My client can't send data
    By hiuao in forum Qt Programming
    Replies: 10
    Last Post: 23rd February 2007, 09:32

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.