Alex Rivera | Logout

C++ How to send structures over socket?

Asked 2011-12-14T23:27:52.193
14

Say I have a structure:

struct person
{
    char name[10];
    int age;
};

struct car
{
    int locationX;
    int locationY;
};

struct company
{
    vector<person> employees;
    vector<car> cars;
};

For example, I want to send/recv the whole company using socket (UDP). So, send and recv once.

How can I do that? Could you please give me some code sinppet? How to send everything and read everything.

Thanks!

Edit
Report

1 Answer

8

If you are to make many of these, you may want to look in Thrift. It will automatically generate all the necessary code to serialize all the structures for you so you don't have to do it manually.

They support strings too so very practical.

Oh! And it's free. 8-)

http://thrift.apache.org/

Another thing, it sends binary data as such, so you won't have to convert numbers to strings and vice versa. That's a lot faster to do so if most of your data are not strings.

answered 2011-12-15T05:29:19.917

Your Answer