Results 1 to 2 of 2

Thread: Sending structure throurh TCP socket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Sending structure throurh TCP socket

    I am using TCP socket to send the below structure data from one application to another application

    Struct message
    {

    std::string messagedata;
    std::string date;
    std::string time;
    std::string source;
    std::string filepath;
    }

    sending side i have used below code to send the structure data

    message *l_msgInfo = new message;
    char *p = (char*)l_msgInfo; // cast it to char* to make a QByteArray
    QByteArray packet(p, sizeof(message));


    at receiver side i have used below code

    message *l_info = new messageinfo;
    QByteArray l_ba;
    message *l_info = new message;
    l_inFromClient >> l_ba; l_info = (message*)l_ba.constData();


    problem facing:
    sender side, if string size is less than 16 bytes(example: error_messages1) than receiver side i am getting that string, otherwise i am receiving Bad_pointer error.

    example1:
    sender side:
    message.messagedata = "error_messages1" //less that 16bytes
    message.date= "04-06-2013" //less that 16bytes
    message.time= "17::05::02" //less that 16bytes
    message.source= "error_messages2" //less that 16bytes
    message.filepath= "C:\temp" //less that 16bytes

    receiver side i am getting the complete structure

    example2:
    sender side:
    message.messagedata = "error_messages123" //greaterthan 16bytes
    message.date= "04-06-2013" //greaterthan 16bytes
    message.time= "17::05::02" //less that 16bytes
    message.source= "error_messages2" //less that 16bytes
    message.filepath= "C:\temp\test\filter\file" //greaterthan 16bytes

    at receiver side am get

    message.messagedata = "Bad Ptr"
    message.date= "04-06-2013"
    message.time= "17::05::02"
    message.source= "error_messages2"
    message.filepath= "Bad Ptr"

    please help me out to how to resolve this problem?
    Thanks in advance,
    Veeresh.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Sending structure throurh TCP socket

    You cannot send that structure that way, when it contains datatype which are not binary copyable.

    Here is just one example:
    You can convert all the structure data in to char data and then send it
    Qt Code:
    1. struct message
    2. {
    3. std::string messagedata;
    4. std::string date;
    5. std::string time;
    6. std::string source;
    7. std::string filepath;
    8. }
    9.  
    10. message msg;
    11. std::string str = msg.messagedata + "," + msg.date + "," + msg.time + "," + msg.source + "," + msg.filepath;
    12.  
    13. QByteArray bytes(str.c_str(), str.length());
    14.  
    15. //send bytes
    16.  
    17. //reveive bytes.
    18. // split it into sub-string
    19. // store is back into struct.
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    veeresh (10th June 2013)

Similar Threads

  1. Replies: 7
    Last Post: 12th September 2011, 10:52
  2. sending quint32 variable in socket
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 24th June 2010, 01:56
  3. QUdpSocket: sending and receiving on the same socket?
    By mhoover in forum Qt Programming
    Replies: 16
    Last Post: 17th June 2009, 03:28
  4. sending structure through udp socket programming
    By hemrajn in forum Qt Programming
    Replies: 9
    Last Post: 15th May 2009, 23:55
  5. sending int array through socket
    By babu198649 in forum Qt Programming
    Replies: 5
    Last Post: 20th December 2007, 08:16

Tags for this Thread

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.