Results 1 to 5 of 5

Thread: QTcpSocket writing reading problem

  1. #1
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket writing reading problem

    Hi all

    I have two network applications, something like client and server.
    On the server side I write data to socket which is in QByteArray.

    Qt Code:
    1. qint64 size = socket->write(array);
    2. qDebug() << "written" << size;
    To copy to clipboard, switch view to plain text mode 

    On the client side in slot which is connected to readyRead signal I first read quint32 which is size of data and then I read data to QByteArray.

    Qt Code:
    1. QDataStream socketStream(socket);
    2.  
    3. quint32 size;
    4. socketStream >> size;
    5. qDebug() << "size" << size;
    6. qDebug() << "socketAvailable" << quint32(socket->bytesAvailable());
    7.  
    8. if (quint32(socket->bytesAvailable()) < size)
    9. return;
    10.  
    11. QByteArray array = socket->read(size);
    To copy to clipboard, switch view to plain text mode 

    So qDebug() on server side give me

    Qt Code:
    1. warning: written 17980
    2. warning: written 224
    3. warning: written 135176
    To copy to clipboard, switch view to plain text mode 

    but on teh client side

    Qt Code:
    1. warning: size 17976
    2. warning: socketAvailable 17976
    3.  
    4. warning: size 220
    5. warning: socketAvailable 220
    6.  
    7. warning: size 135172
    8. warning: socketAvailable 98300
    To copy to clipboard, switch view to plain text mode 

    First four warnings are correct becouse I read size which is 4 bytes so it is smaller but the last two do not match. Every time I write to socket more then 98304 bytes it doesnt let me to read them all and I do not know why. In the second calling of client slot when is readyRead emit again there are all data available but I cant read it exactly becouse I have already read their size before. How can I solve this problem? Thanks.

  2. #2
    Join Date
    Jun 2008
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket writing reading problem


  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket writing reading problem

    i) you have to make sure that enough data is available before reading your block size, too
    ii) you have to store the size you read (in some member variable)
    iii) you have to check whether you already have the block size, if not: see i); else wait for "block size bytes available *without* reading a blocksize again"

    HTH

  4. #4
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket writing reading problem

    Thanks for responding.

    to caduel:
    How can I make sure that enough data is available before reading block size?

    The problem is that I have connected readyRead signal from socket to slot which take that socket and create new thread to read data from it. So I cant make member variable which will be size becouse when next readyRead is emited with rest of data next thread is created. Isnt there any way how can I wait in thread for rest of data or how can I set how much data must be written for emiting readyRead? I cant use socket->waitForReadyRead(xyz) in thread becouse I do not know how long to wait becouse sometime for one piece of data which I write on server side 2 or 3 or more times readyRead is emited.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket writing reading problem

    try a method/class like (untested)
    Qt Code:
    1. class InputProcessor : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. QIODevice *input_;
    6. qint64 blocksize_;
    7. QDataStream datastream_;
    8.  
    9. public:
    10. InputProcessor(QIODevice *input, QObject *parent=0)
    11. : QObject(parent)
    12. , blocksize_(0)
    13. , input_(input)
    14. , datastream(input)
    15. {
    16. connect(input_, SIGNAL(readyRead()), SLOT(slotProcessInput()));
    17. }
    18.  
    19. public slots:
    20. // connect tcp socket readyRead() here
    21. void slotProcessInput()
    22. {
    23. // if we do not have a block size, wait for enough bytes
    24. if (blocksize_==0)
    25. { // wait for blocksize
    26. if (input_->bytesAvailable() < sizeof(qint64))
    27. return;
    28. datastream_ >> blocksize_;
    29. }
    30.  
    31. // now we know the size of the transmitted block: wait for the data
    32. if (input_->bytesAvailable() < blocksize_)
    33. return; // we will be called again
    34.  
    35. // all data here, read block:
    36. // datastream_ >> ...;
    37.  
    38. blocksize_ = 0; // wait for next block (size)
    39. }
    40.  
    41. };
    To copy to clipboard, switch view to plain text mode 

    (I have not tried to compile that. Please post if it is buggy.)

    HTH

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 21:53
  2. problem with reading input data in qt
    By Ahmad in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2007, 11:58
  3. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 17:00
  4. Replies: 6
    Last Post: 8th January 2007, 11:24
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 22:50

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.