PDA

View Full Version : passing structure from tcpserver to client



aj2903
18th December 2008, 09:32
hii


struct Stcp
{
int a;
float b;
char [50];
}

Stcp sendTcp;

I want to send whole sendTcp from server to client.
Pls help...

caduel
18th December 2008, 11:09
Do you *know* that the client is "binary" compatible with the server?
If not, you might have to convert the int from little to big endian...

If that is not an issue:


// assuming your socket is called socket
socket.write(QByteArray::fromRawData(&sendTcp, sizeof(sendTcp)));
(You don't need to send the size of that packet, as the size is fix.)

If that is an issue:
construct a QDataStream on the socket, and stream all the members out.
Prefix the packet by the packet size, so the client has a chance to know when the complete packet has been received.

The network-fortuneserver examples shows, how to add the package's size to the package.
The network-fortuneclient shows how to wait til the complete package has been received.

HTH

jpn
18th December 2008, 14:15
If both sides are Qt, it might be also worth implementing QDataStream stream operators. See Qt Centre Wiki: Using custom data types with Qt for more details.

aj2903
22nd December 2008, 09:35
hii caduel

i'm getting folowing error :

error: no matching function for call to `QByteArray::fromRawData
(sendTcp*, unsigned int)'

Pls help.

caduel
22nd December 2008, 10:11
You have to cast that pointer to const char*, then it should work.
(Note that jpn is right: if both sides are written by you, then the little work of supporting QDataStream is probably a good idea.)