3

I have a C structures in two different machines - server and clients. For example:

struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

My question is what are the possible ways to replicate the data between the machines? Maybe I can try to convert the data in XML and copy it? Or I can use arrays?

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61
user1285928
  • 1,328
  • 29
  • 98
  • 147
  • 1
    You need to convert the data into a chunk of bytes, transport the bytes to the other machine, and then decode those bytes. You can use text, XML, or any other format you like, so long as you can define it at the byte level. – David Schwartz Jun 16 '12 at 22:55
  • 1
    See [this](http://stackoverflow.com/questions/371371/serialize-data-structures-in-c) for a list of C serialization libraries. You can probably use one of them. – dirkgently Jun 16 '12 at 22:59
  • 1
    @blackbear Seems fine for the strings, but possibly creates problems with byte order / word size for the int and float – gcbenison Jun 16 '12 at 23:03

2 Answers2

1

The simplest, most lightweight solution - if you have a connection open between the two machines in the form of a FILE* - may be to transmit with fprintf on one end, and decode with fscanf on the other.

protocol.h:

typedef struct _packet packet_t;

struct _packet {
  int account_number;
  char *first_name;
  char *last_name;
  float balance;
};

static const char packet_fmt[]=" acct: %d fname: %s sname: %s balance: %f";

sender:

  ...
  printf(packet_fmt, acct, "Greg", "Benison", bal);
  ...

listener:

...
int n_read = fscanf(fin, packet_fmt, &acct, fname, sname, &balance);

if (n_read == 4)
  printf("Received balance update for %s %s: %f\n", fname, sname, balance);
...

That may suffice for your four-field struct, but if you anticipate its structure changing or growing significantly it may make sense to pursue XML, JSON, or some other more formal encoding.

gcbenison
  • 11,723
  • 4
  • 44
  • 82
  • Thank you for your answer. Can I for example convert the entire struct into array and sent it via the network to the other machine? Then the array will be translated into struct again. – user1285928 Jun 16 '12 at 23:17
  • 1
    That's what this code does. The `printf` converts the structure into an array of characters and the `fscanf` converts it back. – David Schwartz Jun 19 '12 at 00:29
1

If you are familiar with C then I would use TCP sockets to transmit and receive your data. You will need to basically transmit your data though as raw bytes and in a specified order so you can decode it when received. You will need to allow for the endeanness of the machines and also you will need to send additional fields of data to specify the lengths of your variable length names (so that you know how many bytes to receive). Usually it is a good idea to add a field that indicates the total size of the data packet you are sending. Basically you can encode your data packet in anyway you choose and provided you unpack it in the same way at the other end your data will be transferred. Maybe this is an old hat way of doing things but I use this approach all the time in my day job to transfer data between machines. It is one of a number of options available to you. You could send data as xml of in other formats but sending raw bytes results in smaller packets (which may or may not be an issue you need to address). Hope this helps.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
  • I'm new to C programming. Do you know where I can find a tutorial about this approach with code example? If your project is open source can you share some code? – user1285928 Jun 16 '12 at 23:29
  • Hmm if you are new to C then this will involve a learning curve. I cannot share work code obviously. If you are working on linux then this is a very good place to start http://beej.us/guide/bgnet/ – mathematician1975 Jun 16 '12 at 23:32