Results 1 to 5 of 5

Thread: passing structure from tcpserver to client

  1. #1
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default passing structure from tcpserver to client

    hii

    Qt Code:
    1. struct Stcp
    2. {
    3. int a;
    4. float b;
    5. char [50];
    6. }
    7.  
    8. Stcp sendTcp;
    To copy to clipboard, switch view to plain text mode 

    I want to send whole sendTcp from server to client.
    Pls help...
    Last edited by jpn; 18th December 2008 at 14:09. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: passing structure from tcpserver to client

    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:

    Qt Code:
    1. // assuming your socket is called socket
    2. socket.write(QByteArray::fromRawData(&sendTcp, sizeof(sendTcp)));
    To copy to clipboard, switch view to plain text mode 
    (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

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: passing structure from tcpserver to client

    If both sides are Qt, it might be also worth implementing QDataStream stream operators. See Qt Centre Wiki: [WIKI]Using custom data types with Qt[/WIKI] for more details.
    J-P Nurmi

  4. #4
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: passing structure from tcpserver to client

    hii caduel

    i'm getting folowing error :

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

    Pls help.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: passing structure from tcpserver to client

    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.)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.