Results 1 to 20 of 43

Thread: cannot read correct len from socket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  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

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  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

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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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

    1. sizeof(char) != 4
    2. int does not contain decimal digits, it is a binary value ranging from ~ -2^31 to 2^31 (which can hold 10 decimal digits) for a 32 bit integer memory layout
    3. reading a value that has not been written cannot end well, can it?
    4. you're doing a lot of other errors in your code which completely prove you have no idea what you are doing and just blindly trying to get things working without any understanding what is going on.

    Sockets are not magical entities, they do not convert your data, add some artificial data or remove some data -- if you write a sequence of bytes to it, the exact same sequence of bytes will be read by the other end of the communication channel. Don't try to do network programming if you don't understand how it works. Spend two days reading about network programming instead of listening to hints you don't even understand.
    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.


  9. #9
    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?
    I made a mistake. it is not as chars. it is binary data. So, I need to read the first 4 bytes to get the size of buffer.
    The problem is that I cannot simply write an integer to the socket, and read the int 4 bytes in the client.
    Last edited by saman_artorious; 25th July 2013 at 09:47.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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
    The problem is that I cannot simply write an integer to the socket, and read the int 4 bytes in the client.
    Hmmm... why not?
    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.


  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 wysota View Post
    Then why are you trying to read them as an integer?
    I want to read it as binary. appending data length at the beginning with this:
    Qt Code:
    1. void tcpClient::sendToServer()
    2. {
    3. QByteArray data;
    4.  
    5. data.append(5);
    6. data.append("Saman");
    7.  
    8. if(clientSocket->write (data) == -1)
    9. {
    10. qDebug("write() failed");
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    in the server, let's read the first 4 bytes, as the size of integer and then read the text.
    Qt Code:
    1.  
    2. int length;
    3.  
    4. clientConnection->read((char*)&length, 4);
    5.  
    6. qDebug() << length;
    7.  
    8. text.reserve(length);
    9.  
    10. clientConnection->read(text.data(), length);
    11.  
    12. clientConnection->flush();
    To copy to clipboard, switch view to plain text mode 

    output:
    length read is 1835094789 !

    what am I missing?

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

    Default Re: cannot read correct len from socket

    QByteArray::append(5) appends one byte with a value of 5 not integer. Do it in such a way :
    Qt Code:
    1. clientSocket->write (5);//You are sending int(5) and on other side You can read int
    2. clientSocket->write("Saman");
    To copy to clipboard, switch view to plain text mode 

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

    wrong! that would prompt invalid conversion from int to const char*

  14. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cannot read correct len from socket

    Quote Originally Posted by Lesiok View Post
    QByteArray::append(5) appends one byte with a value of 5 not integer. Do it in such a way :
    Qt Code:
    1. clientSocket->write (5);//You are sending int(5) and on other side You can read int
    To copy to clipboard, switch view to plain text mode 
    There's no write(int) function anywhere I can see in the socket/qiodevice interfaces; this won't compile.

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

    Default Re: cannot read correct len from socket

    OK, clientSocket->write(5) is only idea. This code should compile
    Qt Code:
    1. QDataStream m_stream(&data,QIODevice::WriteOnly);
    2. m_stream << 5 << "Saman";
    3. clientSocket->write(data);
    To copy to clipboard, switch view to plain text mode 
    saman_artorious read more about QDataStream.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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 Lesiok View Post
    saman_artorious read more about QDataStream.
    No, please, don't read about QDataStream. Ignore its existance completely for this project.
    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.


  17. #17
    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 did not know about QDataStream.

    Now, it reads the integer fine. However, the amount of data it reads does not depend on how much I allocate according to length.
    it always reads the whole thing. How can I control it?

    Qt Code:
    1. void Server::readState()
    2. {
    3. int len = 0;
    4.  
    5. QDataStream in(clientConnection);
    6.  
    7. in >> len;
    8.  
    9. qDebug() << len;
    10.  
    11. QByteArray data;
    12.  
    13. data.reserve(len);
    14.  
    15. in >> data; // read 20 chars and not all
    16.  
    17. qDebug() << data.data();
    18.  
    19. data.clear();
    20.  
    21. clientConnection->flush();
    22. }
    To copy to clipboard, switch view to plain text mode 

    what I sent is of length 50, but only 20 chars would be read, though, it reads all 50 each time:
    Qt Code:
    1. void tcpClient::sendToServer()
    2. {
    3. QByteArray data;
    4. QDataStream m_stream(&data,QIODevice::WriteOnly);
    5. m_stream << 20 << "xxxxxxxxxx yyyyyyyyyy wwwwwwwwww zzzzzzzzzz aaaaaaaDD"; //text size = 50
    6. clientSocket->write(data);
    7.  
    8. clientSocket->flush();
    9. }
    To copy to clipboard, switch view to plain text mode 

    output:
    20
    xxxxxxxxxx yyyyyyyyyy wwwwwwwwww zzzzzzzzzz aaaaaaaDD

  18. #18
    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 did not know about QDataStream.

    Now, it reads the integer fine. However, the amount of data it reads does not depend on how much I allocate according to length.
    it always reads the whole thing. How can I control it?

    Qt Code:
    1. void Server::readState()
    2. {
    3. int len = 0;
    4.  
    5. QDataStream in(clientConnection);
    6.  
    7. in >> len;
    8.  
    9. qDebug() << len;
    10.  
    11. QByteArray data;
    12.  
    13. data.reserve(len);
    14.  
    15. in >> data; // read 20 chars and not all
    16.  
    17. qDebug() << data.data();
    18.  
    19. data.clear();
    20.  
    21. clientConnection->flush();
    22. }
    To copy to clipboard, switch view to plain text mode 

    what I sent is of length 50, but only 20 chars would be read, though, it reads all 50 each time:
    Qt Code:
    1. void tcpClient::sendToServer()
    2. {
    3. QByteArray data;
    4. QDataStream m_stream(&data,QIODevice::WriteOnly);
    5. m_stream << 20 << "xxxxxxxxxx yyyyyyyyyy wwwwwwwwww zzzzzzzzzz aaaaaaaDD"; //text size = 50
    6. clientSocket->write(data);
    7.  
    8. clientSocket->flush();
    9. }
    To copy to clipboard, switch view to plain text mode 

    output:
    20
    xxxxxxxxxx yyyyyyyyyy wwwwwwwwww zzzzzzzzzz aaaaaaaDD
    Please stop trying things at random, this is getting out of hand. You went ahead with QDataStream; you should have read its documentation. Do you have any idea what the insertions (<<) and extractions (>>) do?

    What you really need is read a bit about serialization and deserialization of basic C/C++ types. Forget about the network for now, just pretend you are designing a platform and architecture-independent binary file format in which to save/from which to load the piece of data you are interested in. This has nothing to do with Qt, by the way.

  19. #19
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cannot read correct len from socket

    Quote Originally Posted by saman_artorious View Post

    Now, it reads the integer fine. However, the amount of data it reads does not depend on how much I allocate according to length.
    it always reads the whole thing. How can I control it?
    Yes, it is doing that because that is the correct behaviour for QDataStream. You don't "control it" but you could at least try to understand it and all that has preceded it in this thread.

    If you are serious about understanding then try this experiment: using each approach write the data to a file (substitute a QFile for the QTcpSocket). These files represent exactly what you are sending over the wire. Look at the length of the files. Inspect the files byte by byte (a binary editor or Linux od command). Try changing the numbers and see which bytes change. Look at sizeof(int) how many bytes are you trying to read?

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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 want to read it as binary.
    You are always reading it as binary, even if it is textual data. It is just a matter of interpreting the data.

    what am I missing?
    You are missing a couple of hours of learning about computers, their memory and data layout of different data types.

    1835094789d = 0x6D615305 = 0x0553616D (swapped byte order) = "\5Sam" interpreted as text. Does that ring a bell?
    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
  •  
Qt is a trademark of The Qt Company.