00001
00011 #ifdef HAVE_CONFIG_H
00012 #include <config.h>
00013 #endif
00014 #include <mysql/mysql.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <sys/types.h>
00018 #include <sys/socket.h>
00019 #include <netinet/in.h>
00020 #include <arpa/inet.h>
00021 #include <unistd.h>
00022 #include <string.h>
00023 #include <signal.h>
00024
00025 int ID;
00026 char DESCRIPTION[100];
00027 float RATE;
00028 int QUANTITY;
00029 char UOM[100];
00030 char CATEGORY[100];
00032 char buffer[1024];
00033 char insert[1024];
00034 char rev[6][200];
00035 char retrieve[1000];
00036 int listen_file_descriptor;
00037
00038 void print_all_records();
00039 void modifyrecord();
00040 void delete_record();
00041 void fetch_record();
00042 void store_record();
00043 char arr[1000][10];
00044 char buf[4096];
00045 char username[50];
00046 char password[20];
00047 char id[10];
00048 unsigned int key;
00049 unsigned int count;
00050
00055 void close_properly(int signal)
00056 {
00057 int return_value;
00058 printf("Shutting down...\n");
00059 return_value =close(listen_file_descriptor);
00060 if(return_value <0)
00061 {
00062 perror("Cannot close listening socket.");
00063 exit(1);
00064 }
00065 printf("Shutdown complete.\n");
00066 exit(0);
00067 }
00073 int main(int argc, char *argv[])
00074 {
00075
00076 int connection_file_descriptor;
00077
00078 int server_len ;
00079 int rc,port ;
00080 struct sockaddr_in server_address;
00081 port=atoi(argv[1]);
00082 struct sigaction act1;
00083
00084 MYSQL *connection1;
00085 int result;
00086 connection1 = mysql_init (NULL);
00087 if (!connection1)
00088 {
00089 fprintf (stderr, "MySQL initialization failed\n");
00090 exit(EXIT_FAILURE);
00091 }
00092 connection1 = mysql_real_connect (connection1, "localhost", "User", "lucky", "Product", 0, NULL, 0);
00093 if (connection1)
00094 {
00095 printf ("Connection successful\n");
00096 result = mysql_query (connection1, "create table product_info(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,DESCRIPTION varchar(100),RATE float,QUANTITY INT,UNIT_OF_MEASUREMENT varchar(100),CATEGORY varchar(100))");
00097 if (result==0)
00098 {
00099 printf ("Table created successfully.\n");
00100 }
00101 else
00102 {
00103 fprintf (stderr, "Could not create table as %d: %s\n",
00104 mysql_errno (connection1),
00105 mysql_error (connection1));
00106
00107
00108 }
00109
00110 }
00111 else
00112 {
00113 printf ("Connection failed\n");
00114 }
00115 mysql_close (connection1);
00116
00117 act1.sa_handler = close_properly;
00118 sigemptyset(&act1.sa_mask);
00119 act1.sa_flags=0;
00120 sigaction(SIGINT, &act1, 0);
00121
00122
00123 listen_file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
00124 bzero(&server_address, sizeof(server_address));
00125 server_address.sin_family = AF_INET;
00126 server_address.sin_addr.s_addr = htons(INADDR_ANY);
00127 server_address.sin_port = htons(port) ;
00128 server_len = sizeof(server_address);
00129
00130 rc = bind(listen_file_descriptor, (struct sockaddr *) &server_address, server_len);
00131 printf("RC from bind = %d\n", rc ) ;
00132
00133
00134 rc = listen(listen_file_descriptor, 5);
00135 printf("RC from listen = %d\n", rc ) ;
00136
00137 while(1)
00138 { connection_file_descriptor = accept(listen_file_descriptor, (struct sockaddr *) NULL, NULL);
00139 printf("after accept()... connection_file_descriptor = %d\n", connection_file_descriptor) ;
00140 char ch[100];
00141 printf("server waiting\n");
00142 while(1){
00143 rc = read(connection_file_descriptor, ch, 99);
00144 ch[rc]='\0';
00145
00146 if(strcmp(ch,"Insert")==0)
00147 {
00148 int i;
00149 for(i=0;i<1024;i++)
00150 buffer[i]='\0';
00151 printf("%s",ch);
00152 rc = read(connection_file_descriptor, &insert, 99);
00153 store_record();
00154 int l=strlen(insert);
00155 write(connection_file_descriptor, &insert,l);
00156 }
00157 if(strcmp(ch,"Update")==0)
00158 {
00159 int i;
00160 for( i=0;i<1024;i++)
00161 buffer[i]='\0';
00162 rc = read(connection_file_descriptor, &buffer, 99);
00163 printf("RC from read = %d \n", rc ) ;
00164 modifyrecord();
00165 if(rc==0)
00166 break;
00167 int l=strlen(buffer);
00168 write(connection_file_descriptor, &buffer,l);
00169 }
00170 if(strcmp(ch,"combo")==0)
00171 {
00172 print_all_records();
00173 strcpy(buf,arr[0]);
00174 int i=1;
00175 printf("%d\n",count);
00176 while(i<count)
00177 {
00178
00179 strcat(buf," ");
00180 strcat(buf,arr[i]);
00181
00182 i++;
00183 }
00184 write(connection_file_descriptor,&buf,4096);
00185 }
00186 if(strcmp(ch,"Retrieve")==0)
00187 {
00188 printf("%s\n",ch);
00189 int i;
00190 for( i=0;i<1024;i++)
00191 buffer[i]='\0';
00192 printf("%s\n",buffer);
00193 rc = read(connection_file_descriptor, &buffer, 99);
00194 printf("%s\n",buffer);
00195 fetch_record();
00196 if(key==0)
00197 {
00198 char fail[]="F";
00199 write(connection_file_descriptor,&fail,strlen(fail));
00200 }
00201 else
00202 {
00203 strcpy(retrieve,rev[0]);
00204 i=1;
00205 printf("%d\n",key);
00206 while(i<key){
00207 strcat(retrieve," ");
00208 strcat(retrieve,rev[i]);
00209 i++;
00210 }
00211 printf("%s\n",retrieve);
00212 write(connection_file_descriptor,&retrieve,1000);
00213 }
00214 }
00215
00216 if(strcmp(ch,"Delete")==0)
00217 {
00218 printf("%s\n",ch);
00219 rc = read(connection_file_descriptor, &id, 10);
00220 printf("%s\n",id);
00221 delete_record();
00222 }
00223 if(rc==0)
00224 break;
00225 }
00226
00227 close(connection_file_descriptor);
00228 }
00229 printf("server exiting\n");
00230 return 0;
00231 }
00232
00238 void print_all_records()
00239 {
00240 char sql_statement[100];
00241 MYSQL *connection1;
00242 MYSQL_RES *result;
00243 MYSQL_ROW sqlrow;
00244
00245 int return_value;
00246
00247 sprintf(sql_statement, "SELECT ID from product_info");
00248
00249 connection1 = mysql_init(NULL);
00250 if (mysql_real_connect (connection1, "localhost", "User", "lucky", "Product", 0, NULL, 0))
00251 {
00252 return_value = mysql_query (connection1, sql_statement);
00253 if (return_value)
00254 {
00255 printf ("select failed as : %s\n", mysql_error (connection1));
00256 }
00257 else
00258 {
00259 result = mysql_use_result (connection1);
00260 if (result)
00261 { count=0;
00262 while ((sqlrow = mysql_fetch_row (result)))
00263 {
00264 unsigned int field_count;
00265 field_count = 0;
00266 while (field_count < mysql_field_count (connection1))
00267 {
00268 strcpy(arr[count],sqlrow[field_count]);
00269 field_count++;
00270 count++;
00271 }
00272
00273 }
00274
00275 if (mysql_errno (connection1))
00276 {
00277 printf ("Error occurred while retrieving data : %s\n", mysql_error (connection1));
00278 }
00279 }
00280 mysql_free_result(result);
00281
00282 }
00283 mysql_close (connection1);
00284 }
00285 else
00286 {
00287 fprintf (stderr, "Connection failed\n");
00288 if (mysql_errno (connection1))
00289 {
00290 fprintf (stderr, "Connection error %d: %s\n",
00291 mysql_errno (connection1),
00292 mysql_error (connection1));
00293 }
00294
00295 }
00296
00297 }
00302 void modifyrecord()
00303 {
00304 char delims[]=" ";
00305 char *result=NULL;
00306 char str[6][100];
00307 result = strtok( buffer, delims );
00308 int i=0;
00309 while( result != NULL )
00310 { strcpy(str[i],result);
00311
00312 result = strtok( NULL, delims );
00313 i++;
00314 }
00315
00316 ID=atoi(str[0]);
00317 strcpy(DESCRIPTION,str[1]);
00318 RATE=atof(str[2]);
00319 QUANTITY=atoi(str[3]);
00320 strcpy(UOM,str[4]);
00321 strcpy(CATEGORY,str[5]);
00322
00323 char sql_statement[1000];
00324
00325 MYSQL *connection1;
00326 int res;
00327 sprintf(sql_statement, "Update product_info set DESCRIPTION='%s',RATE=%f,QUANTITY=%d,UNIT_OF_MEASUREMENT='%s',CATEGORY='%s' where ID=%d ",DESCRIPTION,RATE,QUANTITY,UOM,CATEGORY,ID);
00328
00329 connection1 = mysql_init(NULL);
00330 if (mysql_real_connect (connection1, "localhost", "User", "lucky", "Product", 0, NULL, 0))
00331 {
00332 res = mysql_query (connection1, sql_statement);
00333 if (res==0)
00334 {
00335 printf ("Row UPDATED successfully.\n");
00336 }
00337 else
00338 {
00339 fprintf (stderr, "Could not update data as %d: %s\n",
00340 mysql_errno (connection1),
00341 mysql_error (connection1));
00342
00343
00344 }
00345
00346 mysql_close (connection1);
00347 }
00348 else
00349 {
00350 fprintf (stderr, "Connection failed\n");
00351 if (mysql_errno (connection1))
00352 {
00353 fprintf (stderr, "Connection error %d: %s\n",
00354 mysql_errno (connection1),
00355 mysql_error (connection1));
00356 }
00357 }
00358 }
00364 void store_record()
00365 {
00366 char delims[]=" ";
00367 char *result=NULL;
00368 char str[6][100];
00369 result = strtok( insert, delims );
00370 int i=0;
00371 while( result != NULL )
00372 { strcpy(str[i],result);
00373
00374 result = strtok( NULL, delims );
00375 i++;
00376 }
00377
00378 ID=atoi(str[0]);
00379 strcpy(DESCRIPTION,str[1]);
00380 RATE=atof(str[2]);
00381 QUANTITY=atoi(str[3]);
00382 strcpy(UOM,str[4]);
00383 strcpy(CATEGORY,str[5]);
00384 printf( "ID=%d DESCRIPTION=%s RATE=%f QUANTITY=%d UNIT=%s CATEGORY=%s", ID,DESCRIPTION,RATE,QUANTITY,UOM,CATEGORY);
00385
00386 char sql_statement[1000];
00387
00388 MYSQL *connection1;
00389 int res;
00390 printf("RATE=%f\n",RATE);
00391 sprintf(sql_statement, "Insert into product_info values (%d,'%s',%f,%d,'%s','%s')",ID,DESCRIPTION,RATE,QUANTITY,UOM,CATEGORY);
00392
00393 connection1 = mysql_init(NULL);
00394 if (mysql_real_connect (connection1, "localhost", "User", "lucky", "Product", 0, NULL, 0))
00395 {
00396 res = mysql_query (connection1, sql_statement);
00397 if (res==0)
00398 {
00399 printf ("Row inserted successfully.\n");
00400 }
00401 else
00402 {
00403 fprintf (stderr, "Could not insert data as %d: %s\n",
00404 mysql_errno (connection1),
00405 mysql_error (connection1));
00406 }
00407 mysql_close (connection1);
00408 }
00409 else
00410 {
00411 fprintf (stderr, "Connection failed\n");
00412 if (mysql_errno (connection1))
00413 {
00414 fprintf (stderr, "Connection error %d: %s\n",
00415 mysql_errno (connection1),
00416 mysql_error (connection1));
00417 }
00418 }
00419 }
00424 void fetch_record()
00425 {
00426 char sql_statement[100];
00427 MYSQL *connection1;
00428 MYSQL_RES *selectresult;
00429 MYSQL_ROW sqlrow;
00430
00431 char delims[]=" ";
00432 char *result=NULL;
00433 char str[2][100];
00434 result = strtok( buffer, delims );
00435 int i=0;
00436 while( result != NULL )
00437 { strcpy(str[i],result);
00438
00439 result = strtok( NULL, delims );
00440 i++;
00441 }
00442
00443 ID=atoi(str[0]);
00444 strcpy(CATEGORY,str[1]);
00445
00446 int return_value;
00447
00448 sprintf(sql_statement, "SELECT * from product_info where ID='%d' and CATEGORY='%s'", ID,CATEGORY);
00449
00450 connection1 = mysql_init(NULL);
00451 if (mysql_real_connect (connection1, "localhost", "User", "lucky", "Product", 0, NULL, 0))
00452 {
00453 return_value = mysql_query (connection1, sql_statement);
00454
00455 if (return_value)
00456 {
00457 printf ("select failed as : %s\n", mysql_error (connection1));
00458 }
00459 else
00460 {
00461 selectresult = mysql_use_result (connection1);
00462 if (selectresult)
00463 { key=0;
00464 while ((sqlrow = mysql_fetch_row (selectresult)))
00465 {
00466 unsigned int field_count;
00467 field_count = 0;
00468 while (field_count < mysql_field_count (connection1))
00469 {
00470 strcpy(rev[key],sqlrow[field_count]);
00471
00472 field_count++;
00473 key++;
00474 }
00475 }
00476 if (mysql_errno (connection1))
00477 {
00478 printf ("Error occurred while retrieving data : %s\n", mysql_error (connection1));
00479 }
00480 }
00481 mysql_free_result(selectresult);
00482
00483 }
00484 mysql_close (connection1);
00485 }
00486 else
00487 {
00488 fprintf (stderr, "Connection failed\n");
00489 if (mysql_errno (connection1))
00490 {
00491 fprintf (stderr, "Connection error %d: %s\n",
00492 mysql_errno (connection1),
00493 mysql_error (connection1));
00494 }
00495
00496 }
00497
00498 }
00499
00504 void delete_record()
00505 {
00506 char sql_statement[1000];
00507 MYSQL *connection1;
00508 int result;
00509 printf("%s\n",id);
00510 sprintf(sql_statement, "DELETE from product_info where ID=%s", id);
00511
00512 connection1 = mysql_init(NULL);
00513 if (mysql_real_connect (connection1, "localhost", "User", "lucky", "Product", 0, NULL, 0))
00514 {
00515 result = mysql_query (connection1, sql_statement);
00516 if (result==0)
00517 {
00518 printf ("Row deleted successfully.\n");
00519 }
00520 else
00521 {
00522 fprintf (stderr, "Could not insert data as %d: %s\n",
00523 mysql_errno (connection1),
00524 mysql_error (connection1));
00525 }
00526 mysql_close (connection1);
00527 }
00528 else
00529 {
00530 fprintf (stderr, "Connection failed\n");
00531 if (mysql_errno (connection1))
00532 {
00533 fprintf (stderr, "Connection error %d: %s\n",
00534 mysql_errno (connection1),
00535 mysql_error (connection1));
00536 }
00537 }
00538 }