I'm writing a pretty trivial game server, and am having some trouble with my sockets and the dataflow therein. I have in my server the following code:
Code:
for(i=0;;i= (i+1)%2){
send(p[i], board, boardsize, 0);
recv(p[i], move, movesize, 0);
if (!validate(move)){
i--;
continue;
}
if (!apply(move))
break;
if (victory())
break;
send(p[i], board, boardsize, 0);
}
Where "p" is an int[2] holding my socket fds, "board" is a global char[5] holding a null-terminated board description, "move" is a char[3] holding a null terminated move description.
validate(move) returns a boolean (read: int) on the validity of the move.
apply(move) applies the move, returning one on success and zero on failure or resignation.
victory() checks for the victory condition on the board.
I'll include the pertinent client loop below, but I'm pretty sure the problem is in my server. When I run it on my little 32 bit system, it seems to run just fine, but when I port it to the 64bit systems it's actually going to be run on, the server has the very, VERY strange habit of passing back the board to the player unedited, then cycling around on the loop and passing the correct board to the other player. I, for the life me, can't figure out why. Any ideas?
In the client:
Code:
while(1){
for(i=0;i<5;i++)
oldboard[i]=newboard[i];
while(1){
printf("Your move? ");
fgets(buf, bufLen, stdin);
buf[strlen(buf)-1]=0;
if (strlen(buf)==3 && buf[1]==' '){
move[0]=buf[0]-'0';
move[1]=buf[2]-'0';
if (valid(move, newboard)) break;
}
printf("\n\"%s\" is not a valid move.\n\n", buf);
}
send(sock, move, 3, 0);
if (boardsize>recv(sock, newboard, boardsize, 0))fail("Server down.\nTry again later");
check(newboard);
if (eq(oldboard, newboard)){
pboard(newboard);
printf("Server Rejected Move\n");
continue;
}
pboard(newboard);
printf("Waiting on %s..\n", opphandle);
if (1>recv(sock, newboard, boardsize, 0))fail("Server down.\nTry again later");
check(newboard);
printf("After %s's move:\n", opphandle);
pboard(newboard);
}
where oldboard and newboard are local char[5] holding null-terminated board descriptions, move is as before.
eq(oldboard, newboard) checks to see if the boards are the same.
pboard(board) prints the board.
check(board) checks to see if the board secretly contains a state message concerning the program (Win conditions, Lose conditions, Server Errors, etc.)
The client has already received a starting board before starting the loop.