Struct of ints via socket

Hey there, I have some paper work to do about a game I should be able to play with my class mates. We should be able to send and recive a struct concerning about the 'moves' of the pieces at the game. This struct is made of 4 ints, simple as that.
I had a previous paper work to do where I'd have to send ints and that was ok, but now that I have to send a struct I'm failing to do so. This is What I've tried:
1
2
3
4
5
6
7
8
9
//SERVER
struct message{
	int code;
	int piece;
	int x;
	int y;
}send;
bytes_sent=write(new_socket,&send,sizeof(message));
cout<<bytes_sent;


So far so good, I see 16 as bytes_sent's value.
On the other hand:
1
2
3
4
5
6
7
8
9
//CLIENT
struct message{
	int code;
	int piece;
	int x;
	int y;
}recive;
bytes_recvd=read(my_socket,&recive,sizeof(message));
cout<<bytes_recvd;

I always see 0 as the bytes_recvd's value.
Doing some research I found out I'd have to serialize my sending structure, but I failing to do so. Can anyone help me here? I kind of don't know what to do, would I really have to serialize? Does anyone have something for me to read or whatever? I'm lost.
start with the documentation:

read() http://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html#tag_16_474

If fildes refers to a socket, read() shall be equivalent to recv() with no flags set.


recv() http://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html

If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0

ok, I'm kind of into this functions already. I've made some changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//SERVER
struct message{
	int code;
	int piece;
	int x;
	int y;
}send;
my_socket=socket(AF_INET,SOCK_STREAM,0);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(50000);
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(serv_addr.sin_zero), 8);
bind(my_socket, (struct sockaddr *) &serv_addr,sizeof(struct sockaddr));
listen(my_socket,2);
clilen = sizeof(cli_addr);
new_socket=accept(my_socket,(struct sockaddr *) &cli_addr, &clilen);
bytes_sent=send(my_socket,&send,sizeof(struct message),0);
cout<<bytes_sent;


And now I'm getting this error "send: Socket operation on non-socket"
I'd like to know if CAN do this "&send", as send being a struct. Because if I can, the problem could only be on the 3rd parameter, the mesage size. Right?
Last edited on
well, all this time to find out the problem was a missing ( ... ) at the if for accept. This is a serious problem of noobism.
Topic archived. No new replies allowed.