Results 1 to 5 of 5

Thread: How to send my own memory buffer via QTcpSocket and QTcpServer?

  1. #1
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    5

    Question How to send my own memory buffer via QTcpSocket and QTcpServer?

    Hello,

    I'm a newbie to QT . Can anyone give me some hints on how I can send my own memory buffer which contains a rather complicated data structure via QTcpSocket or QTcpServer?

    Here is what I'm trying. But it seems not work.

    Thank you for your help in advance!

    //QBuffer buffer;
    QByteArray buffer;
    QDataStream out(&buffer, QIODevice::WriteOnly);
    char *to =(char*) buffer.data().data();
    memcpy(to,&myOwnBufffer, myOwnBufferSize);
    out.device()->seek(0);
    out << (quint16)(buffer.size() - sizeof(quint16));
    tcpSocket->write(buffer);

    myOwnBuffer is a structure<-structures<-structures (contains several data type).

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to send my own memory buffer via QTcpSocket and QTcpServer?

    First of all be aware that if your data structure contains any pointers or non-POD objects (like classes), you will receive garbage on the receiving end.

    Now that you have been warned...

    The easiest way to do what you want is:
    Qt Code:
    1. socket->write((const char*)&myOwnBuffer, myOwnBufferSize);
    To copy to clipboard, switch view to plain text mode 
    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. The following user says thank you to wysota for this useful post:

    fulin (22nd March 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    5

    Default Re: How to send my own memory buffer via QTcpSocket and QTcpServer?

    Thank you, wysota. It works well.

    For receiving data, I'm thinking about doing the same way

    socket->read(&myReceiveBuffer, myDataMaxSize);

    Just do not know how to decide myDataMaxSize. It seems that I must know what is the expected data in advance. For the QByteArray data, the QTcpSocket seems can know the max size.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to send my own memory buffer via QTcpSocket and QTcpServer?

    In general the way you are doing it is a wrong approach. A good approach would be to implement streaming operators to and from QDataStream for your structure and use that instead of sending raw data over network.
    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.


  6. #5
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    5

    Default Re: How to send my own memory buffer via QTcpSocket and QTcpServer?

    Yes, you are right.

    But here it is for coping with our legacy native C code.

Similar Threads

  1. Replies: 4
    Last Post: 30th November 2010, 21:09
  2. QTcpServer QTcpSocket problem
    By jmsbc in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2009, 17:42
  3. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2009, 23:16
  4. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 21:38
  5. QTcpSocket readyRead and buffer size
    By pdoria in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2008, 10:11

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.