I am doing unix network programming over cygwin. I am trying to receive an object via the recvfrom() function (I am using UDP sockets). The class definition of that object is :
class Message{
public:
int ID;
int seq;
string *s;
};
Message::Message(){
s = new string();
}
The object sending is fine. The receive operation looks fine but There is a problem. I am giving you the code:
You're overwriting the object s. When you try to dereference s you're probably addressing memory outside your address space (if you're lucky). In fact, it would be worse if the program didn't crash because then you'd be trashing memory in your program.
You have to serialise/deserialise your object if you want to sent it over a link.
Are you aware of the implications of passing such a structure with an unreliable protocol?
A common(and dirty) way to achieve the serialization/deserialization you want in C is to create a struct along the lines of:
1 2 3 4 5 6
struct packet {
uint32_t id;
uint32_t seq;
uint8_t data[1];
};
/* add the proper attribute for your compiler to ensure no padding(eg. __attribute__((packed))" for gcc) */
Notice the data-arry with a size of one. The trick here is that you can allocate a memblock of needed size and cast it to the packet struct. Now you can access the data using the array index by deliberatly indexing "out of bounds". Example:
1 2 3 4 5 6 7 8 9
/* A packet with a data section of 100 bytes */
struct packet *my_packet = (struct packet *)malloc(sizeof(struct packet) + 99);
my_packet->id = htonl(the_id_to_use);
my_packet->seq = htonl(the_seq_to_use);
memcpy(my_packet->data, the_data_to_add, 100);
/* Random access to data using array index */
my_packet->data[34] = 0xff;
/* xmit */
send(my_sock, my_packet, sizeof(struct packet) + 99, 0);
Now on the recieving end you can cast a recieve buffer to the packet struct, do the needed byte conversions and your ready :-)
This is not the cleanest way of doing [de]serialization, but it can be convenient IMHO.