Page 2 of 2 FirstFirst 12
Results 21 to 40 of 43

Thread: cannot read correct len from socket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

    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. #2
    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. #3
    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
    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. #4
    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. #5
    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

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

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