After reading a few related questions I've decided to use the tpl library to serialize my structures in order to send and receive them through sockets. I'm having trouble understanding how to send and receive the tpl images using sockets. I get a segfault error on the server side when I call the tpl_dump
function.
I know the sockets are working on both end because I was using the code to previously send strings back and forth. I was also able to use tpl to create and read an tpl image on the client without any issues.
This isn't the structure I eventually want to be sending back and forth, but I hope to figure this sample out so I can do this in the future. I know I'm mishandling something between the incoming buffer and the tpl_dump
. I'm still learning C (as evidenced by my previous questions) so I apologize if I have glaring errors in my code.
Edits
The issues Nikolai pointed out have been corrected in the code below. However the server code logs error: tpl_load to non-root node
and still segfaults at tpl_unpack(tn, 0);
Client Code:
tpl_node *tn;
void *addr;
size_t len;
struct ci {
char c;
int i;
};
struct ci sample = {'a', 1};
tn = tpl_map("S(ci)", &sample); /* pass structure address */
tpl_pack(tn, 0);
tpl_dump(tn, TPL_MEM, &addr, &len );
tpl_free(tn);
send(sockfd, addr, len, 0);
Server Code:
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
//error handling
} else {
tpl_node *tn;
struct ci {
char c;
int i;
};
struct ci recieve;
tpl_map("S(ci)", &recieve);
tpl_load(tn, TPL_MEM, &buf, &nbytes );
tpl_unpack(tn, 0);
tpl_free(tn);
In case this will come in handy - tpl user's guide