00001
00008 #include <stdio.h>
00009 #include <unistd.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <strings.h>
00013 #include <sys/types.h>
00014 #include <arpa/inet.h>
00015 #include <netinet/in.h>
00016 #include <sys/socket.h>
00017 #include <time.h>
00018 #include <signal.h>
00019 #include <sqlite3.h>
00020
00021 #define BUFFER 4096
00022
00023
00024 sqlite3 *database1,*database2;
00025
00026
00027 int sfd1;
00028
00029
00033 void close_properly(int signal)
00034 {
00035 int return_value;
00036
00037 printf("Shutting down...\n");
00038
00039 return_value =close(sfd1);
00040
00041 if(return_value <0)
00042 {
00043 perror("Cannot close listening socket.");
00044 exit(1);
00045 }
00046
00047 printf("Shutdown complete.\n");
00048 exit(0);
00049 }
00050
00051
00065 int main(int argc, char *argv[])
00066 {
00067
00068
00069 if(argc != 2)
00070 {
00071 fprintf(stderr, "%s: usage: %s <port_number>\n", argv[0], argv[0]);
00072 exit(1);
00073 }
00074
00075
00076 int re1;
00077
00078
00079 struct sockaddr_in address1;
00080
00081
00082 char buff1[BUFFER];
00083
00084
00085 int cfd1;
00086
00087
00088 char datadir[100];
00089
00090
00091 char sql_statement[BUFFER];
00092
00093
00094 char **result1,**result2;
00095
00096
00097 char *zErrMsg = 0;
00098
00099
00100 char outcome[10000]="",temp[1000]="";
00101
00102
00103 int rc,trow,tcol,rrow,rcol,j;
00104
00105
00106 sfd1= socket(AF_INET, SOCK_STREAM, 0);
00107
00108
00109 if(sfd1<0)
00110 {
00111 printf("Cannot Open Socket\n");
00112 exit(1);
00113 }
00114
00115
00116 bzero(&address1,sizeof(address1));
00117
00118
00119 address1.sin_family=AF_INET;
00120
00121
00122
00123 address1.sin_addr.s_addr = htonl(INADDR_ANY);
00124
00125
00126
00127
00128
00129 address1.sin_port=htons(atoi(argv[1]));
00130
00131
00132 re1 = bind(sfd1,(struct sockaddr *) &address1, sizeof(address1));
00133
00134
00135 if(re1<0)
00136 {
00137 printf("Binding Failed\n");
00138 exit(1);
00139 }
00140
00141
00142
00143
00144 re1= listen(sfd1, 5);
00145
00146
00147
00148 if(re1 < 0)
00149 {
00150 perror("Cannot listen");
00151 exit(1);
00152 }
00153
00154
00155 cfd1= accept(sfd1,(struct sockaddr *) NULL, NULL);
00156
00157
00158 if(cfd1 < 0)
00159 {
00160 perror("accept() failed.");
00161 exit(1);
00162 }
00163
00164
00165 re1= read(cfd1,buff1, BUFFER-1);
00166 buff1[re1]='\0';
00167
00168
00169 if(re1 < 0)
00170 {
00171 perror("read() failed.\n");
00172 exit(1);
00173 }
00174
00175
00176
00177 char des[100];
00178 strcpy(des,buff1);
00179 char *token,des1[100];
00180 token=strtok(des,"/");
00181 while(token!=NULL)
00182 {
00183 strcpy(des1,token);
00184 token=strtok(NULL,"/");
00185 }
00186
00187
00188
00189 rc = sqlite3_open (buff1, &database1);
00190
00191
00192 if (rc != SQLITE_OK)
00193 {
00194 fprintf (stderr, "\nDatabase Does Not Exists : %s\n", zErrMsg);
00195 }
00196
00197
00198 re1= read(cfd1,buff1, BUFFER-1);
00199 buff1[re1]='\0';
00200
00201
00202 int cf=atoi(buff1);
00203
00204 while(1)
00205 {
00206 re1= read(cfd1,buff1, BUFFER-1);
00207 buff1[re1]='\0';
00208
00209 if(re1 < 0)
00210 {
00211 perror("read() failed.\n");
00212 exit(1);
00213 }
00214
00215 sprintf(temp,"Roll No :%s\t\t\t",buff1);
00216 strcpy(outcome,temp);
00217
00218 sprintf(sql_statement, "SELECT * from %s order by qno",buff1);
00219 rc=sqlite3_get_table(database1,sql_statement,&result1,&trow,&tcol,&zErrMsg);
00220 if (rc != SQLITE_OK)
00221 {
00222
00223 }
00224
00225 strcpy(datadir,"key");
00226
00227 rc = sqlite3_open (datadir, &database2);
00228 if (rc != SQLITE_OK)
00229 {
00230 fprintf (stderr, "\nDatabase Does Not Exists : %s\n", zErrMsg);
00231 }
00232
00233 sprintf(sql_statement, "SELECT name from %s where rno='%s'",des1,buff1);
00234 rc=sqlite3_get_table(database2,sql_statement,&result2,&rrow,&rcol,&zErrMsg);
00235 if (rc != SQLITE_OK)
00236 {
00237 fprintf (stderr, "\nSQL error: %s\n", zErrMsg);
00238 }
00239 sprintf(temp,"Name :%s\n",result2[1]);
00240 strcat(outcome,temp);
00241
00242 sprintf(sql_statement, "select * from answers order by qno");
00243 rc=sqlite3_get_table(database2,sql_statement,&result2,&rrow,&rcol,&zErrMsg);
00244 sprintf(temp,"Question\tAnswer\tCorrect Answer\tCorrect/Incorrect\n");
00245 strcat(outcome,temp);
00246 int count=0;
00247 for(j=2;j<(trow*tcol)+2;j+=2)
00248 {
00249 if((!strcmp(result2[j],result1[j]))&&(!strcmp(result2[j+1],result1[j+1])))
00250 {
00251 sprintf(temp,"%s\t\t%s\t\t\t%s\t\t\tCorrect\n",result1[j],result1[j+1],result2[j+1]);
00252 strcat(outcome,temp);
00253 count++;
00254 }
00255 else
00256 {
00257 sprintf(temp,"%s\t\t%s\t\t\t%s\t\t\tIncorrect\n",result1[j],result1[j+1],result2[j+1]);
00258 strcat(outcome,temp);
00259 }
00260 }
00261
00262 sprintf(temp,"\n\t\t\tMarks Obtained are :%d\n",count);
00263 strcat(outcome,temp);
00264 if(count<cf)
00265 {
00266 sprintf(temp,"\n\t\t\tResult : Fail\n");
00267 strcat(outcome,temp);
00268 }
00269 else
00270 {
00271 sprintf(temp,"\n\t\t\tResult : Pass\n");
00272 strcat(outcome,temp);
00273 }
00274 re1 = write(cfd1, outcome, strlen(outcome));
00275 if(re1 < 0)
00276 {
00277 perror("write() failed.\n");
00278 exit(1);
00279 }
00280
00281 }
00282
00283 re1 = close(cfd1);
00284 if(re1 <0)
00285 {
00286 perror("close failed\n");
00287 exit(1);
00288 }
00289
00290 return 0;
00291 }