OpenGL + Socket!!! Flush socket problem!!!

KingdomHeart
Unregistered
 
Post: #1
I wrote a battleship game in OpenGL with socket.
The client code communicate with server like this
turn = 1;
Code:
void myMouse(){
        ...
        if(turn){
            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

Code:
void myIdle(){
    if(!turn){
      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;
      if(board2[localrow][localcol]){
         point--;
         if(point>0){
            printf("Idle: hit\n");
            sendline[0] = 'h';
            sendline[1] = '\0';
         }else if(point == 0){
            sendline[0] = 'w';
            sendline[1] = '\0';
         }
      }else{
         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
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
I can't understand exactly what your problem, it seems like you should have some stages like Playing, Waiting for counter attack etc, in which the networking is dealt with different. You could try to read and understand this in case it helps http://fax.twilightcoders.net/BATOL%20SHIPZ/ .

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
KingdomHeart
Unregistered
 
Post: #3
well, my problem is that I dont know a way to set up my client and server so that no clients and send it attack twice. Is there an algorithm to solve my problems
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
Moved to more appropriate forum.

Sounds like you need a bit of logic to prevent the client from queuing multiple attack messages. Presumably, this would be done by preventing additional attacks until the remote server sends the client an acknowledgment that the attack message has been processed, and that it's the client's turn to attack again. Having the server verify that it's receiving sane data from the client (a single well-formed attack message) would also be highly advisable.
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #5
Maybe put an id on your messages. This way you could send as many of the same message as you need and the server will only process the first one it receives and ignores the rest. This id will change for each unique message you send to the server. I'm guessing UDP is being used.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Socket Library on OSX? MacFiend 16 12,057 Apr 15, 2010 05:44 AM
Last Post: Kirienko
  How to check if data come in from the socket without hanging there!!! KingdomHeart 2 5,714 Nov 23, 2007 09:25 AM
Last Post: KittyMac
  BSD Socket: Send Data but never Receive kodex 2 7,926 Jul 1, 2005 10:56 PM
Last Post: kodex
  Cocoa Socket Control? SummerLand 4 5,430 Dec 1, 2002 01:52 AM
Last Post: Gastropod