Zoltorn
Zoltorn

Reputation: 171

Sending a struct via send command

I am trying to send a struct via send from a client to a server. The struct looks like this:

typedef struct{
    int age;
    long int id;
    char firstName[10];
    char lastName[10];
    uint32_t somefield;
} Person;

Now, when I fill out the fields in my client code:

Person person;
person.age = 52;
person.id = 1234567;
strcpy(person.firstName, "Walter");
strcpy(person.lastName, "White");
inet_pton(AF_INET, "127.0.0.1", &person.somefield);
person.somefield = htonl(person.somefield);

And print it out I get the correct output

I then call send to pass the struct to the server

send(socketfd, &person, sizeof(&person), 0);

On the server's side of things, it reads in a person using recv:

 Person person;
 recv(newSocketfd, &person, sizeof(Person), 0);

And printing out the fields, it doesn't give me the output I'm expecting...

I have done research and it appears that as long as I'm not passing a pointer through the struct I should be able to do this. Am I incorrect about this? Any help would be appreciated.

Upvotes: 0

Views: 66

Answers (1)

John3136
John3136

Reputation: 29266

sizeof(&person) (in the send) is the size of a the address of a Person. Not the size of a Person struct. Try sizeof(Person).

Upvotes: 1

Related Questions