[Tux4kids-commits] r1102 - tuxmath/branches/lan/server

akash gangil gangil-guest at alioth.debian.org
Sat Jun 27 22:53:23 UTC 2009


Author: gangil-guest
Date: 2009-06-27 22:53:23 +0000 (Sat, 27 Jun 2009)
New Revision: 1102

Modified:
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/server.h
   tuxmath/branches/lan/server/testclient.c
   tuxmath/branches/lan/server/transtruct.h
Log:
Giving each client an identity

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-06-27 22:23:09 UTC (rev 1101)
+++ tuxmath/branches/lan/server/server.c	2009-06-27 22:53:23 UTC (rev 1102)
@@ -24,7 +24,7 @@
 #include "transtruct.h"
 #include "mathcards.h"
 
-#define LAN_DEBUG
+
 #define NUM_CLIENTS 16
 
 TCPsocket sd,csd; /* Socket descriptor */
@@ -43,7 +43,6 @@
   char buf[NET_BUF_LEN];
   int command_type = -1,numready,j;
   static int sockets_used=0;
-  static int game_started=0;
   static int i=0;
   static int num_clients=0;
   MC_FlashCard flash;
@@ -111,42 +110,17 @@
         // check all sockets with SDLNet_SocketReady and handle the active ones.
         for(j=0;j<sockets_used;j++)
         {
-#ifdef LAN_DEBUG
-  printf("inside for %d\n",quit);
-#endif
-
          if(SDLNet_SocketReady(client[j].csd)) 
          {
-#ifdef LAN_DEBUG
-  printf("inside ready\n",quit);
-#endif
-
           if (SDLNet_TCP_Recv(client[j].csd, buffer, NET_BUF_LEN) > 0)
           {
-#ifdef LAN_DEBUG
-  printf("inside recv %d         %s\n",quit,buffer);
-#endif
-#ifdef LAN_DEBUG
-  printf("inside recv %d  \n",strncmp(buffer,"start",5));
-#endif
-
-
            if(strncmp(buffer,"start",5)==0)
            {
-           quit=1;
-#ifdef LAN_DEBUG
-  printf("quit is %d\n",quit);
-#endif
-
-
+           quit=1;                                        //if any one player is ready the game stops accepting the connections , this can be modified later to suit the needs
            snprintf(buf, NET_BUF_LEN, 
                 "%s\n",
                 "Success");
            x = SDLNet_TCP_Send(client[j].csd, buf, sizeof(buf));
-#ifdef LAN_DEBUG
-  printf("buf sent:::: %d bytes\n", x);
-  printf("buf is: %s\n", buf);
-#endif
            client[j].flag=1;
            }
           }
@@ -159,7 +133,13 @@
     client[i].csd = SDLNet_TCP_Accept(sd);
     if (client[i].csd !=NULL)
     {
-       printf("this is the value of i = %d\n",i);
+    
+     if( SDLNet_TCP_Recv(client[i].csd, buffer, NET_BUF_LEN) > 0)
+     {
+      strcpy(client[i].name,buffer);
+      printf(" JOINED  :::   %s",client[i].name);
+     }
+      printf("this is the value of i = %d\n",i);
       num_clients++;
       /* Now we can communicate with the client using client[i].csd socket
       /* sd will remain opened waiting other connections */
@@ -176,22 +156,12 @@
       }
       else
         fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
-
-#ifdef LAN_DEBUG
-printf("before add socket\n");
-#endif      
       sockets_used = SDLNet_TCP_AddSocket(client_set,client[i].csd);
       if(sockets_used == -1) 
       {
         printf("SDLNet_AddSocket: %s\n", SDLNet_GetError());
         // perhaps you need to restart the set and make it bigger...
       }
-
-
-#ifdef LAN_DEBUG
-      printf("sockets used::::%d\n",sockets_used);
-#endif
-      
       i++;
     
     }//end of *if(client[i].csd = SDLNet_TCP_Accept(sd))*  
@@ -200,6 +170,8 @@
 
 num_clients=sockets_used;
 
+
+/*This loop sees that the game starts only when all the players are ready */
  for(j=0;j<num_clients;j++)
  {
   if(client[j].flag!=1)
@@ -213,21 +185,11 @@
                 "%s\n",
                 "Success");
      x = SDLNet_TCP_Send(client[j].csd, buf, sizeof(buf));
-#ifdef LAN_DEBUG
-  printf("buf sent:::: %d bytes\n", x);
-  printf("buf is: %s\n", buf);
-#endif
     }
    }
   }
  }
 
-#ifdef LAN_DEBUG
-printf("Out of the while loop.......\n",sockets_used);
-#endif
-
-
-
 /* If no players join the game */
 if(num_clients==0)
 {
@@ -239,10 +201,6 @@
  exit(1);
 }
 
-
-
-game_started=1;                 //indicating the game has started
-
 #ifdef LAN_DEBUG
 printf("We have %d players.......\n",sockets_used);
 #endif
@@ -345,7 +303,8 @@
      
            if(strncmp(command, "CORRECT_ANSWER",14) == 0)
            {
-             command_type = CORRECT_ANSWER;              
+             command_type = CORRECT_ANSWER; 
+             printf("question id %d was answered correctly by %s",id,client[j].name);             
                if (!MC_NextQuestion(&flash))
                { 
                  /* no more questions available */
@@ -384,6 +343,7 @@
 
            if(strncmp(command, "exit",4) == 0) /* Terminate this connection */
            {
+             printf("LEFT the GAME : %s",client[j].name);
              client[j].flag=0;
              SDLNet_TCP_DelSocket(client_set,client[j].csd);
              SDLNet_TCP_Close(client[j].csd);
@@ -392,6 +352,7 @@
 
            if(strncmp(command, "quit",4) == 0) /* Quit the program */
            {
+             printf("Server has been shut down by %s",client[j].name); 
              client[j].flag=0;
              SDLNet_TCP_DelSocket(client_set,client[j].csd);
              SDLNet_TCP_Close(client[j].csd);

Modified: tuxmath/branches/lan/server/server.h
===================================================================
--- tuxmath/branches/lan/server/server.h	2009-06-27 22:23:09 UTC (rev 1101)
+++ tuxmath/branches/lan/server/server.h	2009-06-27 22:53:23 UTC (rev 1102)
@@ -13,11 +13,13 @@
 
 
 #include "SDL_net.h"
+#define NAME_SIZE 50
 
 #ifndef SERVER_H
 #define SERVER_H
 typedef struct client_type {
   int flag;                 //flag=1 , if it has been alloted to a client, and 0 otherwise
+  char name[NAME_SIZE];
   TCPsocket csd;
 }client_type;
 

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-06-27 22:23:09 UTC (rev 1101)
+++ tuxmath/branches/lan/server/testclient.c	2009-06-27 22:53:23 UTC (rev 1102)
@@ -25,7 +25,6 @@
 #include "mathcards.h"
 #include "testclient.h"
 
-#define LAN_DEBUG
 
 TCPsocket sd;           /* Server socket descriptor */
 SDLNet_SocketSet set;
@@ -99,11 +98,7 @@
              "'quit' to end both client and server\n>\n"); 
     char *check;
     check=fgets(buffer,NET_BUF_LEN,stdin);
-#ifdef LAN_DEBUG
-    printf("buffer is %s",buffer);   
-    printf("%d",strncmp(buffer, "quit",4));
-#endif
-    //Figure out if we are trying to quit:
+   //Figure out if we are trying to quit:
     if(  (strncmp(buffer, "exit",4) == 0)
       || (strncmp(buffer, "quit",4) == 0))
     {
@@ -117,6 +112,23 @@
     }
     else if (strncmp(buffer, "game",4) == 0)
     {
+      char name[NAME_SIZE];
+      printf("Enter your Name.\n");
+      check=fgets(buffer,NET_BUF_LEN,stdin);
+      strncpy(name,check,strlen(check));
+      snprintf(buffer, NET_BUF_LEN, 
+                       "%s",
+                       name);
+      len = strlen(buffer) + 1;
+      if (SDLNet_TCP_Send(sd, (void *)buffer, NET_BUF_LEN) < NET_BUF_LEN)
+      {
+       fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
+       exit(EXIT_FAILURE);
+      }
+#ifdef LAN_DEBUG
+  printf("Sent the name of the player %s\n",check);
+#endif
+
       printf("Starting math game:\n");
       playgame();
       printf("Math game finished.\n");

Modified: tuxmath/branches/lan/server/transtruct.h
===================================================================
--- tuxmath/branches/lan/server/transtruct.h	2009-06-27 22:23:09 UTC (rev 1101)
+++ tuxmath/branches/lan/server/transtruct.h	2009-06-27 22:53:23 UTC (rev 1102)
@@ -17,6 +17,7 @@
 //#define LAN_DEBUG
 #define NET_BUF_LEN 512
 #define DEFAULT_PORT 4779
+#define NAME_SIZE 50
 
 #define MC_USE_NEWARC
 #define MC_FORMULA_LEN 40




More information about the Tux4kids-commits mailing list