Results 1 to 9 of 9

Thread: Problems with maximum data sent over a QTcpSocket

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problems with maximum data sent over a QTcpSocket

    Hi, I am trying to create a basic videoconference program for an university assignment.
    I am using Qt 4.5.2 on Linux.

    When I send full images (something like 230k) over the socket it waits in the socket.disconnect() on the sender side and on the receiver side it only goes to 170k or so of bytesAvailable().

    I am sending images through QTcpSocket like this:

    Sender's side:

    Qt Code:
    1. int NetworkSender::sendData (char *image, int dataSize, unsigned short port)
    2. {
    3. QTcpSocket socket;
    4. int return_value;
    5.  
    6. socket.connectToHost(remoteHost, port);
    7.  
    8. QByteArray block;
    9. QDataStream out(&block, QIODevice::WriteOnly);
    10. out.setVersion(QDataStream::Qt_4_0);
    11. out << (quint32)0;
    12. out.writeBytes((const char*)image, dataSize);
    13. out.device()->seek(0);
    14. out << (quint32)(block.size() - sizeof(quint32));
    15.  
    16. return_value = socket.write(block);
    17. //socket.flush();
    18. //socket.waitForBytesWritten(20);
    19.  
    20. socket.disconnectFromHost();
    21. socket.waitForDisconnected();
    22.  
    23. if (return_value != -1) {
    24.  
    25. return 0;
    26. }else {
    27. return -1;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 


    On the receiver side I have a thread, like this:


    Qt Code:
    1. void NetworkReceiverThread::run()
    2. {
    3. QTcpSocket socket;
    4. // socket.setReadBufferSize(500000);
    5.  
    6. int timeout = 2 * 1000;
    7.  
    8. if (!socket.setSocketDescriptor(socketDescriptor)) {
    9. emit error(socket.error(), socket.errorString());
    10. return;
    11. }
    12.  
    13. while (socket.bytesAvailable() < (int)sizeof(quint32)) {
    14. if (!socket.waitForReadyRead(timeout)) {
    15. emit error(socket.error(), socket.errorString());
    16. return;
    17. }
    18. }
    19.  
    20. quint32 blockSize;
    21. QDataStream in(&socket);
    22. in.setVersion(QDataStream::Qt_4_0);
    23. in >> blockSize;
    24. while (socket.bytesAvailable() < blockSize) {
    25. if (!socket.waitForReadyRead(timeout)) {
    26. qDebug("4");
    27. emit error(socket.error(), socket.errorString());
    28. return;
    29. }
    30. }
    31. qDebug("5");
    32.  
    33. unsigned int data_len;
    34. in.readBytes(data, data_len);
    35.  
    36. qDebug("received data");
    37. qDebug(QString::number(data_len).toStdString().data());
    38.  
    39. emit(done());
    40. }
    To copy to clipboard, switch view to plain text mode 


    As I said if I try to put a debug on the reader inside " while (socket.bytesAvailable() < blockSize) {" printing bytes available I got something like 80k , 120k 160k and then it stops.
    I have tried to set manually the buffer of the receiver as can be seen as a commented line, same result.
    I have tried also calling flush() on the sender just after the write(), I got this:

    QNativeSocketEngine::write() was not called in QAbstractSocket::ConnectedState
    QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState
    I have tried waitForBytesWritten() but same result

    I am testing on loopback (127.0.0.1)
    Last edited by jordip; 3rd February 2010 at 12:04.

Similar Threads

  1. QTCPSocket not getting all data
    By jhowland in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2010, 15:20
  2. QTcpSocket + receiving data
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 08:10
  3. Problem with reading in data from a QTCPSocket
    By Denarius in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2009, 08:54
  4. Problem in printing the data return /read from QTcpsocket
    By skumar434 in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 19:36
  5. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 08:25

Tags for this Thread

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.