views:

1251

answers:

10

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

+1  A: 
Nikolai N Fetissov
Thanks for pointing that the calls on the server were out of order. I changed the code on the client side from "int addr" to "void *addr." is this correct?
Eric de Araujo
Does it still segfault?
Nikolai N Fetissov
Yes it still segfaults. The question has been updated with my most recent code and errors.
Eric de Araujo
This solved the problem with handling the tpl side of things correctly. After I corrected my handling of the buffers and sockets I was able to pass the structure successfully.
Eric de Araujo
A: 

will you post how you were able to send this structure using sockets?

Sure thing. I'll get the code up later today. Thanks for reminding me that I hadn't posted a successful implementation.
Eric de Araujo
I hope you find that helpful.
Eric de Araujo
A: 

Per request, here is a successful implementation using sockets:

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:

char buf[256]; // buffer for client data
int nbytes;

if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {

//error handling

} else {
    tpl_node *tn;

    struct ci {
        char c;
        int i;
    };

struct ci receive;

tpl_map("S(ci)", &receive);
tpl_load(tn, TPL_MEM, buf, nbytes );
tpl_unpack(tn, 0);
tpl_free(tn);

printf("Struct: {%c,%d}\n", receive.c, receive.i);
Eric de Araujo
A: 

Hey! thanks for the prompt response, however I receive 0 nbytes with your code. My struct contains two integers, however, I tried your way as well with the same results. In my case, the server is sending a struct containing response data (simulating Ack, or Nack) to the client, but it should be no different.

A: 

Hey Guy! Nevermind This works. I needed to allocate memory for *addr. Thanks!

A: 

Hi. I'll need to use tpl implementation like this. So, I need to know how I include the "tpl.h" on my project.

I'm using Visual Studio and tried to include the file in the library dependences after downloading it. I didn't have success though.

Can you help?

A: 

Nikolai, I really appreciate your post. It saved my life! Thank you so much!.

Anselmo Alves
A: 

i have implement tpl as shown above but getting the following error on both client and server tclient.c:(.text+0x26c): undefined reference to tpl_map' tclient.c:(.text+0x284): undefined reference totpl_pack' tclient.c:(.text+0x2a8): undefined reference to tpl_dump' tclient.c:(.text+0x2b4): undefined reference totpl_free' collect2: ld returned 1 exit status

note = i already copy the file tpl.c & tpl.h in my project floder...

need help :(:(

fayaz
A: 

i have occur an error on client side from where i m sending struct to server side using tpl libaray .. error says

unsupported option n failed to parse S(input) Segmentation fault

where input is my struct name................havnt use any 'n' ...kindly reply its urgent

fayaz