[Tux4kids-commits] r1061 - tuxmath/branches/lan/server
David Bruce
dbruce-guest at alioth.debian.org
Wed Jun 17 03:24:21 UTC 2009
Author: dbruce-guest
Date: 2009-06-17 03:24:20 +0000 (Wed, 17 Jun 2009)
New Revision: 1061
Modified:
tuxmath/branches/lan/server/Makefile.am
tuxmath/branches/lan/server/mathcards.h
tuxmath/branches/lan/server/server.c
tuxmath/branches/lan/server/server.h
tuxmath/branches/lan/server/testclient.c
Log:
fix segfault in buf to flashcard conversion
Modified: tuxmath/branches/lan/server/Makefile.am
===================================================================
--- tuxmath/branches/lan/server/Makefile.am 2009-06-16 22:06:32 UTC (rev 1060)
+++ tuxmath/branches/lan/server/Makefile.am 2009-06-17 03:24:20 UTC (rev 1061)
@@ -6,5 +6,5 @@
tuxmathserver_SOURCES = server.c mathcards.c
tuxmathtestclient_SOURCES = testclient.c
-EXTRA_DIST = mathcards.h
+EXTRA_DIST = mathcards.h server.h
Modified: tuxmath/branches/lan/server/mathcards.h
===================================================================
--- tuxmath/branches/lan/server/mathcards.h 2009-06-16 22:06:32 UTC (rev 1060)
+++ tuxmath/branches/lan/server/mathcards.h 2009-06-17 03:24:20 UTC (rev 1061)
@@ -15,7 +15,7 @@
#ifndef MATHCARDS_H
#define MATHCARDS_H
-#define MC_DEBUG
+//#define MC_DEBUG
#ifdef MC_DEBUG
#define mcdprintf(...) printf(__VA_ARGS__)
#else
Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c 2009-06-16 22:06:32 UTC (rev 1060)
+++ tuxmath/branches/lan/server/server.c 2009-06-17 03:24:20 UTC (rev 1061)
@@ -35,9 +35,17 @@
int network_function = -1;
// size_t length;
MC_FlashCard flash;
- static int initialize=0;
+ static int initialize = 0;
-
+ printf("Started tuxmathserver, waiting for client to connect:\n");
+
+ if (!MC_Initialize())
+ {
+ fprintf(stderr, "Could not initialize MathCards\n");
+ exit(EXIT_FAILURE);
+ }
+
+
if (SDLNet_Init() < 0)
{
fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
@@ -72,7 +80,7 @@
/* Get the remote address */
if ((remoteIP = SDLNet_TCP_GetPeerAddress(csd)))
/* Print the address, converting in the host format */
- printf("Host connected: %x %d\n",
+ printf("Host connected: IP = %x, Port = %d\n",
SDLNet_Read32(&remoteIP->host),
SDLNet_Read16(&remoteIP->port));
else
@@ -90,7 +98,7 @@
if(strcmp(buffer,"a") == 0)
{
initialize=1;
- network_function = SETUP_QUESTION_LIST;
+ network_function = NEW_GAME;
}
//'b' for asking for a question(flashcard)
@@ -118,7 +126,7 @@
switch(network_function)
{
- case SETUP_QUESTION_LIST: //mainly to setup the question list
+ case NEW_GAME: //mainly to setup the question list
{
if (!MC_StartGame())
{
@@ -160,12 +168,14 @@
}
}
-
+
/* Close the client socket */
SDLNet_TCP_Close(csd);
}
}
-
+ /* Clean up mathcards heap memory */
+ MC_EndGame();
+
SDLNet_TCP_Close(sd);
SDLNet_Quit();
@@ -191,26 +201,9 @@
x = SDLNet_TCP_Send(csd, buf, sizeof(buf));
printf("SendQuestion() - buf sent:::: %d bytes\n", x);
-
- //old code:
-/* x = SDLNet_TCP_Send(csd, &(flash.question_id), sizeof(flash.question_id));
- printf("no:(1):::QUESTION_ID::::Sent %d bytes\n", x);
-
- x = SDLNet_TCP_Send(csd,&(flash.difficulty),sizeof(flash.difficulty));
- printf("no:(2):::DIFFICULTY::::Sent %d bytes\n", x);
-
- x = SDLNet_TCP_Send(csd, &(flash.answer), sizeof(flash.answer));
- printf("no:(3)::::ANSWER:::Sent %d bytes\n",x);
-
- x = SDLNet_TCP_Send(csd, flash.answer_string, strlen(flash.answer_string) + 1);
- printf("no:(4):::ANSWER_STRING::::Sent %d bytes\n", x);
-
- x = SDLNet_TCP_Send(csd, flash.formula_string, strlen(flash.formula_string) + 1);
- printf("no:(5):::FORMULA_STRING::::Sent %d bytes\n", x);
-*/
+ if (x == 0)
+ return 0;
return 1;
-
-
}
@@ -224,10 +217,10 @@
switch(message)
{
- case NO_QUESTION_LIST:
+ case NO_QUESTION_LIST:
{
- msg="Please! first setup the question list by typing <a>";
- len=strlen(msg)+1; // add one for the terminating NULL
+ msg = "Please! first setup the question list by typing <a>";
+ len = strlen(msg) + 1; // add one for the terminating NULL
snprintf(buf, NET_BUF_LEN,
"%s\t%s\n",
"SEND_MESSAGE",
Modified: tuxmath/branches/lan/server/server.h
===================================================================
--- tuxmath/branches/lan/server/server.h 2009-06-16 22:06:32 UTC (rev 1060)
+++ tuxmath/branches/lan/server/server.h 2009-06-17 03:24:20 UTC (rev 1061)
@@ -18,7 +18,7 @@
enum {
- SETUP_QUESTION_LIST,
+ NEW_GAME,
SEND_A_QUESTION
};
Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c 2009-06-16 22:06:32 UTC (rev 1060)
+++ tuxmath/branches/lan/server/testclient.c 2009-06-17 03:24:20 UTC (rev 1061)
@@ -29,12 +29,14 @@
SDLNet_SocketSet set;
MC_FlashCard flash;
-MC_FlashCard* Make_Flashcard(char *buf);
+int Make_Flashcard(char *buf, MC_FlashCard* fc);
+
int main(int argc, char **argv)
{
- MC_FlashCard* fclist;
+// MC_FlashCard* fclist;
IPaddress ip; /* Server address */
int quit, len, sockets_used;
+ int numready;
char buffer[512]; // for command-line input
char buf[512]; // for network messages from server
int x, i = 0;
@@ -100,46 +102,52 @@
}
//Now we check for any responses from server:
- //FIXME have to make this not block so we can keep checking until all the messages are gone
- //do
+ //NOTE keep looping until SDLNet_CheckSockets() detects no activity.
+ numready = 1;
+ while(numready > 0 && !quit)
{
char command[NET_BUF_LEN];
int i = 0;
- int numready;
//This is supposed to check to see if there is activity and time out
// after 1000 ms if no activity
- numready = SDLNet_CheckSockets(set, 1000);
+ numready = SDLNet_CheckSockets(set, 10);
if(numready == -1)
{
printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
//most of the time this is a system error, where perror might help you.
perror("SDLNet_CheckSockets");
}
- else if(numready)
+ else
{
- printf("There are %d sockets with activity!\n",numready);
+ printf("There are %d sockets with activity!\n", numready);
// check all sockets with SDLNet_SocketReady and handle the active ones.
if(SDLNet_SocketReady(sd))
{
+ buf[0] = '\0';
x = SDLNet_TCP_Recv(sd, buf, sizeof(buf));
/* Copy the command name out of the tab-delimited buffer: */
- for (i = 0; buf[i] != '\t' && i < NET_BUF_LEN; i++)
+ for (i = 0;
+ buf[i] != '\0' && buf[i] != '\t' && i < NET_BUF_LEN;
+ i++)
+ {
command[i] = buf[i];
+ }
+
command[i] = '\0';
+
printf("buf is %s\n", buf);
printf("command is %s\n", command);
/* Now we process the buffer according to the command: */
if(strcmp(command, "SEND_QUESTION") == 0)
{
-
- fclist=Make_Flashcard(buf); /* function call to parse buffer into MC_FlashCard */
+ Make_Flashcard(buf, &flash); /* function call to parse buffer into MC_FlashCard */
}
}
}
-
- }// while (x > 0);
+ }
+ printf("No active sockets within timeout interval\n");
}
@@ -150,10 +158,9 @@
}
-MC_FlashCard* Make_Flashcard(char *buf)
+int Make_Flashcard(char* buf, MC_FlashCard* fc)
{
- MC_FlashCard *fc;
- int i,j,tab=0,s=0;
+ int i, j, tab = 0, s = 0;
char formula[MC_FORMULA_LEN];
sscanf (buf,"%*s %d %d %d %s",
&fc->question_id,
@@ -167,20 +174,20 @@
if(*buf=='\t')
tab++;
buf++;
- if(tab==5)
+ if(tab == 5)
break;
}
- while(*buf!='\n')
- {
+ while((*buf!='\n')
+ && (s < MC_FORMULA_LEN - 1)) //Must leave room for terminating null
+ {
formula[s]=*buf;
buf++;
s++;
- }
-
+ }
+
formula[s]='\0';
- strcpy(fc->formula_string,formula);
-
+ strcpy(fc->formula_string, formula);
printf ("card is:\n");
printf("QUESTION_ID : %d\n",fc->question_id);
@@ -189,7 +196,7 @@
printf("ANSWER : %d\n",fc->answer);
printf("DIFFICULTY : %d\n",fc->difficulty);
- return fc;
+ return 1;
}
More information about the Tux4kids-commits
mailing list