[Qt-interest] QTcpSocket & QDataStream

pmqt71 pmqt71 at gmail.com
Mon Feb 7 15:34:22 CET 2011


Hi Jason,
that was my error: on one side using QDataStream and QByteArray, on the
other side only QDataStream.
I was looking at the fortune example and it seemed to me to do in the same
way.
Now I'm not using QByteArray and the problem is over.
thanks
pm



2011/2/5 Jason H <scorp1us at yahoo.com>

>  You're using a datastream on one side but not the other? If so, this is
> what I would expect. If you're exchanging multiple objects (including
> strings, ints, etc) You need to now where one starts and the other ends. If
> you're using Qt on both aides and not implementing some standard protocol
> (e.g. http) then use the DataStream stuff on both sides. It is by far the
> easiest solution. lo
>
> If, for example, you're talking to a webserver, then you'll need to use the
> header information and/or chunked-length encoding.
>
> The only other option is some application-level framing/protocol. Like
> struct { int opcode; int datalen; char data[maxlen];} But even so, if you
> can write operators >> << for it, QDataStream will serialize that for you.
>
>
>
>  ------------------------------
> *From:* pmqt71 <pmqt71 at gmail.com>
> *To:* qt-interest at qt.nokia.com
> *Sent:* Sat, February 5, 2011 10:01:48 AM
> *Subject:* Re: [Qt-interest] QTcpSocket & QDataStream
>
> Both client and server run on my laptop. Same Qt version, compiler...
>
> the setVersion is called in both sides on QDataStream
>
> *setVersion(QDataStream::Qt_4_0);*
>
>
> 2011/2/5 Andreas Pakulat <apaku at gmx.de>
>
>>  On 05.02.11 11:02:15, pmqt71 wrote:
>> > Hi,
>> >
>> > I'm sending data from a client to a server using QTcpSocket just as
>> > described in the fortune example. But when the server reads, it finds
>> the
>> > data shifted by 4 bytes (in wich I find the length of the QByteArray
>> used in
>> > the client side).
>> >
>> > Something similar happens in the following example. QByteArray::data (or
>> > constData) is the same used by QTcpSocket to write a QByteArray.
>> >
>> >
>> > *QByteArray ba;*
>> > *char *ss = NULL;*
>> > * *
>> > *QDataStream ds(&ba, QIODevice::WriteOnly);*
>> > *ds << "hello";*
>> > *ss = ba.data();   //ss is empty*
>> > *ss += 4;            //ss contains "hello"*
>> >
>> > The first 4 bytes seems to contain the length of ba: 0,0,0,6
>> >
>> > If I don't use QDataStream I have no extra bytes:
>> >  *QByteArray ba;*
>> > *char *ss = NULL;*
>> > * *
>> > *ba.append("hello");*
>> > *ss = ba.data();   //ss contains "hello"*
>> >
>> >
>> > I should skip the first 4 bytes before reading my data (that already
>> contain
>> > a length), but I don't find the same instruction in the fortune example.
>> > Where is the problem in my code?
>>
>> Are you using the same qt version to read the content as you use for
>> writing? Or maybe you don't use Qt at all for reading? QDataStream has
>> an internal data format which is being used to store the data you put
>> into it into a list of bytes. This format is specific to QDataStream and
>> may change between different Qt versions, hence you can specify which
>> QDataStream format you want to use with the aproriate setter. So make
>> sure that both sides use QDataStream to read the data and they use the
>> same data-format version.
>>
>> Andreas
>>
>> --
>> Beware of a tall blond man with one black shoe.
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110207/629cf6bb/attachment.html 


More information about the Qt-interest-old mailing list