PDA

View Full Version : QTcpSocket write() Trasmission Data Design



Daxos
15th July 2010, 10:28
Hi,
I have a network application.
I want communicating between client and server whit QTcpsocket.

My server process some information that i want to be able to send to the client.
This information is a struct:

typedef struct Test{
QTcpSocket socket;
QString string;
};

How I can transfer this structure with TcpSocket?
The signature of "write" is:

write ( const char *, qint64 ) : qint64
write ( const char * ) : qint64
write ( const QByteArray & ) : qint64
writeData ( const char *, qint64 ) : qint64

I don't know how use the write function of tcpSocket to do it.

Can anyone help me please?

Thanks, Bye

Daxos
15th July 2010, 14:35
I have try whit QDataStream:



QByteArray block;

QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << m_xMyStruct;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));



where MyStruct is a personal data type.

The problem is that QDataStream support operator << only for predefined data type.

Thanks, Bye

tbscope
15th July 2010, 14:41
The problem is that QDataStream support operator << only for predefined data type.

Then you need to create a class that implements the << and >> operators.
After that you need to register it.

That said, you don't need to send the socket object as this is completely wrong logic. The object only lives on your pc.
Why do you want to do this?

Daxos
15th July 2010, 15:24
Then you need to create a class that implements the << and >> operators.
After that you need to register it.


Mmmm, do you know how I can do it? I think that it's complicated..
Why Qt doesn't be able to send persona data type?



That said, you don't need to send the socket object as this is completely wrong logic. The object only lives on your pc.
Why do you want to do this?

Sorry, is an cut&paste error ;).

tbscope
15th July 2010, 19:19
Mmmm, do you know how I can do it? I think that it's complicated..
Why Qt doesn't be able to send persona data type?

But Qt can do this, you only need to tell what and how. Nokia can't provide serialisation of data for each and every case. That's why there is a mechanism to do this yourself.

Some suggestions to make it yourself a little bit easier.
Forget about the << and >> operators, instead do one of the following:

1. If you only need to send text and not binary data, use a QTextStream
2. If you need to send binary data too, use a QDataStream

In both cases, you can do one of the following:
1. Each segment of your stream has a certain predefined length (like in the old'en days), then you can parse it based on segment length.
2. Use a separator like a comma for example.

Both the above suggestions are stupid, you waste bandwidth in the first case and you make it yourself difficult by excluding control characters in the second case.

Therefor I suggest you use the following class:
http://doc.qt.nokia.com/4.6/qxmlstreamwriter.html
and
http://doc.qt.nokia.com/4.6/qxmlstreamreader.html

They are very easy to use and you can keep your code top level without going into the gory details. You can even plug those directly into a tcp socket.