Send a structure through a socket using tpl for serialization using c -


After reading some related questions, I have decided to use it to sort my structures so that they have chairs Can get it by sending. I am having trouble understanding how to send and receive TPL images through sockets. When I call the tpl_dump function, I get a graphical error on the server side.

I know that the chairs are working on both sides because I wanted to send the code back and forth first. I was able to create a tpl image on the client without any problem and even use tpl to read.

It is not the structure that I want to send back and forth, but I hope to understand this, so sample out so that I can do this in the future. I know that I have some mismatch between incoming buffer and tpl_dump . I am still learning (According to my previous questions) I apologize if there are errors in my code.

Edit Nicolae said that the issues have been corrected in the code below

Customer code: P>

  tpl_node * tn; Zero * edit; Size_t lane; Struct ci {char c; Int i; }; Struct CI sample = {'A', 1}; Tn = tpl_map ("S (CI)", and sample); / * Pass structure address * / tpl_pack (tn, 0); Tpl_dump (tn, TPL_MEM, & amp; addr, & amp; len); Tpl_free (Tamil Nadu); Send (Sokafide, Adrer, Lane, 0);  

Server code:

  if ((nbytes = recv (i, buf, sizeof buf, 0)) & lt; = 0) {// Error handling} other {tpl_node * tn; Struct ci {char c; Int i; }; Structure CI recipient; Tpl_map ("S (CI)", & amp; amp; recieve); Tpl_load (tn, TPL_MEM, & amp; buf, & amp; nbytes); Tpl_unpack (TN, 0); Tpl_free (Tamil Nadu);  

If it comes in handy -

It seems Just like you are abusing at least tpl_dump on the customer - the second argument should not be zero ** , int * . Macro Store Your Buffer Address Allocated in the addr integer, so that you are sending data (some trash with data) to the server, the sequence of calls on the server side also exits the order.

Looking closer to your source:

  • From << code> buf and lane Code>; Expected tpl_load to zero * and size_t ;
  • You need free () when you are doing;
  • You need to look for less socket reading - you can get less (or more), then you have read from the TCP socket.

Without a sockets, here is a simple example:

  #include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; #include "tpl.h" int main (int argc, char * argv []) {char * buffer; Size_t i, lane; Tpl_node * tn; Struct ci {char c; Int i; } S = {'a', 1}, s1; Printf ("Input {% c,% d} \ n", s.c, s.i); Tn = tpl_map ("s (ci)", and s); / * Pass structure address * / tpl_pack (tn, 0); Tpl_dump (tn, TPL_MEM, and buffer, and lane); Tpl_free (tn); Printf ("size =% lu \ n", lane); Tn = tpl_map ("s (ci)", & s1); Tpl_load (tn, TPL_MEM, buffer, lane); Tpl_unpack (TN, 0); Tpl_free (tn); Free (buffer); Printf ("Output {% c,% d} \ n", s1.c, s1.i); Return 0; }  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -