Page 3 of 3 FirstFirst 123
Results 41 to 43 of 43

Thread: cannot read correct len from socket

  1. #41
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot read correct len from socket

    If you guys take a look at the code above, you will realize, I am doing the same thing. I think this is bug irrelevant to my code. Besides, with your explanation, I do not think my code is wrong at all.

    client:
    Qt Code:
    1. int size = blob.length();
    2. clientSocket->write((char*)&size, 4);
    3. clientSocket->write(blob.data(), size);
    To copy to clipboard, switch view to plain text mode 

    server:

    Qt Code:
    1. int size;
    2. int y = clientConnection->bytesAvailable();
    3. qDebug() << y;
    4. int x = clientConnection->read((char*)&size, 4);
    5.  
    6. int size2 = size;
    7. int offset = 0;
    8. char* buffer = new char[size];
    9. while(size2 > 0)
    10. {
    11. int r = clientConnection->read(buffer+offset, size2);
    12. if(r == 0)
    13. break;
    14. size2 -= r;
    15. offset += r;
    16. }
    17. qDebug() << size;
    To copy to clipboard, switch view to plain text mode 

  2. #42
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cannot read correct len from socket

    Quote Originally Posted by saman_artorious View Post
    I do not think my code is wrong at all.
    And yet it is wrong.

    Qt Code:
    1. int size = blob.length();
    2. clientSocket->write((char*)&size, 4);
    3.  
    4. int size;
    5. int x = clientConnection->read((char*)&size, 4);
    To copy to clipboard, switch view to plain text mode 
    This is not portable. The size of an int is not necessarily 4 bytes. That is why I mentionned quint32 in my previous post. In addition, this fails if the byte orders are not the same on the server and client machines. That is why I mentioned endianness in my previous post.

    Qt Code:
    1. int size;
    2. int y = clientConnection->bytesAvailable();
    3. qDebug() << y;
    4. int x = clientConnection->read((char*)&size, 4);
    5.  
    6. int size2 = size;
    7. int offset = 0;
    8. char* buffer = new char[size];
    9. while(size2 > 0)
    10. {
    11. int r = clientConnection->read(buffer+offset, size2);
    12. if(r == 0)
    13. break;
    14. size2 -= r;
    15. offset += r;
    16. }
    17. qDebug() << size;
    To copy to clipboard, switch view to plain text mode 
    QTcpSocket is non blocking by default. When read() returns 0, you must return to the event loop and wait for the readyRead() signal before you can read more data. That is why I mentioned asynchronous-style communication, discussed the storage of the state of your receiver, and provided a link to some explanations on the stream-oriented nature of TCP in my previous post.

    Now, are you certain that you have read the material I linked to, and that you have experience in socket programming?

    Also I recommend reading the documentation of the Qt classes you use (QIODevice, QAbstractSocket, QTcpSocket, ...) and the various networking examples.

  3. #43
    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: cannot read correct len from socket

    Quote Originally Posted by saman_artorious View Post
    If you guys take a look at the code above, you will realize, I am doing the same thing.
    I don't see any code of yours that waits for all the data to arrive.

    I think this is bug irrelevant to my code.
    Please, don't continue. Just get a book and read it.

    Besides, with your explanation, I do not think my code is wrong at all.
    Well, it doesn't work so surely it is not correct.

    Qt Code:
    1. clientSocket->write((char*)&size, 4);
    To copy to clipboard, switch view to plain text mode 
    Wrong! What about byte order?

    Qt Code:
    1. int x = clientConnection->read((char*)&size, 4);
    To copy to clipboard, switch view to plain text mode 
    Wrong! What if less than four bytes are available in the socket? What about byte order?

    Qt Code:
    1. char* buffer = new char[size];
    To copy to clipboard, switch view to plain text mode 
    Crashes if size is negative or larger than available memory.

    Qt Code:
    1. while(size2 > 0)
    2. {
    3. int r = clientConnection->read(buffer+offset, size2);
    4. if(r == 0)
    5. break;
    6. size2 -= r;
    7. offset += r;
    8. }
    9. qDebug() << size;
    To copy to clipboard, switch view to plain text mode 
    Wrong! What if less than "size" bytes are available in the socket?
    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. Replies: 6
    Last Post: 7th October 2012, 16:42
  2. Can't read from socket
    By marekdor in forum Newbie
    Replies: 6
    Last Post: 9th August 2012, 11:06
  3. Read all_ tcp socket
    By Mayssa in forum General Programming
    Replies: 1
    Last Post: 21st February 2012, 17:12
  4. Replies: 2
    Last Post: 22nd May 2011, 21:31
  5. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 4th July 2007, 23:12

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.