convert byte array to structure in c -


i developing client-server application in c.

i want send structure client character array , convert character array structure @ server side.

i have following structures

typedef struct mail{       char cc[30];        char bcc[30];       char body[30];   }  typedef struct msg_s{        int msgid;       mail mail_l;     } 

i want send msg1 client.

unsigned char data[100];    struct msg_s msg1 ;   msg1.msgid=20;   // suppose data in mail structure filled.     data = (unsigned char*)malloc(sizeof(msg1));   memcpy(data, &msg1, sizeof(msg1));   write(socketfd , data , sizeof(data));  

when data @ server side, how convert structure?

i want same in both c , java language.

if possible, please suggest me article read this, , name of concept if missing.

i see many errors,

first, character array declaration inside struct mail doesn't following c convention

it should like,

typedef struct mail{       char cc[30];        char bcc[30];       char body[30];   } 

2nd

change struct msg_s = msg1 ; struct msg_s msg1 ; // declaration of struct msg1

3rd

unsigned char data[100]; allocates 100 bytes memory in static memory.

and allocating dynamic memory [heap] of size struct msg1 again data = (unsigned char*)malloc(sizeof(msg1)); .

change unsigned char data[100]; unsigned char *data;

in client side,

struct msg_s *inmsg ;   inmsg = malloc(sizeof(struct msg_s));  // malloc in c doesn't require typecasting memcpy(inmsg ,data, sizeof(struct msg_s));   

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -