Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to send structures over socket?

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!

like image 947
JJ Liu Avatar asked Dec 14 '11 23:12

JJ Liu


People also ask

How do you send structures in sockets?

Use network protocols as network protocols. Design your protocol, in octets, and write yourself a library to send and receive it. Or use an existing one, such as DML, XDR, ... Using structs introduces at least six dependencies you may not even be aware of, and causes further problems like this one.

Can socket programming be done in C?

It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. At this point, the connection is established between client and server, and they are ready to transfer data.

What is socket function C?

DESCRIPTION. The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created ...


1 Answers

The phrasing of your question suggests that what you're looking for is this:

company foo;
send(sockfd, &foo, sizeof(foo), 0); // DO NOT do this

This will basically dump all the memory of the company struct into your socket. This WILL NOT WORK in this instance. And even when it sort of works, it's a really bad idea. The reason it won't work is that vectors do not contain the data directly. They point at it. This means that when you dump the structure containing vectors into the socket, you will be dumping pointers to memory, but not the stuff being pointed at. This will result in (at best) crashes on the receiving side.

It would sort of work for individual person or car objects. These contain no pointers, and so their memory contains all the relevant values'

On the sending side:

person joe = { "Joe", 35 };
send(sockfd, &joe, sizeof(joe), 0); // may work, but is a bad idea, see below

On the receiving side:

person joe;
recv(sockfd, &joe, sizeof(joe), 0);

But, this is still a bad idea. It relies on the sending side and receiving side having the exact same memory layout for their structures. This may not be true for any number of reasons. Some include one being on a PowerPC chip, and the other being on an Intel x86 chip. Or one being on a Windows machine compiled with Visual Studio and the other being on a Linux machine compiled with gcc. Or maybe someone has tweaked some compiler flags that cause the default structure layout to be different. Any number of reasons.

Really, you ought to use a serialization framework like everybody here has suggested. I would suggest Google protocol buffers or the Boost serialization framework that other people have already linked to. But there are many others.

Another serialization framework that should be mentioned because it is blazingly fast (almost as fast as straight dumping the memory image of a struct into a socket) is Cap'n Proto.

like image 161
Omnifarious Avatar answered Sep 19 '22 05:09

Omnifarious