View Full Version : Opengl + socket !!! Flush socket buffer???

11-20-2007, 12:44 AM
I wrote a battleship game in OpenGL with socket.
The client code communicate with server like this
turn = 1;

void myMouse(){
printf("myMouse: Fire a missle\n");
sendline[0] = rowAttack + 48;
sendline[1] = colAttack + 48;
sendline[2] = '\0';
//Attack the opponent
Write(sockfd, sendline, MAXLINE);
//Read the result of my attack
Read(sockfd, recvline, MAXLINE);
if(recvline[0] == 'w'){
printf("myMouse: you won the game\n");
}else if(recvline[0] == 'h'){
printf("myMouse: you hit\n");
}else if(recvline[0] == 'm'){
printf("myMouse: you miss\n");
turn = 0;

then in my Idle function

void myIdle(){
printf("Idle: Read the attack\n");
//Receive the attack and process whether it's a hit or not
Read(sockfd, recvline, MAXLINE);******************************
localrow = recvline[0] - 48;
localcol = recvline[1] - 48;
printf("Idle: hit\n");
sendline[0] = 'h';
sendline[1] = '\0';
}else if(point == 0){
sendline[0] = 'w';
sendline[1] = '\0';
printf("Idle: you miss\n");
sendline[0] = 'm';
sendline[1] = '\0';
turn = 1;
Write(sockfd, sendline, MAXLINE);

And in the server code I check if the same player attack twice.
Here is the dilemma. User 1 click on the board to attack and immediately read back the result from his attack. Idle responsible for send the result back. But if user 1 try to attack twice then, strange thing start to happen. After the first attack, right now it should sit the myIdle() trying to read in something (where the stars is). If user 1 try to attack twice or three time.., seems like the program queue up his attack, and when user 2 click on the board. All the attack from user1 click got draw. So instead of sending the attack to the server, which then detect as an attack from the same client, Now it is a valid attack, because it queue up the attack and wait for the user 2 to attack. How can I fix this problem? Thank you

11-20-2007, 01:02 AM
This is an OpenGL forum. You need a networking/general gaming forum.