Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.36 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. #include <pthread.h>
  11. #include <sqlite3.h>
  12. int j=0;
  13. #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
  14. #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
  15.  
  16. static int select_query(void *data,int argc, char **argv, char **azColName){
  17. if (argv==NULL)
  18. {return 0;}
  19. else
  20. {return 4;}
  21. }
  22.  
  23. static int select_descriptor_query(void *data,int argc, char **argv, char **azColName){
  24. if (argv==NULL){
  25. *(int*)data=0;
  26. return 0;}
  27. else{
  28. *(int*)data=atoi(argv[1]);
  29. return 4;}
  30. }
  31.  
  32. static int nothing_to_do_query(void *NotUsed, int argc, char **argv, char **azColName){
  33. return 0;
  34. }
  35.  
  36. static int select_messages_query(void *descriptor_client, int argc, char **argv, char **azColName){
  37. j++;
  38. printf("%d\n",j);
  39. int i;
  40. char istoricul[1000]="";
  41. for(i=0; i<argc; i++){
  42. strcpy(istoricul,azColName[i]);
  43. strcat(istoricul," = ");
  44. if(argv[i]){
  45. strcat(istoricul,argv[i]);
  46. }
  47. else{
  48. strcat(istoricul,"NULL");
  49. }
  50. if(i==(argc-1)){
  51. strcat(istoricul,"\n\n");
  52. }
  53. if (write (*(int*)descriptor_client, istoricul,1000) <= 0){
  54. perror (" Eroare la write() catre client.\n");
  55.  
  56. }
  57. }
  58. return 0;
  59. }
  60.  
  61.  
  62. #define PORT 2910
  63. extern int errno;
  64.  
  65. typedef struct thData{
  66. int idThread; //id-ul thread-ului tinut in evidenta de acest program
  67. int cl; //descriptorul intors de accept
  68. }thData;
  69.  
  70. char* citire(thData tdL){
  71. char* array=(char*)malloc(500);
  72. if(read (tdL.cl,array,500) <= 0)
  73. {
  74. printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
  75. }
  76. return array;
  77. }
  78.  
  79. int scriere_un_int(thData tdL,int nr){
  80. fflush(stdout);
  81. if (write (tdL.cl, &nr, sizeof(int)) <= 0){
  82. printf("[Thread %d] ",tdL.idThread);
  83. perror (" Eroare la write() catre client.\n");
  84. }
  85. return nr;
  86. }
  87.  
  88. int log_inreg_decon(thData tdL){
  89. int nr=-1;
  90. fflush (stdout);
  91. if (read (tdL.cl, &nr,sizeof(int)) <= 0){
  92. printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
  93. close (tdL.cl);
  94. return(-1);
  95. }
  96. return scriere_un_int(tdL,nr);
  97. }
  98.  
  99.  
  100. static void *user(void * arg){
  101. char array[500],array1[600]="";
  102. char temporary_username[30]="";
  103. int nr;
  104. fflush (stdout);
  105. pthread_detach(pthread_self());
  106. struct thData tdL;
  107. tdL= *((struct thData*)arg);
  108. sqlite3 *db;
  109. char *zErrMsg = 0;
  110. int rc;
  111. char *sql;
  112. rc = sqlite3_open_v2("retele.db", &db,SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX,NULL);
  113. if(rc){
  114. fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  115. exit(0);
  116. }
  117. else{
  118. fprintf(stderr, "Opened database successfully\n");
  119. }
  120. char* parola=(char*)malloc(30);
  121. char* login=(char*)malloc(30);
  122.  
  123. while(1){
  124. nr=0;
  125. while(nr!=1 && nr!=2 && nr!=3 && nr!=4){
  126. nr=log_inreg_decon(tdL);
  127. if(nr==-1){
  128. close (((struct thData*)arg)->cl);
  129. return(NULL);
  130. }
  131. }
  132. if (nr==4){//a fost aleasa deconectarea
  133. close (((struct thData*)arg)->cl);
  134. return(NULL);
  135. }
  136. fflush (stdout);
  137. login=citire(tdL);
  138. parola=citire(tdL);
  139. if(nr==1 || nr ==3){//logarea sau stergerea contului
  140. sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("' AND password='")+30+sizeof("'"));
  141. strcpy(sql,"SELECT * from USERS WHERE username='");
  142. strcat(sql,login);
  143. strcat(sql,"' AND password='");
  144. strcat(sql,parola);
  145. }
  146. else if(nr==2){//inregistrarea
  147. sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
  148. strcpy(sql,"SELECT * from USERS WHERE username='");
  149. strcat(sql,login);
  150. }
  151. strcat(sql,"'");
  152. printf("%s\n",sql );
  153. rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
  154. if( rc != SQLITE_OK && rc!=4 ){
  155. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  156. sqlite3_free(zErrMsg);
  157. }
  158. rc=scriere_un_int(tdL,rc);
  159. if (nr==1){//a fost aleasa logarea
  160. if(rc==0){//logare nereusita
  161. if (read (tdL.cl, &nr,sizeof(int)) <= 0){
  162. printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
  163. }
  164. printf("daca vrem sa continuam sau nu %d\n",nr );
  165. if(nr==1){
  166. continue;
  167. }
  168. else{
  169. close (((struct thData*)arg)->cl);
  170. return(NULL);
  171. }
  172. }
  173. else if(rc==4){//logare reusita
  174. break;
  175. }
  176. }
  177. else if (nr==2){//a fost aleasa inregistrarea
  178. if (rc==0){//inregistrare reusita
  179. sql=(char*)realloc(sql,sizeof("INSERT into USERS(username,password) values('")+30+sizeof("','")+30+sizeof("')"));
  180. strcpy(sql,"INSERT into USERS(username,password) values('");
  181. strcat(sql,login);
  182. strcat(sql,"','");
  183. strcat(sql,parola);
  184. strcat(sql,"')");
  185. rc = sqlite3_exec(db, sql, nothing_to_do_query, 0, &zErrMsg);
  186. if( rc != SQLITE_OK ){
  187. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  188. sqlite3_free(zErrMsg);
  189. }
  190. else{
  191. fprintf(stdout, "Records created successfully\n");
  192. }
  193. }
  194. if (read (tdL.cl, &nr,sizeof(int)) <= 0){
  195. printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
  196. }
  197. if(nr==1){
  198. continue;
  199. }
  200. else{
  201. close (((struct thData*)arg)->cl);
  202. return(NULL);
  203. }
  204. }
  205. else if (nr==3){//a fost aleasa stergerea contului
  206. if (rc==4){//stergerea contului reusita
  207. sql=(char*)malloc(sizeof("DELETE from USERS WHERE username='")+30+sizeof("' AND password='")+30+sizeof("'"));
  208. strcpy(sql,"DELETE from USERS WHERE username='");
  209. strcat(sql,login);
  210. strcat(sql,"' AND password='");
  211. strcat(sql,parola);
  212. strcat(sql,"'");
  213. rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
  214. if( rc != SQLITE_OK ){
  215. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  216. sqlite3_free(zErrMsg);
  217. }
  218. else{
  219. fprintf(stdout, "Records deleted successfully\n");
  220. }
  221. }
  222. if (read (tdL.cl, &nr,sizeof(int)) <= 0){
  223. printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
  224. return NULL;
  225. }
  226. if(nr==1){
  227. continue;
  228. }
  229. else{
  230. close (((struct thData*)arg)->cl);
  231. return(NULL);
  232. }
  233. }
  234. }
  235.  
  236. sql=(char*)malloc(sizeof("SELECT * from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
  237. strcpy(sql,"SELECT * from CONNECTED_USERS WHERE username='");
  238. strcat(sql,login);
  239. strcat(sql,"'");
  240. rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
  241. if( rc != SQLITE_OK && rc!=4 ){
  242. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  243. sqlite3_free(zErrMsg);
  244. }
  245.  
  246. if(rc==4){
  247. int descriptor_se_va_inchide;
  248. sql=(char*)malloc(sizeof("SELECT * from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
  249. strcpy(sql,"SELECT * from CONNECTED_USERS WHERE username='");
  250. strcat(sql,login);
  251. strcat(sql,"'");
  252. rc = sqlite3_exec(db, sql, select_descriptor_query, (void*)&descriptor_se_va_inchide, &zErrMsg);
  253. if( rc != SQLITE_OK ){
  254. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  255. sqlite3_free(zErrMsg);
  256. }
  257. if (write (descriptor_se_va_inchide,"Sunteti deconectati deoarece s-a conectat alt user cu contul dumneavoastra",sizeof("Sunteti deconectati deoarece s-a conectat alt user cu contul dumneavoastra")) <= 0){
  258. perror ("[client]Eroare la write() spre server.\n");
  259. close (((struct thData*)arg)->cl);
  260. return(NULL);
  261. }
  262. close(descriptor_se_va_inchide);
  263. sql=(char*)malloc(sizeof("DELETE from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
  264. strcpy(sql,"DELETE from CONNECTED_USERS WHERE username='");
  265. strcat(sql,login);
  266. strcat(sql,"'");
  267. rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
  268. if( rc != SQLITE_OK ){
  269. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  270. sqlite3_free(zErrMsg);
  271. }
  272. else{
  273. fprintf(stdout, "Records deleted successfully\n");
  274. }
  275. }
  276.  
  277. char buffer[33];
  278. sprintf(buffer,"%d",tdL.cl);
  279. sql=(char*)realloc(sql,sizeof("INSERT into CONNECTED_USERS(username,descriptor) values('")+30+sizeof("',")+sizeof(int)+sizeof(")"));
  280. strcpy(sql,"INSERT into CONNECTED_USERS(username,descriptor) values('");
  281. strcat(sql,login);
  282. strcat(sql,"',");
  283. strcat(sql,buffer);
  284. strcat(sql,")");
  285. rc = sqlite3_exec(db, sql, nothing_to_do_query, 0, &zErrMsg);
  286. if( rc != SQLITE_OK ){
  287. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  288. sqlite3_free(zErrMsg);
  289. }
  290. else{
  291. fprintf(stdout, "Records created successfully\n");
  292. }
  293.  
  294. int user_cu_cine_vb=-1,online=-1;
  295. char nume_cu_cine_vb[30]="";
  296.  
  297. sql=(char*)realloc(sql,sizeof("SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES,USERS WHERE USERNAME='")+30+sizeof("' AND TO_USER=USERNAME AND TIME_SENT>LAST_LOG_OUT"));
  298. strcpy(sql,"SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES,USERS WHERE USERNAME='");
  299. strcat(sql,login);
  300. strcat(sql,"' AND TO_USER=USERNAME AND TIME_SENT>LAST_LOG_OUT");
  301. rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
  302. if( rc != SQLITE_OK && rc!=4 ){
  303. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  304. sqlite3_free(zErrMsg);
  305. }
  306. rc=scriere_un_int(tdL,rc);
  307. if (rc==4){
  308. if(read (tdL.cl,&nr,sizeof(int)) <= 0){
  309. printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
  310. return NULL;
  311. }
  312. if(nr==1){
  313. rc = sqlite3_exec(db, sql, select_messages_query, (void*)&tdL.cl, &zErrMsg);
  314. if( rc != SQLITE_OK && rc!=4 ){
  315. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  316. sqlite3_free(zErrMsg);
  317. }
  318. }
  319. }
  320.  
  321.  
  322. while(1){
  323. if(read (tdL.cl,array,500) <= 0){
  324. printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
  325. break;
  326. }
  327. if(strcmp(array,".logout")==0){//urmeaza deconectarea
  328. break;
  329. }
  330. else if(strcmp(array,".delete")==0){//urmeaza stergerea contului
  331. sql=(char*)malloc(sizeof("DELETE from USERS WHERE username='")+30+sizeof("' AND password='")+30+sizeof("'"));
  332. strcpy(sql,"DELETE from USERS WHERE username='");
  333. strcat(sql,login);
  334. strcat(sql,"' AND password='");
  335. strcat(sql,parola);
  336. strcat(sql,"'");
  337. rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
  338. if( rc != SQLITE_OK ){
  339. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  340. sqlite3_free(zErrMsg);
  341. }
  342. else{
  343. fprintf(stdout, "Records deleted successfully\n");
  344. if (write (tdL.cl, "Stergerea contului a fost efectuata cu succes\n", sizeof("Stergerea contului a fost efectuata cu succes\n")) <= 0){
  345. printf("[Thread %d] ",tdL.idThread);
  346. perror (" Eroare la write() catre client.\n");
  347. }
  348. }
  349. break;
  350. }
  351. else if (strcmp(array,".meniu")==0){//se afiseaza lista de shortcut-uri si instrumente ale aplicatiei
  352. continue;
  353. }
  354. else if (strcmp(array,".history")==0){//se afiseaza tot istoricul userului
  355. sql=(char*)malloc(sizeof("SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES WHERE TO_USER='")+30+sizeof("' OR FROM_USER='")+30+sizeof("'"));
  356. strcpy(sql,"SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES WHERE TO_USER='");
  357. strcat(sql,login);
  358. strcat(sql,"' OR FROM_USER='");
  359. strcat(sql,login);
  360. strcat(sql,"'");
  361. rc = sqlite3_exec(db, sql, select_messages_query, (void*)&tdL.cl, &zErrMsg);
  362. if( rc != SQLITE_OK ){
  363. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  364. sqlite3_free(zErrMsg);
  365. }
  366.  
  367. continue;
  368. }
  369. else if (array[0]=='@'){//mesajul incepe cu @, se presupune ca se alege user nou cu care se va vorbi
  370. char*ret;
  371. ret = strrchr(array,'@');
  372. char username[30]="";
  373. if(strcmp(ret,"@")==0 && array[1]!='\0'){//se alege user nou cu care se va vorbi
  374. int user_posibil_cu_cine_vb=0;
  375. int i=1;
  376. while(strcmp(array+i,"@")!=0){
  377. strncat(username,(array+i),1);
  378. i++;
  379. }
  380. printf("Username ales va fi %s\n",username);
  381. fflush(stdout);
  382. sql=(char*)malloc(sizeof("SELECT * from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
  383. strcpy(sql,"SELECT * from CONNECTED_USERS WHERE username='");
  384. strcat(sql,username);
  385. strcat(sql,"'");
  386. strcpy(nume_cu_cine_vb,username);
  387. rc = sqlite3_exec(db, sql, select_descriptor_query, (void*)&user_posibil_cu_cine_vb, &zErrMsg);
  388. rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
  389. if( rc != SQLITE_OK ){
  390. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  391. sqlite3_free(zErrMsg);
  392. }
  393. if(user_posibil_cu_cine_vb!=0){
  394. fflush(stdout);
  395. if (write (tdL.cl, "Userul ales este online\n", sizeof("Userul ales este online\n")) <= 0){
  396. printf("[Thread %d] ",tdL.idThread);
  397. perror (" Eroare la write() catre client.\n");
  398. }
  399. user_cu_cine_vb=user_posibil_cu_cine_vb;
  400. online=1;
  401. fflush(stdout);
  402. }
  403. else{
  404. sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
  405. strcpy(sql,"SELECT * from USERS WHERE username='");
  406. strcat(sql,username);
  407. strcat(sql,"'");
  408. rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
  409. if( rc != SQLITE_OK && rc!=4){
  410. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  411. sqlite3_free(zErrMsg);
  412. }
  413. if(rc==4){
  414. online=0;
  415. user_cu_cine_vb=0;
  416. fflush(stdout);
  417. if (write (tdL.cl, "Userul ales este offline\n", sizeof("Userul ales este offline\n")) <= 0){
  418. printf("[Thread %d] ",tdL.idThread);
  419. perror (" Eroare la write() catre client.\n");
  420. }
  421. }
  422. else{
  423. if (write (tdL.cl, "Nu exista nici un user inregistrat cu username-ul introdus\n", sizeof("Nu exista nici un user inregistrat cu username-ul introdus\n")) <= 0){
  424. printf("[Thread %d] ",tdL.idThread);
  425. perror (" Eroare la write() catre client.\n");
  426. }
  427. strcpy(nume_cu_cine_vb,"");
  428. user_cu_cine_vb=-1;
  429. online=-1;
  430. }
  431. }
  432. continue;
  433. }
  434. }
  435.  
  436. else if (array[0]=='.' && array[1]=='h' && array[2]=='i' && array[3]=='s' && array[4]=='t' && array[5]=='@'){//se afiseaza istoricul userului cu nume_user
  437. char * nume_din_istoric=array+5;
  438. char*ret;
  439. ret = strrchr(nume_din_istoric,'@');
  440. char username[30]="";
  441. if(strcmp(ret,"@")==0 && array[6]!='\0'){//se alege user nou cu care se va vorbi
  442. int user_posibil_cu_cine_vb=0;
  443. int i=1;
  444. while((nume_din_istoric+i)[0]!='@' && (nume_din_istoric+i+1)!=NULL){
  445. strncat(username,(nume_din_istoric+i),1);
  446. i++;
  447. }
  448. sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
  449. strcpy(sql,"SELECT * from USERS WHERE username='");
  450. strcat(sql,username);
  451. strcat(sql,"'");
  452. rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
  453. if( rc != SQLITE_OK && rc!=4){
  454. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  455. sqlite3_free(zErrMsg);
  456. }
  457. if(rc==4){
  458. fflush(stdout);
  459. sql=(char*)malloc(sizeof("SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES WHERE (TO_USER='")+30+sizeof("' AND FROM_USER='")+30+sizeof("') or (TO_USER='")+30+sizeof("' AND FROM_USER='")+30+sizeof("')"));
  460. strcpy(sql,"SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES WHERE (TO_USER='");
  461. strcat(sql,login);
  462. strcat(sql,"' AND FROM_USER='");
  463. strcat(sql,username);
  464. strcat(sql,"') or (TO_USER='");
  465. strcat(sql,username);
  466. strcat(sql,"' AND FROM_USER='");
  467. strcat(sql,login);
  468. strcat(sql,"')");
  469. rc = sqlite3_exec(db, sql, select_messages_query, (void*)&tdL.cl, &zErrMsg);
  470. if( rc != SQLITE_OK ){
  471. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  472. sqlite3_free(zErrMsg);
  473. }
  474. }
  475. else{
  476. if (write (tdL.cl, "Nu exista nici un user inregistrat cu username-ul introdus\n", sizeof("Nu exista nici un user inregistrat cu username-ul introdus\n")) <= 0){
  477. printf("[Thread %d] ",tdL.idThread);
  478. perror (" Eroare la write() catre client.\n");
  479. }
  480. }
  481. continue;
  482. }
  483. }
  484.  
  485. else if (array[0]=='<' && array[1]!='\0' ){//se trimite un mesaj care va fi raspund la mesaj de la nume_user trimis la numar_secunde
  486. bzero(array1,sizeof(array1));
  487. int poz_delim=strcspn(array,">");
  488. int poz_delim_1=strcspn(array+poz_delim+1,">");
  489. if(poz_delim<(sizeof(array)-1) && poz_delim>1 && array[poz_delim+1]=='<' && array[poz_delim+2]!='>' && poz_delim_1<(sizeof(array)-1)){
  490. char username[30]="";
  491. int i=1;
  492. while(i<poz_delim){
  493. strncat(username,(array+i),1);
  494. i++;
  495. }
  496. sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
  497. strcpy(sql,"SELECT * from USERS WHERE username='");
  498. strcat(sql,username);
  499. strcat(sql,"'");
  500. rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
  501. if( rc != SQLITE_OK && rc!=4){
  502. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  503. sqlite3_free(zErrMsg);
  504. }
  505. if(rc==4){
  506. fflush(stdout);
  507. char numar_secunde[30]="";
  508. i=poz_delim+2;
  509. while(array[i]!='>'){
  510. strncat(numar_secunde,(array+i),1);
  511. i++;
  512. }
  513. sql=(char*)malloc(sizeof("SELECT * from MESSAGES WHERE FROM_USER='")+30+sizeof("' AND TIME_SENT=")+30);
  514. strcpy(sql,"SELECT * from MESSAGES WHERE FROM_USER='");
  515. strcat(sql,username);
  516. strcat(sql,"' AND TIME_SENT=");
  517. strcat(sql,numar_secunde);
  518. rc = sqlite3_exec(db, sql, select_query, (void*)&tdL.cl, &zErrMsg);
  519. if( rc != SQLITE_OK && rc!=4){
  520. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  521. sqlite3_free(zErrMsg);
  522. }
  523. if(rc==4){
  524. strcpy(array,(array+poz_delim_1+poz_delim+2));
  525. fflush(stdout);
  526. strcat(array1,"(Raspuns pentru mesaj trimis de ");
  527. strcat(array1,"<");
  528. strcat(array1,username);
  529. strcat(array1,"> la ");
  530. strcat(array1,"<");
  531. strcat(array1,numar_secunde);
  532. strcat(array1,"> )");
  533. strcat(array1,array);
  534. strcpy(temporary_username,username);
  535. }
  536. else{
  537. if (write (tdL.cl, "Nu a fost trimis nici un mesaj de user introdus la timp dat\n", sizeof("Nu a fost trimis nici un mesaj de user introdus la timp dat\n")) <= 0){
  538. printf("[Thread %d] ",tdL.idThread);
  539. perror (" Eroare la write() catre client.\n");
  540. }
  541. continue;
  542. }
  543. }
  544. else{
  545. if (write (tdL.cl, "Nu exista nici un user inregistrat cu username-ul introdus\n", sizeof("Nu exista nici un user inregistrat cu username-ul introdus\n")) <= 0){
  546. printf("[Thread %d] ",tdL.idThread);
  547. perror (" Eroare la write() catre client.\n");
  548. }
  549. continue;
  550. }
  551. }
  552. }
  553. if(user_cu_cine_vb!=-1 || strcmp(array1,"\0")!=0){
  554. //se trimite la user descriptorul caruia se pastreaza in user_cu_cine_vb
  555. time_t timer;
  556. time(&timer);
  557. char buffer[33];
  558. sprintf(buffer,"%d",timer);
  559. if(online==1){
  560. char mesaj[532]="";
  561. strcpy(mesaj,"<");
  562. strcat(mesaj,login);
  563. strcat(mesaj,">");
  564. strcat(mesaj,array);
  565. strcat(mesaj,"<");
  566. strcat(mesaj,buffer);
  567. strcat(mesaj,">");
  568. if(write(user_cu_cine_vb,mesaj,532)<=0){
  569. perror ("[client]Eroare la write() spre alt client.\n");
  570. exit;
  571. }
  572. }
  573. if (strcmp(array1,"\0")!=0){
  574. strcpy(array,array1);
  575. strcpy(array1,"\0");
  576. }
  577. if (strcmp(temporary_username,"\0")!=0){
  578. char aux[30]="";
  579. strcpy(aux,nume_cu_cine_vb);
  580. strcpy(nume_cu_cine_vb,temporary_username);
  581. strcpy(temporary_username,aux);
  582. }
  583. sql=(char*)malloc(sizeof("INSERT into MESSAGES(from_user,to_user,time_sent,message) values('")+30+sizeof("','")+30+sizeof("',")+sizeof(int)+sizeof(",' ")+500+sizeof(" ')"));
  584. strcpy(sql,"INSERT into MESSAGES(from_user,to_user,time_sent,message) values('");
  585. strcat(sql,login);
  586. strcat(sql,"','");
  587. strcat(sql,nume_cu_cine_vb);
  588. strcat(sql,"',");
  589. strcat(sql,buffer);
  590. strcat(sql,",' ");
  591. strcat(sql,array);
  592. strcat(sql," ')");
  593. rc = sqlite3_exec(db, sql, nothing_to_do_query, 0, &zErrMsg);
  594. if( rc != SQLITE_OK ){
  595. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  596. sqlite3_free(zErrMsg);
  597. }
  598. if (strcmp(temporary_username,"\0")!=0){
  599. char aux[30]="";
  600. strcpy(aux,nume_cu_cine_vb);
  601. strcpy(nume_cu_cine_vb,temporary_username);
  602. strcpy(temporary_username,aux);
  603. }
  604. fflush(stdout);
  605.  
  606.  
  607.  
  608. }
  609. else{
  610. if(write(tdL.cl,"Nu este selectat nici un user ca destinatar al mesajelor dumneavoastra.\nPuteti consulta meniul(comanda '.meniu') pentru a afla cum se alege user pentru schimb de mesaje.",sizeof("Nu este selectat nici un user ca destinatar al mesajelor dumneavoastra.\nPuteti consulta meniul(comanda '.meniu') pentru a afla cum se alege user pentru schimb de mesaje."))<=0){
  611. perror ("[client]Eroare la write() spre alt client.\n");
  612. exit;
  613. }
  614. }
  615. }
  616.  
  617.  
  618. close (((struct thData*)arg)->cl);
  619. sql=(char*)malloc(sizeof("DELETE from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
  620. strcpy(sql,"DELETE from CONNECTED_USERS WHERE username='");
  621. strcat(sql,login);
  622. strcat(sql,"'");
  623. rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
  624. if( rc != SQLITE_OK ){
  625. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  626. sqlite3_free(zErrMsg);
  627. }
  628. else{
  629. fprintf(stdout, "Records deleted successfully\n");
  630. }
  631. time_t timer;
  632. time(&timer);
  633. sprintf(buffer,"%d",timer);
  634. sql=(char*)malloc(sizeof("UPDATE USERS set LAST_LOG_OUT = ")+33+sizeof( " WHERE username='")+30+sizeof("'"));
  635. strcpy(sql,"UPDATE USERS set LAST_LOG_OUT = ");
  636. strcat(sql,buffer);
  637. strcat(sql," WHERE username='");
  638. strcat(sql,login);
  639. strcat(sql,"'");
  640. rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
  641. if( rc != SQLITE_OK ){
  642. fprintf(stderr, "SQL error: %s\n",zErrMsg);
  643. sqlite3_free(zErrMsg);
  644. }
  645. else{
  646. fprintf(stdout, "Records updated successfully\n");
  647. }
  648. return(NULL);
  649. };
  650.  
  651.  
  652. int main ()
  653. {
  654. struct sockaddr_in server; // structura folosita de server
  655. struct sockaddr_in from;
  656. int sd; //descriptorul de socket
  657. int pid;
  658. pthread_t *th=NULL; //Identificatorii thread-urilor care se vor crea
  659. int i=0;
  660.  
  661. /* crearea unui socket */
  662. if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1){
  663. perror ("[server]Eroare la socket().\n");
  664. return errno;
  665. }
  666. /* utilizarea optiunii SO_REUSEADDR */
  667. int on=1;
  668. setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
  669. /* pregatirea structurilor de date */
  670. bzero (&server, sizeof (server));
  671. bzero (&from, sizeof (from));
  672. /* umplem structura folosita de server */
  673. /* stabilirea familiei de socket-uri */
  674. server.sin_family = AF_INET;
  675. /* acceptam orice adresa */
  676. server.sin_addr.s_addr = htonl (INADDR_ANY);
  677. /* utilizam un port utilizator */
  678. server.sin_port = htons (PORT);
  679. /* atasam socketul */
  680. if (bind (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1){
  681. perror ("[server]Eroare la bind().\n");
  682. return errno;
  683. }
  684. /* punem serverul sa asculte daca vin clienti sa se conecteze */
  685. if (listen (sd, 2) == -1){
  686. perror ("[server]Eroare la listen().\n");
  687. return errno;
  688. }
  689. /* servim in mod concurent clientii...folosind thread-uri */
  690. while (1){
  691. int client;
  692. thData * td; //parametru functiei executata de thread
  693. int length = sizeof (from);
  694. printf ("[server]Asteptam la portul %d...\n",PORT);
  695. fflush (stdout);
  696. /* acceptam un client (stare blocanta pina la realizarea conexiunii) */
  697. if ( (client = accept (sd, (struct sockaddr *) &from, &length)) < 0){
  698. perror ("[server]Eroare la accept().\n");
  699. continue;
  700. }
  701. int idThread; //id-ul threadului
  702. int cl; //descriptorul intors de accept
  703. td=(struct thData*)malloc(sizeof(struct thData));
  704. td->idThread=i++;
  705. td->cl=client;
  706. th=(pthread_t*) realloc(th,i*sizeof(pthread_t));
  707. pthread_create(&th[i-1], NULL, &user,td);
  708. printf("descriptorul clientului este %d\n",client );
  709. }
  710. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement