Results 1 to 3 of 3

Thread: Sending binary data packet over TCPSocket

  1. #1
    Join Date
    Apr 2014
    Posts
    14
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Sending binary data packet over TCPSocket

    Hello,
    I am a newbie to C++ and Qt and I'm working on a project using Qt now. I want to send a packet over the TCPSocket, the packet structure is as follows:

    SYNC_WORD+RESERVE+PAYLOAD_LENGTH+PAYLOAD+ENDBYTE1+ ENDBYTE2
    and size:
    (1B)+(1B)+(2B)+(VAR B)+(1B)+(1B)

    The payload is the contents of a QlineEdit

    How do I do that? I have tried lots of alternatives, the last of which I defined the packet as an array of characters, but the RESERVE byte is currently 0x00 and it translates into NULL.
    Any help would be appreciated.

  2. #2
    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: Sending binary data packet over TCPSocket

    QByteArray can hold arbitrary binary data and is easily written to the socket device. In a rough, raw form:
    Qt Code:
    1. const QByteArray payload = ui->lineEdit->text().toUtf8();
    2. QByteArray message;
    3. message.append('\x00'); // sync
    4. message.append('\x00'); // reserved
    5. message.append(static_cast<char>(payload.size() & 0xFF)); // payload size LSB
    6. message.append(static_cast<char>(payload.size() >> 8 & 0xFF)); // payload size MSB
    7. message.append(payload);
    8. message.append('\x00'); // end byte 1
    9. message.append('\x00'); // end byte 2
    10. qDebug() << message.toHex();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    Miss Engineer (17th April 2014)

  4. #3
    Join Date
    Apr 2014
    Posts
    14
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sending binary data packet over TCPSocket

    This worked perfectly. Thank you very much.

    Quote Originally Posted by ChrisW67 View Post
    QByteArray can hold arbitrary binary data and is easily written to the socket device. In a rough, raw form:
    Qt Code:
    1. const QByteArray payload = ui->lineEdit->text().toUtf8();
    2. QByteArray message;
    3. message.append('\x00'); // sync
    4. message.append('\x00'); // reserved
    5. message.append(static_cast<char>(payload.size() & 0xFF)); // payload size LSB
    6. message.append(static_cast<char>(payload.size() >> 8 & 0xFF)); // payload size MSB
    7. message.append(payload);
    8. message.append('\x00'); // end byte 1
    9. message.append('\x00'); // end byte 2
    10. qDebug() << message.toHex();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 30th November 2010, 08:54
  2. Sending over UDP - Binary Manipulation
    By Cotlone in forum Newbie
    Replies: 6
    Last Post: 9th June 2010, 10:24
  3. Packet getting fragmented when sending over QTcpSocket
    By arturo182 in forum Qt Programming
    Replies: 14
    Last Post: 6th August 2009, 00:11
  4. binary file sending using qt4.3
    By omprakash in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2008, 21:56
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 19:10

Tags for this Thread

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.