Am I creating a temporary c struct and sending it through socket? -


i have managed send c struct on socket.

but, have read there 2 ways of doing this:

  1. by dynamically allocation memory , filling struct elements returning pointer structure

  2. create temporary struct , filling struct members returning content of struct.

my code here:

i have struct:

typedef struct enquery {     char type[7];     char table_name[53];     char columns[5][53];     struct values {         char doc[129];         char key[2][206];         char iv[17];         char td[53];         char s[206];     }values;     struct encondition {         int k;         char attr[53];         char val[53];     }encondition;     int len;     int  rn; }enquery; 

and declared in main():

struct enquery * ieq; ieq = (enquery *)malloc(sizeof(enquery)); strcpy(ieq->type,"insert"); strcpy(ieq->table_name,"table_name"); strcpy(ieq->columns[0],"hello"); strcpy(ieq->columns[1],"hi"); strcpy(ieq->columns[2],"an"); strcpy(ieq->columns[3],"nyung"); strcpy(ieq->columns[4],"haseyo"); strcpy(ieq->values.doc,"doc"); strcpy(ieq->values.key[0],"key1"); strcpy(ieq->values.key[1],"key2"); strcpy(ieq->values.iv,"iv"); strcpy(ieq->values.td,"td"); strcpy(ieq->values.s,"s"); ieq->encondition.k = htonl(1); strcpy(ieq->encondition.attr,"attr"); strcpy(ieq->encondition.val,"val"); ieq->len = htons(1); ieq->rn = htons(2); 

and uses function send:

send_struct(&sfd,ieq); 

the content of send_struct():

send_struct(int * sfd, struct enquery *ieq) {     int n;     char buff[256];      if(n = read(*sfd,buff,256) > 0) {         struct enquery eq;         eq = *ieq;         printf("eq.type = %s\n",eq.type);         printf("eq.table_name = %s\n",eq.table_name);         printf("eq.columns = {%s,%s,%s,%s,%s}\n",eq.columns[0],eq.columns[1],eq.columns[2],eq.columns[3],eq.columns[4]);         printf("eq.values.doc = %s\n",eq.values.doc);         printf("eq.values.key = {%s,%s}\n",eq.values.key[0],eq.values.key[1]);         printf("eq.values.iv = %s\n",eq.values.iv);         printf("eq.values.td = %s\n",eq.values.td);         printf("eq.values.s = %s\n",eq.values.s);         printf("eq.encondition.k = %d\n",ntohs(eq.encondition.k));         printf("eq.encondition.attr = %s\n",eq.encondition.attr);         printf("eq.encondition.val = %s\n",eq.encondition.val);         printf("eq.len = %d\n",ntohs(eq.len));         printf("eq.rn = %d\n",ntohs(eq.rn));         send(*sfd,&eq,sizeof(struct enquery),0);     } } 

on receiving side:

recv_struct(int sfd){     struct enquery eq;     file* instream = fdopen(sfd,"r");     if(fread(&eq,sizeof(struct enquery),1,instream) != 1) {         error("fread(1)");     }     printf("eq.type = %s\n",eq.type);     printf("eq.table_name = %s\n",eq.table_name);     printf("eq.columns = {%s,%s,%s,%s,%s}\n",eq.columns[0],eq.columns[1],eq.columns[2],eq.columns[3],eq.columns[4]);     printf("eq.values.doc = %s\n",eq.values.doc);     printf("eq.values.key = {%s,%s}\n",eq.values.key[0],eq.values.key[1]);     printf("eq.values.iv = %s\n",eq.values.iv);     printf("eq.values.td = %s\n",eq.values.td);     printf("eq.values.s = %s\n",eq.values.s);     eq.encondition.k = ntohs(eq.encondition.k);     printf("eq.encondition.k = %d\n",eq.encondition.k);     printf("eq.encondition.attr = %s\n",eq.encondition.attr);     printf("eq.encondition.val = %s\n",eq.encondition.val);     eq.len = ntohs(eq.len);     printf("eq.len = %d\n",eq.len);     eq.rn = ntohs(eq.rn);     printf("eq.rn = %d\n",eq.rn); } 

since have been implementing trial , error, not sure exact doing here.

which of top code according to? sending linked binary format?

can elaborate happening code?

dynamically allocating struct pointer not required (as being done before send_struct()).
temporary structure work fine. (send_struct(&sfd, &obj) struct enquery obj;)

socket data communication array of bytes.
if see structure storage in memory, array of bytes in memory.

hence, dynamically allocating or temporary instance differ in memory area structure data stored (heap in first case, stack in second case).


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -