我已设法通过套接字发送c结构。
但是,我读过有两种方法可以做到这一点:
通过动态分配内存并填充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;
并在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);
并使用此功能发送:
send_struct(&sfd,ieq);
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);
}
}
在接收方:
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);
}
由于我一直在通过反复试验来实施, 我不确定我在这里做了什么。
根据我的代码中哪一个是顶级的?它是以链接二进制格式发送的吗?
有人可以详细说明我的代码发生了什么吗?
答案 0 :(得分:0)
不需要动态分配结构指针(在send_struct()之前完成) 临时结构也可以正常工作。 (send_struct(& sfd,& obj)其中struct EnQuery obj;)
套接字数据通信基本上是字节数组 如果在内存中看到结构存储,则它也是内存中的字节数组。
因此,动态分配或临时实例只会在存储结构数据的内存区域中有所不同(第一种情况下是堆,第二种情况下是堆栈)。