How to transfer from the server to the client the value of the variable of the structure type? In send, you need to convert it to char *. How to do this using the SI language?

2 answers 2

In principle, you can write

send(sock, &my_struct, sizeof(my_struct), 0); 

and accept the data

 recv (sock,&my_struct, sizeof(my_struct), 0); 

but, here it is full of reefs.

  • I tried to pass. Actually, no field was transmitted correctly. What are some ways to circumvent these stones? - carapuz
  • Do you have different structures? The structure is a memory area, the fields in it in a row (well, maybe with a shift to alignment), the order and type are important, not the name. In general, it is better to describe the structure in one .h and include it (.h) in the client and server. More computers db. with the same bit depth and byte order. You give an example of what does not work. After send / recv print the number of bytes transferred. And most likely you have some kind of error in the application protocol of the exchange (transmission and reception are mismatched). - avp 2:29 pm
  • @carapuz A description of the structure can be viewed? Although in any case, "no field" - the result is fantastic. Maybe your dog was buried elsewhere. - alexlz

You can, as suggested by @avp . But for greater compatibility and reliability, it is better to transfer successively the values ​​of the fields, while fixing their size.

Better yet, transfer it in a completely platform-independent form, like XML or JSON formats.

  • Just sort it out ... (especially XML). It is better to transmit in symbolic form field values ​​in strings. sprintf (), fputs () / fgets (), sscanf (); In the same place (in specially formatted lines) it is possible to drive information on synchronization, control, etc. - avp pm