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.