Results 1 to 9 of 9

Thread: waitForBytesWritten

  1. #1
    Join Date
    Jan 2006
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default waitForBytesWritten

    hi all,

    i am writing a client server application and my server uses a thread per client,
    writing to the socket as follows

    Qt Code:
    1. socket.write(block) // block is of type QByteArray
    2. socket.waitForBytesWritten(-1)
    To copy to clipboard, switch view to plain text mode 

    does the command in the second line enforce all data t be actually written to the socket, which was in the block variable?
    the docu says just "it waits until a payload of data has been written".

    greetings

    smalls

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: waitForBytesWritten

    I don't thik it can 'enforce' anything, it only returns once data has been written, and will not reutrn as long as its not so.

  3. #3
    Join Date
    Jan 2006
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: waitForBytesWritten

    so, is there a way to tell whether it's all send. or maybe some function returning the amount of data that is still in the socket's buffer?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: waitForBytesWritten

    so, is there a way to tell whether it's all send.
    Yes, as the docs say:
    ...Returns true if a payload of data was written to the device;
    So, if it returns false, not all data was written.
    You can use a timeout and ask bytesToWrite () to see howmany bytes are still wating to be written.

  5. #5
    Join Date
    Mar 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: waitForBytesWritten

    The data is written to the socket.

    My question is, is also sent to the receiver?

    Can the Nagel algorithm have something to do with this?
    How to turn that of on a Windows XP system?

    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: waitForBytesWritten

    What exactly you want to do?
    And what is the Nagel algoritm ?
    a life without programming is like an empty bottle

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: waitForBytesWritten

    In order to know if this has to do with the Nagel algorithm you could just flush and see if it gets to the receiver.

  8. #8
    Join Date
    Mar 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: waitForBytesWritten

    I want to send a large file and when the total file is sent I just want to send an ack.

    I Want the ack in a new TCP message. I have hard to find the ack when sniffing the network, once i found it last in the last "file" TCP message. Now, when introduced the flush, I cant find the ack anywhere.

    Qt Code:
    1. #define TCP_MAXBUF 0x08000 // max TCP send and receive size buffer 32768 (MAX right now)
    2.  
    3. // Send File
    4. do
    5. {
    6. // read file pattly
    7. qint64 bytesRead = vrFile->read((char *)buffer,TCP_MAXBUF);
    8.  
    9. int sentSize = pFPSocket->write(fileBuffer.data(), bytesRead);
    10. pFPSocket->flush();
    11. if(!pFPSocket->waitForBytesWritten(Timeout))
    12. {
    13. theLog->WriteLog(QString("Wait for bytes written timed out"),LOG_LEVEL_ERR);
    14. }
    15. }while(!vrFile->atEnd()) ;
    16.  
    17. // Send Ack
    18. QByteArray ackBuffer(7, 0);
    19. QDataStream outAck(&ackBuffer, QIODevice::WriteOnly);
    20.  
    21. outAck << (unsigned char)TCP_DISCONNECT;
    22. outAck << (quint32)1;
    23. outAck << (unsigned char)"¤";
    24.  
    25. int i = sizeof(TCPPacket);
    26. if(pFPSocket->write(ackBuffer.data(),6) < 6 )
    27. {
    28. // ERROR not total ack sent
    29. theLog->WriteLog(QString("Could not store %1, complete ack not sent.").arg(Filename),LOG_LEVEL_ERR);
    30. statusOK = false;
    31. }
    32.  
    33. pFPSocket->flush();
    34. if(!pFPSocket->waitForBytesWritten(Timeout))
    35. {
    36. // ERROR not total ack sent
    37. theLog->WriteLog(QString("Could not store %1, ack bytes not written.").arg(Filename),LOG_LEVEL_ERR);
    38. statusOK = false;
    39. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: waitForBytesWritten

    Try this (I added another flush() between the "file" and "ack" packets):.

    Qt Code:
    1. #define TCP_MAXBUF 0x08000 // max TCP send and receive size buffer 32768 (MAX right now)
    2.  
    3. // Send File
    4. do
    5. {
    6. // read file pattly
    7. qint64 bytesRead = vrFile->read((char *)buffer,TCP_MAXBUF);
    8.  
    9. int sentSize = pFPSocket->write(fileBuffer.data(), bytesRead);
    10. pFPSocket->flush();
    11. if(!pFPSocket->waitForBytesWritten(Timeout))
    12. {
    13. theLog->WriteLog(QString("Wait for bytes written timed out"),LOG_LEVEL_ERR);
    14. }
    15. }while(!vrFile->atEnd()) ;
    16. //flush the file packet
    17. pFPSocket->flush();
    18. // Send Ack
    19. QByteArray ackBuffer(7, 0);
    20. QDataStream outAck(&ackBuffer, QIODevice::WriteOnly);
    21.  
    22. outAck << (unsigned char)TCP_DISCONNECT;
    23. outAck << (quint32)1;
    24. outAck << (unsigned char)"¤";
    25.  
    26. int i = sizeof(TCPPacket);
    27. if(pFPSocket->write(ackBuffer.data(),6) < 6 )
    28. {
    29. // ERROR not total ack sent
    30. theLog->WriteLog(QString("Could not store %1, complete ack not sent.").arg(Filename),LOG_LEVEL_ERR);
    31. statusOK = false;
    32. }
    33.  
    34. pFPSocket->flush();
    35. if(!pFPSocket->waitForBytesWritten(Timeout))
    36. {
    37. // ERROR not total ack sent
    38. theLog->WriteLog(QString("Could not store %1, ack bytes not written.").arg(Filename),LOG_LEVEL_ERR);
    39. statusOK = false;
    40. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QThread, QFile and QTcpSocket freezes GUI
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 23: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.