00001
00002
00003 #include <unistd.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <strings.h>
00008 #include <sys/socket.h>
00009 #include <sys/types.h>
00010 #include <netinet/in.h>
00011 #include <time.h>
00012 #include <signal.h>
00013 #include <poll.h>
00014 #include <pthread.h>
00015 #include <mysql/mysql.h>
00016
00017 #define BUFFER_SIZE 4096
00018
00019 #define SERVER_PORT 8000
00020 #define NUM_STUDENTS 1000
00021
00022
00023 #define LEN 1000
00024 int NUM_QS;
00025 int TOTAL_QS;
00026 int LISTEN_PORT;
00027 MYSQL *connection1;
00028 MYSQL *connection2;
00029 MYSQL_RES *result1;
00030 MYSQL_RES *result2;
00031 MYSQL_ROW sqlrow;
00032
00033 int listen_file_descriptor;
00034 int connection_file_descriptor[NUM_STUDENTS];
00035
00043 void display_row (char ques[])
00044 {
00045 unsigned int field_count;
00046 field_count = 1;
00047 while (field_count < mysql_field_count (connection1)-1)
00048 {
00049 if(field_count==mysql_field_count(connection1)-2)
00050 {
00051 strcat(ques,sqlrow[field_count]);
00052
00053 }
00054 else
00055 {
00056 strcat(ques,sqlrow[field_count]);
00057 strcat(ques,"|");
00058
00059 }
00060 field_count++;
00061 }
00062 strcat(ques,"#");
00063 strcat(ques,sqlrow[field_count]);
00064 printf ("\n");
00065 }
00066
00075 void search(int quesno,char ques[],int level)
00076 {
00077 char query[LEN];
00078 int return_value;
00079 strcpy(ques,"");
00080
00081
00082 sprintf(query,"select * from exam%d where quesno=%d",level,quesno);
00083
00084 return_value = mysql_query (connection1, query);
00085 if (return_value)
00086 {
00087 printf ("select failed as : %s\n", mysql_error (connection1));
00088 }
00089 else
00090 {
00091 result1 = mysql_use_result (connection1);
00092 if (result1)
00093 {
00094 if ((sqlrow = mysql_fetch_row (result1)))
00095 display_row (ques);
00096
00097
00098 if (mysql_errno (connection1))
00099 {
00100 printf ("Error occurred while retrieving data : %s\n", mysql_error (connection1));
00101 }
00102 }
00103 mysql_free_result(result1);
00104 }
00105
00106
00107 }
00108
00116 int find(int level)
00117 {
00118 if(level==1)
00119 return 4;
00120 else if(level==2)
00121 return 6;
00122 else if(level==3)
00123 return 8;
00124 else if(level==4)
00125 return 10;
00126 }
00127
00137 int found(int levels[],int qno[],int numqs,int curlevel)
00138 {
00139 int ret,i;
00140 int flag=0;
00141 srand(time(NULL));
00142 while(1)
00143 {
00144 flag=0;
00145 ret=rand()%TOTAL_QS+1;
00146 for(i=0;i<numqs;i++)
00147 {
00148 if((levels[i]==curlevel)&&qno[i]==ret)
00149 {
00150 flag=1;
00151 break;
00152 }
00153 }
00154 if(flag==0)
00155 {
00156 break;
00157 }
00158 }
00159 return ret;
00160 }
00161
00168 void *sendques(void *arg)
00169 {
00170 struct pollfd pollfd1[1];
00171 char input_data[LEN];
00172 int characters_read;
00173 int characters_written;
00174 int client;
00175 int i;
00176 int totalmarks;
00177 int levels[1000];
00178 int questions[1000];
00179 char *split;
00180 int quesno;
00181 client=(int)arg;
00182 int ans,marks=0;
00183 int level=4;
00184 totalmarks=NUM_QS*10;
00185 printf("client no: %d\n",client);
00186
00187
00188 for(i=0;i<NUM_QS;i++)
00189 {
00190 strcpy(input_data,"");
00191
00192
00193 quesno=found(levels,questions,i,level);
00194 levels[i]=level;
00195 questions[i]=quesno;
00196
00197 search(quesno,input_data,level);
00198 split=strtok(input_data,"#");
00199 printf("Question is %s\n",split);
00200
00201 write(connection_file_descriptor[client],split,strlen(split));
00202 split=strtok(NULL,"#");
00203 ans=atoi(split);
00204 printf("ANSWER is %d\n",ans);
00205 characters_read=read(connection_file_descriptor[client],input_data,BUFFER_SIZE-1);
00206 input_data[1]='\0';
00207 if(strcmp(split,input_data)==0)
00208 {
00209 marks=marks+find(level);
00210 if(level!=4)
00211 level++;
00212 printf("ANSWER is correct\n");
00213 }
00214 else
00215 {
00216 if(level!=1)
00217 level--;
00218 printf("ANSWER is wrong\n");
00219 }
00220
00221
00222 }
00223 sprintf(input_data,"end %d %d",marks,totalmarks);
00224 write(connection_file_descriptor[client],input_data,strlen(input_data));
00225 pthread_exit(NULL);
00226 return NULL;
00227 }
00228
00234 void close_properly (int signal)
00235 {
00236 int return_value;
00237 int i;
00238 printf ("Shutting down...\n");
00239 return_value = close (listen_file_descriptor);
00240 if (return_value < 0)
00241 {
00242 perror ("Cannot close listening socket 1.");
00243 exit (EXIT_FAILURE);
00244 }
00245 printf ("Shutdown complete.\n");
00246 exit (0);
00247 }
00248
00249
00261 int main (int argc, char *argv[])
00262 {
00263 struct sockaddr_in server_address;
00264 int return_value;
00265 struct sigaction act1;
00266 pthread_t child[NUM_STUDENTS];
00267 void *child_return_value;
00268 int i;
00269 int result;
00270 char url[LEN],username[LEN],password[LEN],databasename[LEN];
00271 connection1 = mysql_init(NULL);
00272 strcpy(url,"localhost");
00273
00274 printf("Enter the database username:\n");
00275 scanf("%s",username);
00276
00277 printf("Enter the database password:\n");
00278 scanf("%s",password);
00279
00280 printf("Enter the database name:\n");
00281 scanf("%s",databasename);
00282
00283 printf("Enter the number of questions in each level of questions:\n");
00284 scanf("%d",&TOTAL_QS);
00285 printf("Enter the number of questions in the exam:\n");
00286 scanf("%d",&NUM_QS);
00287 LISTEN_PORT=SERVER_PORT;
00288
00289
00290
00291
00292 if (mysql_real_connect (connection1, url, username, password,databasename, 0, NULL, 0))
00293 {
00294 printf ("Database Connection successful\n");
00295 }
00296 else
00297 {
00298 fprintf (stderr, "Database Connection failed\n");
00299 if (mysql_errno (connection1))
00300 {
00301 fprintf (stderr, "Database Connection error %d: %s\n",
00302 mysql_errno (connection1),
00303 mysql_error (connection1));
00304 }
00305 }
00306
00307
00308 act1.sa_handler = close_properly;
00309 sigemptyset (&act1.sa_mask);
00310 act1.sa_flags = 0;
00311 sigaction (SIGINT, &act1, 0);
00312
00313
00314
00315
00316 listen_file_descriptor = socket (AF_INET, SOCK_STREAM, 0);
00317 if (listen_file_descriptor < 0)
00318 {
00319 fprintf (stderr, "%s: cannot open socket.\n", argv[0]);
00320 exit (EXIT_FAILURE);
00321 }
00322 bzero (&server_address, sizeof (server_address));
00323 server_address.sin_family = AF_INET;
00324 server_address.sin_addr.s_addr = htonl (INADDR_ANY);
00325 server_address.sin_port = htons (LISTEN_PORT);
00326 return_value =bind (listen_file_descriptor, (struct sockaddr *) &server_address,sizeof (server_address));
00327 if (return_value < 0)
00328 {
00329 perror ("Cannot bind");
00330 exit (EXIT_FAILURE);
00331 }
00332 return_value = listen (listen_file_descriptor, 5);
00333 if (return_value < 0)
00334 {
00335 perror ("Cannot listen");
00336 exit (EXIT_FAILURE);
00337 }
00338
00339 while (1)
00340 {
00341
00342 for(i=0;i<NUM_STUDENTS;i++)
00343 {
00344 connection_file_descriptor[i] = accept (listen_file_descriptor, (struct sockaddr *) NULL, NULL);
00345 if (connection_file_descriptor[i] < 0)
00346 {
00347 perror ("accept() failed.");
00348 exit (EXIT_FAILURE);
00349 }
00350 else
00351 {
00352 return_value = pthread_create(&child[i], NULL, sendques, (void*)i);
00353 if(return_value !=0)
00354 {
00355 perror("Thread 1 creation failed");
00356 exit(EXIT_FAILURE);
00357 }
00358 printf ("Got client for connection %d",i);
00359 }
00360 }
00361
00362
00363
00364
00365 for(i=0;i<NUM_STUDENTS;i++)
00366 {
00367 return_value = close (connection_file_descriptor[i]);
00368 if (return_value < 0)
00369 {
00370 perror ("close failed");
00371 exit (EXIT_FAILURE);
00372 }
00373 }
00374 }
00375 return 0;
00376 }