Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 43

Thread: cannot read correct len from socket

  1. #21
    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.


  2. #22
    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.

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


  4. #24
    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?

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

  6. #26
    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*

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


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

  9. #29
    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.

  10. #30
    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.


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

  12. #32
    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.

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

  14. #34
    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 ChrisW67 View Post
    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?
    It is not like I have new to socket programming. I have done it with C many times. The problem when I even try to send and receive data with simple read n write operations it does not give the expected value. I opened a binary file and read the contents, it writes to buffer correctly. the buffer size is also equal to image size. However, writing it to the other side and reading, is not correct.

    Qt Code:
    1. QString fileName = "/home/saman/Desktop/1.png";
    2. QFile file(fileName);
    3. if (!file.open(QIODevice::ReadOnly)) return;
    4. QByteArray blob = file.readAll();
    5.  
    6. int len = blob.length();
    7. clientSocket->write(blob, len);
    To copy to clipboard, switch view to plain text mode 

    server:
    Qt Code:
    1. int len;
    2. clientConnection->read((char*)&len, 4);
    3. qDebug() << len;
    To copy to clipboard, switch view to plain text mode 

    does not read correctly!

  15. #35
    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
    It is not like I have new to socket programming. I have done it with C many times.
    Then do it with regular C calls, nobody forces you to use Qt classes for that. With plain C calls you'll be having the exact same "problems" you have now -- you will have to format the data somehow and read it somehow according to all rules that govern asynchronous communication between two autonomous systems.
    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. #36
    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

    how to format it? what are those rules?


    Added after 1 11 minutes:


    Quote Originally Posted by wysota View Post
    Then do it with regular C calls, nobody forces you to use Qt classes for that. With plain C calls you'll be having the exact same "problems" you have now -- you will have to format the data somehow and read it somehow according to all rules that govern asynchronous communication between two autonomous systems.
    I think you are wrong. As I tested the program in another system, with standard c++ rcv function, it could receive the image.
    So, that means the send or write commands is correct and client program works fine:

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

    However, this shows the server program written (the first one) is not correct, which is quite strange. As I debugged it, it received the packet up to 50 KB and then returns 0! On the other, the other one which used std sockets, can read all of it.
    Qt Code:
    1. int size;
    2. clientConnection->read((char*)&size, 4);
    3. qDebug() << size;
    4.  
    5. char* image;
    6.  
    7. qDebug() << size;
    8.  
    9. image = new char[size];
    10.  
    11. int size2 = size;
    12. int offset = 0;
    13.  
    14. while(size2 > 0)
    15. {
    16. int r = clientConnection->read(image+offset, size2);
    17. if(r == 0)
    18. break;
    19. size2 -= r;
    20. offset += r;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 28th July 2013 at 11:09.

  17. #37
    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
    how to format it? what are those rules?
    You should know, it's not the first time you do socket communication.

    As I tested the program in another system, with standard c++ rcv function, it could receive the image.
    Good for you. Seems the problem is solved, isn't it?
    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. #38
    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 also tried the receiving buffer code in C. it again only read 49KB and then returns 0, ignoring the rest of file.
    I never had such problem in programs, simple send and recv would do it all.
    Could I again ask you what I need to do to solve this problem?

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

    I am at a loss. Are you assuming that if you write n bytes using one call to write(), then the other side will read those n bytes in one call to read()? Since you have already programmed with sockets, you must know that TCP is a stream protocol and does not work this way. I recommend checking out http://blog.stephencleary.com/2009/0...ckets-faq.html for more information on this topic.

    I suppose I would do something along these lines to solve your problem:

    On the sender's side:
    1. Send the length of the byte array as e.g. a 32-bit unsigned integer in big-endian byte order (see quint32 and qToBigEndian()).
    2. Send the bytes themselves.

    On the receiver's side:
    1. Store received bytes in a buffer until I have 4 bytes.
    2. Interpret those 4 bytes as a 32-bit unsigned integer in big-endian byte order (let n be the value thus read).
    3. Store received bytes in a byte array until I have n of them.

    This is a quite standard approach to TCP asynchronous communication. You will probably need a few variables to track the current state on the receiver side (e.g. whether you are currently waiting for bytes that are part of the integer or the byte array, how many bytes you are still waiting for, etc.).

  20. #40
    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 also tried the receiving buffer code in C. it again only read 49KB and then returns 0, ignoring the rest of file.
    How unsurprising. I guess if you streamed 16GB of data into the socket, you'd expect to receive that 16GB of data after 2 seconds on the receiving end, right?

    I never had such problem in programs, simple send and recv would do it all.
    Which means you have no understanding of network programming.

    Could I again ask you what I need to do to solve this problem?
    You need a good book on networking theory.
    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.


  21. The following user says thank you to wysota for this useful post:

    saman_artorious (30th July 2013)

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.