Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED]Need Help:structure over socket

[SOLVED]Need Help:structure over socket

Scheduled Pinned Locked Moved Mobile and Embedded
23 Posts 6 Posters 14.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Nico
    wrote on 28 Jan 2013, 07:05 last edited by
    #1

    How can I send and receive a structure into a socket? can somebody give me a working Qt code/example? thanks

    (Client Side)Ex.:
    @
    typedef struct structName
    {
    int varint;
    char varchar [10];

    }structName;...

    void Client::send()
    {
    structName structNameSend
    structNameSend.varint=9999;
    structNameSend.varchar="Message";
    socket->write( structNameSend...);
    etc...
    }...

    void Client::receive()
    {

    bytes = buffer->write(socket->readAll());
    buffer->seek(buffer->pos() - bytes);
    while (buffer->canReadLine())
    {
    QString line = buffer->readLine();
    capture->append(line.simplified());
    }
    etc...
    }...
    @

    [[Added code markup, Tobias]]

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 28 Jan 2013, 09:25 last edited by
      #2

      I'd define a set of streaming operators to stream your struct into and out of a [[doc:QDataStream]]. Then, you can create a QDataStream on your socket, and use that to write and read your type to/from the socket.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on 28 Jan 2013, 10:11 last edited by
        #3

        Litle example for your struct:
        in *.h file
        @struct structName
        {
        int varint;
        char varchar [10];
        };
        QDataStream &operator <<(QDataStream &out,const structName &dataStruct);
        QDataStream &operator >>(QDataStream &in, structName &dataStruct);@

        in *.cpp file
        @QDataStream &operator <<(QDataStream &out,const structName &dataStruct)
        {
        out << dataStruct.varint;
        out.writeRawData ( dataStruct.varchar, 10 );
        return out;
        }

        QDataStream &operator >>(QDataStream &in, structName &dataStruct)
        {
        dataStruct = structName();
        in >> dataStruct.varint;
        in.readRawData ( dataStruct.varchar, 10 )
        return in;
        }@

        didn't test it but i think it should work.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qt Nico
          wrote on 30 Jan 2013, 00:31 last edited by
          #4

          Thank you. It works great.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on 30 Jan 2013, 05:27 last edited by
            #5

            You’re welcome ! :)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on 21 Feb 2013, 16:22 last edited by
              #6

              Note that QDataStream may add metadata into the stream for its own use. You will not see it, but this may be a problem if your application has to be compatible with other non-Qt clients or if you have to implement a defined wire-protocol.

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qt Nico
                wrote on 7 Mar 2013, 00:23 last edited by
                #7

                I have successfully transmit the structured data over
                the network with this "transmit function".

                @void Client::transmit()
                {
                structName structCall;

                structCall.varint = 5;
                structCall.varchar [10] ='AaBbCcDdEeFf';

                socket->write(structCall.varint + structCall.varchar [10] );
                }
                @
                Now, how can I put the structured data (databytes)
                that this "receive function" is receiving from socket to update the "structName structCall".
                @
                void Client::receive()
                {
                structName structCall;
                qDebug() << "DATA" << socket->readAll();
                }
                @
                I am hoping somebody could help me with this situation.

                Am... I have also a question. what is the correct way of sending the datastruct to a socket.
                as you can see in my "transmit() function" I have used
                @socket->write(structCall.varint + structCall.varchar [10] );@

                is there a simpler way in sending my entire struct over a network?
                like:
                @socket->write(structcall);@

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qxoz
                  wrote on 7 Mar 2013, 04:34 last edited by
                  #8

                  Hi Qt Nico!
                  I'm not clearly understand you. I thought you resolved issue with data stream. Can you give more explaination?
                  And also can you put your code in @ tags.

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Nico
                    wrote on 7 Mar 2013, 05:53 last edited by
                    #9

                    sorry, if I did not stated my problem clearly.

                    Alright, this is my "receive function".
                    @
                    void Client::receiveMessage()
                    {
                    qint64 bytes = buffer->write(socket->readAll());
                    buffer->seek(buffer->pos() - bytes);

                    QString structContainer = buffer->readAll();
                    
                    qDebug() << structContainer;
                    
                    structName structCall;
                    

                    }
                    @

                    so, how can I put the values of "structContainer" to my structCall?
                    thanks

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on 7 Mar 2013, 06:26 last edited by
                      #10

                      Here is the implementation of idea:
                      @void transmit()
                      {
                      structName structCall;
                      structCall.varint = 5;
                      structCall.varchar [10] ='AaBbCcDdEeFf';

                      QByteArray sendArray;
                      QDataStream out(&sendArray,QIODevice::WriteOnly);
                      out.setVersion(QDataStream::Qt_4_8);
                      out << structCall;
                      socket->write(sendArray);
                      

                      }

                      void receiveMessage()
                      {
                      QByteArray receiveArray;
                      structName structCall;
                      receiveArray.append(socket->readAll());

                      QDataStream in(&receiveArray,QIODevice::ReadOnly);
                      in.setVersion(QDataStream::Qt_4_8);
                      in >> structCall;
                      

                      }@

                      You should make some modifications and write few checks for correct data.
                      Is it what you ask?

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KA51O
                        wrote on 7 Mar 2013, 07:23 last edited by
                        #11

                        [quote author="qxoz" date="1362637580"]Here is the implementation of idea:
                        @void transmit()
                        {
                        structName structCall;
                        structCall.varint = 5;
                        structCall.varchar [10] ='AaBbCcDdEeFf';

                        QByteArray sendArray;
                        QDataStream out(&sendArray,QIODevice::WriteOnly);
                        out.setVersion(QDataStream::Qt_4_8);
                        out << request;
                        socket->write(sendArray);
                        

                        }@
                        [/quote]
                        I think in line 10 of the transmit function its supposed to be out << structCall ?

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on 7 Mar 2013, 07:27 last edited by
                          #12

                          Yes you right :). I missed it.
                          Fixed now.
                          Thanks KA51O.

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            Qt Nico
                            wrote on 7 Mar 2013, 09:47 last edited by
                            #13

                            This is exactly what I am looking for.

                            But I noticed, when I try to send the data.

                            for example:

                            I give varint with a value of 5 on my "transmit function"
                            @
                            structCall.varint = 5;
                            socket->write(sendArray);
                            qDebug() << "DATA" << sendArray;
                            @

                            and my "Application Output" reported:
                            "DATA 00000005"

                            And it actually sends "00000005" data instead of "05" data only over the socket.
                            so, what should I do to eliminate the first "000000" and enable me to send only "05".

                            thanks qxoz, you save a lot of time of me.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on 7 Mar 2013, 09:58 last edited by
                              #14

                              I don't quite get the roundtrip through QByteArray, to be honest. QStreamWriter can operate directly on the socket...

                              Anyway, I will assume your implementation of the streaming operator for your structName is still like this:
                              @
                              QDataStream &operator <<(QDataStream &out,const structName &dataStruct)
                              {
                              out << dataStruct.varint;
                              out.writeRawData ( dataStruct.varchar, 10 );
                              return out;
                              }
                              @

                              Here, you first output the int. You are using a plain int, but I'd recommend to always use variables with a guaranteed size for these purposes. int normally is the same as qint32. So, you have a 32 bits (four bytes) value. You will need to send all of these bytes in the stream. If your value has a smaller range, you should use a smaller data type like quint16 or qint8. Otherwise, on the receiving side, there is no way to know how to read in the data again. Note that this is binary data, not text that you are seeing.

                              1 Reply Last reply
                              0
                              • Q Offline
                                Q Offline
                                qxoz
                                wrote on 7 Mar 2013, 10:15 last edited by
                                #15

                                [quote author="Andre" date="1362650288"]I don't quite get the roundtrip through QByteArray, to be honest. QStreamWriter can operate directly on the socket...
                                [/quote]
                                This failings of my knowledge :)

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on 7 Mar 2013, 10:19 last edited by
                                  #16

                                  /teacher mode...

                                  QDataStream is a class that is meant to operate on a [[doc:QIODevice]]. The socket is such an IO device. If you use it on a QByteArray, it actually creates a [[doc:QBuffer]] in the background. QBuffer is a class that provides a QIODevice interface on a QByteArray.

                                  1 Reply Last reply
                                  0
                                  • Q Offline
                                    Q Offline
                                    Qt Nico
                                    wrote on 7 Mar 2013, 10:32 last edited by
                                    #17

                                    Sir Andre, your recommendation actually works with int. But I still having a problem with variable type like QbyteArray and char.

                                    1 Reply Last reply
                                    0
                                    • Q Offline
                                      Q Offline
                                      qxoz
                                      wrote on 7 Mar 2013, 11:55 last edited by
                                      #18

                                      Thank you Andre.
                                      Qt Nico@what problem with char do you have?

                                      1 Reply Last reply
                                      0
                                      • Q Offline
                                        Q Offline
                                        Qt Nico
                                        wrote on 8 Mar 2013, 00:40 last edited by
                                        #19

                                        Problem is now solved. Thanks Sir Andre and qxoz. thanks to everyone.

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          andre
                                          wrote on 8 Mar 2013, 09:38 last edited by
                                          #20

                                          Note that I'd recommend against sending raw data in the form of a @char[10]@ or something like that. I'd just use a QByteArray instead.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved