Note that there are some explanatory texts on larger screens.

plurals
  1. POSend a structure through a socket using tpl for serialization using c
    primarykey
    data
    text
    <p>After reading a few related questions I've decided to use the <a href="http://tpl.sourceforge.net/" rel="nofollow noreferrer">tpl library</a> 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 <code>tpl_dump</code> function. </p> <p>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.</p> <p>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 <code>tpl_dump</code>. I'm still learning C (as evidenced by my previous questions) so I apologize if I have glaring errors in my code.</p> <p><strong>Edits</strong> The issues Nikolai pointed out have been corrected in the code below. However the server code logs <code>error: tpl_load to non-root node</code> and still segfaults at <code>tpl_unpack(tn, 0);</code></p> <p>Client Code:</p> <pre><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)", &amp;sample); /* pass structure address */ tpl_pack(tn, 0); tpl_dump(tn, TPL_MEM, &amp;addr, &amp;len ); tpl_free(tn); send(sockfd, addr, len, 0); </code></pre> <p>Server Code:</p> <pre><code>if ((nbytes = recv(i, buf, sizeof buf, 0)) &lt;= 0) { //error handling } else { tpl_node *tn; struct ci { char c; int i; }; struct ci recieve; tpl_map("S(ci)", &amp;recieve); tpl_load(tn, TPL_MEM, &amp;buf, &amp;nbytes ); tpl_unpack(tn, 0); tpl_free(tn); </code></pre> <p>In case this will come in handy - <a href="http://tpl.sourceforge.net/userguide.html" rel="nofollow noreferrer">tpl user's guide</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload