PDA

View Full Version : tcpscoket problem



aj2903
18th December 2008, 08:20
Hi..
I want simple tcpClient tcpServer communcation example.
I have read fortuneClient & fortuneServer but have found it difficult to understand.
Is their any simple example.

Pls help..

muellerp
18th December 2008, 08:32
What part is difficult to understand?
I don't know what to add to these examples.

aj2903
18th December 2008, 08:34
sending the data & recieving it is little difficult.
Can u explain it?

muellerp
18th December 2008, 14:51
Sending data:
Assume you already have a socket established.

Then sending on the socket is just as in the example:


QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);

out << QString("test");

clientConnection->write(block);


You need a byte array. You define a Datastream based on the byte array.
You feed the byte array with data and when finished you just write it to the socket.
Thats how you send data.

Receiving data is that you react on the signal bytesAvailable() of the socket.
You get again a datastream. It is now similar to sending data.

But for this one it is important to understand that the signal just tells that some data has arrived, not that all data has arrived.
It is your job to check if everything is already there what you need. That's why in the Fortune example the first thing transmitted is the length of your packet (which I have removed in above example).

Any futher hint needed?