Page 1 of 3 123 LastLast
Results 1 to 20 of 43

Thread: cannot read correct len from socket

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

    I am sending a QByteArray from server to client. the client needs to extract the first four bytes for length of packet and then read the rest of the packet. The unexpected value I get from length of packet though, is a very large value;
    what I do is:
    Qt Code:
    1. int len;
    2. char *data;
    3.  
    4. client.read((char*)len, 4); // read the first integer for the length
    5. client.read(data, len);
    To copy to clipboard, switch view to plain text mode 

    can anyone guess what the problem is?

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot read correct len from socket

    Try client.read((char*)&len, 4);


    Added after 4 minutes:


    Qt Code:
    1. global int blockSize = 0;
    2. ...
    3.  
    4. while (clientConnection->state() != 0)
    5. {
    6. clientConnection->waitForReadyRead(1);
    7.  
    8. if (clientConnection->bytesAvailable() > 0)
    9. {
    10. if (blockSize == 0)
    11. {
    12. if (clientConnection->bytesAvailable() < sizeof( int ))
    13. continue;
    14.  
    15. clientConnection->read( (char *)&blockSize, sizeof( int ) );
    16. }
    17.  
    18. clientConnection->waitForReadyRead(1);
    19.  
    20. if (clientConnection->bytesAvailable() < blockSize)
    21. continue;
    22.  
    23. char *msg = new char[ blockSize ];
    24. memset( msg, 0, blockSize );
    25.  
    26. clientConnection->read( &msg[0], blockSize );
    27. ...
    28. blockSize = 0;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Jonny174; 24th July 2013 at 11:52.

  3. #3
    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

    it doesn't help. it gives terrible large numbers.

    is the byte array is "47dannie", reading 4 bytes would I think, read 47da, next time it would read nnie, converting them into integers.
    Qt Code:
    1. int len;
    2. char *data;
    3. client.read((char*)&len, 4);
    4. client.read(&data[0], len);
    To copy to clipboard, switch view to plain text mode 

    this is not correct.

  4. #4
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot read correct len from socket

    Qt Code:
    1. void MyServer::slotReadClient()
    2. {
    3. QTcpSocket* pClientSocket = (QTcpSocket *)sender();
    4. QDataStream in( pClientSocket );
    5.  
    6. in.setVersion( QDataStream::Qt_4_7 );
    7.  
    8. for (;;)
    9. {
    10. if (!m_nNextBlockSize)
    11. {
    12. if (pClientSocket->bytesAvailable() < sizeof( quint16 ))
    13. break;
    14.  
    15. in >> m_nNextBlockSize;
    16. }
    17.  
    18. if (pClientSocket->bytesAvailable() < m_nNextBlockSize)
    19. break;
    20.  
    21. QTime time;
    22. QString str;
    23.  
    24. in >> time >> str;
    25.  
    26. QString strMessage = time.toString() + " " + "Client has sended - " + str;
    27. m_ptxt->append( strMessage );
    28.  
    29. m_nNextBlockSize = 0;
    30.  
    31. sendToClient( pClientSocket, "Server Response: Received \"" + str + "\"" );
    32. }
    33. }
    34.  
    35. // ----------------------------------------------------------------------
    36. void MyServer::sendToClient( QTcpSocket *pSocket, const QString &str )
    37. {
    38. QByteArray arrBlock;
    39. QDataStream out( &arrBlock, QIODevice::WriteOnly );
    40.  
    41. out.setVersion( QDataStream::Qt_4_7 );
    42. out << quint16(0) << QTime::currentTime() << str;
    43.  
    44. out.device()->seek(0);
    45. out << quint16( arrBlock.size() - sizeof( quint16 ) );
    46.  
    47. pSocket->write( arrBlock );
    48. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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

    I am not asking for the way we deal with code in qtcpsocket.
    i am asking why the above code which is quite logical in C, does not work out! and how to correct it.

  6. #6
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: cannot read correct len from socket

    What is the value of the big value you get?

  7. #7
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot read correct len from socket

    X3. Maybe not all the data had to come. Shaw all read code.

  8. #8
    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

    Quote Originally Posted by nix View Post
    What is the value of the big value you get?
    output:
    newConnection
    1632843572
    879649133
    1835094839
    926183009
    1634558291
    1396126830
    1851878753
    1632843572
    879649133
    1835094839
    926183009

    the strange part is when you readLine() and use qbytearray, it contains both the digit and the string "47Saman". Though, in this case, nope

  9. #9
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: cannot read correct len from socket

    Ok, according to the example you gave, your length seems to have a 2 octet size, if the size is fixed try this

    Qt Code:
    1. QByteArray lenBA(2,'0');
    2.  
    3. client.read(lenBA.data(), 2); // read the first integer for the length
    4. short int length = lenBA.toShort(); // check the conversion is ok here
    5. data.reserve(length);
    6.  
    7. client.read(data.data(), length );
    To copy to clipboard, switch view to plain text mode 

    1632843572 = 0x61533734 and give in ascii : "aS74" look realy close to "47Sa" if you considering byte order issue from brutal conversion from char * to int.
    Last edited by nix; 24th July 2013 at 13:13.

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot read correct len from socket

    Quote Originally Posted by saman_artorious View Post
    the strange part is when you readLine() and use qbytearray, it contains both the digit and the string "47Saman". Though, in this case, nope
    As You see the length of data is transported as characters not as binary int. So You can't read read it as binary data. You must read it as string and convert to int. Another problem : what if data length is bigger then 99 - it is 3 digits.

  11. #11
    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

    Quote Originally Posted by Lesiok View Post
    what if data length is bigger then 99 - it is 3 digits.
    Yes, the length of data is random and may vary. I cannot find a way to deal with this.

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot read correct len from socket

    Quote Originally Posted by saman_artorious View Post
    Yes, the length of data is random and may vary. I cannot find a way to deal with this.
    This is very simple. Always send fixed number of digits with leading zeros. In example if You want to send 4 digits You should send "0047Saman".
    In fact, the problem is the definition of the protocol.

  13. #13
    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
    I am sending a QByteArray from server to client. the client needs to extract the first four bytes for length of packet and then read the rest of the packet. The unexpected value I get from length of packet though, is a very large value;
    what I do is:
    Qt Code:
    1. int len;
    2. char *data;
    3.  
    4. client.read((char*)len, 4); // read the first integer for the length
    5. client.read(data, len);
    To copy to clipboard, switch view to plain text mode 

    can anyone guess what the problem is?
    How were the first four bytes written to the socket by the server?
    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.


  14. #14
    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

    Quote Originally Posted by wysota View Post
    How were the first four bytes written to the socket by the server?
    By the Client, it was written as such:

    Qt Code:
    1. void tcpClient::sendCommand(QString com)
    2. {
    3. QByteArray data;
    4.  
    5. data.append(com); // "47Saman"
    6.  
    7. if(clientSocket->write (data) == -1)
    8. {
    9. qDebug("sendCommand(QString) Failed");
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    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

    If you are writing "47Saman" to the socket, what do you expect to receive as first four bytes on the other end of the communication?
    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.


  16. #16
    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

    Quote Originally Posted by wysota View Post
    If you are writing "47Saman" to the socket, what do you expect to receive as first four bytes on the other end of the communication?
    Wysota, client is sending me a jpeg file. in the server program, I used readAll() to read it all and pixmap it to a label. But, the image does not appear in the label (though the widget background flashes). To solve this, I was told that they would send the size of the jpeg file along with its content. Hence, I need to read the length first, and then realize how many bytes exactly to read.

    Qt Code:
    1. void MainWindow::readPixmap(QByteArray data)
    2. {
    3. QPixmap pm;
    4.  
    5. pm.loadFromData(data);
    6.  
    7. ui->label->setPixmap(pm);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 24th July 2013 at 18:57.

  17. #17
    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

    Please answer my question.
    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.


  18. #18
    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

    Quote Originally Posted by wysota View Post
    Please answer my question.
    A string of chars.

  19. #19
    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

    Then why are you trying to read them as an integer?
    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.


  20. #20
    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

    Quote Originally Posted by wysota View Post
    Then why are you trying to read them as an integer?
    because I have no idea what the length of the integer might be as a character, 2 digits or 5 digits!
    that's why I try to read 4 bytes = sizeof(char)

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.