I want to send an array of characters via a tcp socket to unix.
My first idea was to use a regular char array for the structure to be sent over the socket:
typedef struct __attribute__((packed)) { uint8_t type; uint8_t id_index; char char_value[STR_MSG_MAX]; } a_msg;
Just because C char is always 8 bits. However, after some googling, I found that even if char is always 8 bits longer, the underlying representation may actually be a 32-bit integer. So I feel that char may not be the best way to represent a string in a message that will be sent over a socket from FreeBSd to Linux (or type some other unixes if you want =) ...).
stdint.h is present in all modern unixes for the day (I hope), and my thoughts are that perhaps an uint8_t or int8_t array could do the trick.
typedef struct __attribute__((packed)) { uint8_t type; uint8_t id_index; uint8_t char_value[STR_MSG_MAX]; } a_msg;
or
typedef struct __attribute__((packed)) { uint8_t type; uint8_t id_index; int8_t char_value[STR_MSG_MAX]; } a_msg;
However, uint8_t is an unsigned char, and int8_t is a signed char. The C char standard is neither the implementation of undefined, as I understand it.
My questions: What is the best way to represent a character array (string) in C that will be sent via tcp / ip in a platform independent nix (Linux, FreeBSD, etc.).
source share