Results 1 to 4 of 4

Thread: socket read/write bytes

  1. #1
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default socket read/write bytes

    Hi at all!,

    I want to write (an after read) an bytes array to the TCP socket.
    I try with this code:

    Qt Code:
    1. QByteArray block;
    2. QDataStream out(&block, QIODevice::WriteOnly);
    3. out.setVersion(QDataStream::Qt_4_1);
    4.  
    5. QString variable="CIAO";
    6.  
    7. out << quint16(0) << variable;
    8.  
    9. out.device()->seek(0);
    10. out << quint16(block.size() - sizeof(quint16));
    11. tcpSocket.write(block);
    To copy to clipboard, switch view to plain text mode 

    this code work, but the problem is that I not read only "CIAO" on my receiver program.
    I'm waiting to receive 6 bytes... "04CIAO"
    I must change the variable type?

    nowire
    Last edited by nowire75; 3rd July 2007 at 21:49.

  2. #2
    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: socket read/write bytes

    Don't use QDataStream, it's a serialization mechanism, not a general purpose binary stream.

  3. #3
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: socket read/write bytes

    must I use QTextStream? Can you post an example?

    I want to make a block-oriented application.

    nowire

  4. #4
    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: socket read/write bytes

    Don't use a stream at all.

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.