Hello All,
I'm trying to create a distributed system using a tcp server/client model and I'm having trouble getting the clients and server to talk. The main communication protocol is to pass message structures over a connected socket but it appears the data is getting garbled after a write() function call.
I've tried converting to network byte (htonl)order before sending and converting it back (ntohl) when receiving but that doesn't appear to help.
The structure I'm passing is fairly simple, it contains 3 integers and a character array. After some debugging, I think it might be the way I copy the data upon receiving it and I'm hoping someone can help me understand what I am doing wrong.
Thanks, David
The structure looks like this:
struct {
int size;
int id;
char name[128]
} _struct
I'm sending the structure like this:
send(socket, &_struct, sizeof(_struct));
and inside my receive function, I read the data like this:
_struct inc_struct;
read(socket, &inc_struct, sizeof(_struct));
..
..
// print out the data
printf("id is %d\n", inc_struct.id);
..
..
I'm trying to create a distributed system using a tcp server/client model and I'm having trouble getting the clients and server to talk. The main communication protocol is to pass message structures over a connected socket but it appears the data is getting garbled after a write() function call.
I've tried converting to network byte (htonl)order before sending and converting it back (ntohl) when receiving but that doesn't appear to help.
The structure I'm passing is fairly simple, it contains 3 integers and a character array. After some debugging, I think it might be the way I copy the data upon receiving it and I'm hoping someone can help me understand what I am doing wrong.
Thanks, David
The structure looks like this:
struct {
int size;
int id;
char name[128]
} _struct
I'm sending the structure like this:
send(socket, &_struct, sizeof(_struct));
and inside my receive function, I read the data like this:
_struct inc_struct;
read(socket, &inc_struct, sizeof(_struct));
..
..
// print out the data
printf("id is %d\n", inc_struct.id);
..
..