I am trying to send a structure in C without using serialization
// client
typedef struct student_rec {
char name[25];
float gpa;
int pid;
} stu;
stu stu = { "Ray T Champion" , 4.0 , 12345666};
sendto(sk,&stu,sizeof(struct student_rec),0,&remote,sizeof(remote));
//server
typedef struct student_rec {
char name[25];
float gpa;
int pid;
} stu;
stu s ;
struct student_rec *ptr;
ptr = &s;
recvfrom(sk,&s,sizeof(struct student_rec),0,&remote,&rlen);
printf("%s\n", ptr->name);
printf( "%d\n", ptr->pid );
I recieve the name just fine , but the pid is not correct, I get garbage values, I am not concerend about endianess, I would just like to be able to send the struct in one shot.
You face an endianess issue.
You send 12345666
which is the same as 0x00BC6142
and you receive 1113701376
which equal to 0x4261BC00
.
Before sending convert to network byte order by
stu.pid = htonl(stu.pid);
After receiving convert (back) to host byte order.
stu.pid = ntohl(stu.pid);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.