Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <pthread.h>
- #include <sqlite3.h>
- int j=0;
- #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
- #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
- static int select_query(void *data,int argc, char **argv, char **azColName){
- if (argv==NULL)
- {return 0;}
- else
- {return 4;}
- }
- static int select_descriptor_query(void *data,int argc, char **argv, char **azColName){
- if (argv==NULL){
- *(int*)data=0;
- return 0;}
- else{
- *(int*)data=atoi(argv[1]);
- return 4;}
- }
- static int nothing_to_do_query(void *NotUsed, int argc, char **argv, char **azColName){
- return 0;
- }
- static int select_messages_query(void *descriptor_client, int argc, char **argv, char **azColName){
- j++;
- printf("%d\n",j);
- int i;
- char istoricul[1000]="";
- for(i=0; i<argc; i++){
- strcpy(istoricul,azColName[i]);
- strcat(istoricul," = ");
- if(argv[i]){
- strcat(istoricul,argv[i]);
- }
- else{
- strcat(istoricul,"NULL");
- }
- if(i==(argc-1)){
- strcat(istoricul,"\n\n");
- }
- if (write (*(int*)descriptor_client, istoricul,1000) <= 0){
- perror (" Eroare la write() catre client.\n");
- }
- }
- return 0;
- }
- #define PORT 2910
- extern int errno;
- typedef struct thData{
- int idThread; //id-ul thread-ului tinut in evidenta de acest program
- int cl; //descriptorul intors de accept
- }thData;
- char* citire(thData tdL){
- char* array=(char*)malloc(500);
- if(read (tdL.cl,array,500) <= 0)
- {
- printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
- }
- return array;
- }
- int scriere_un_int(thData tdL,int nr){
- fflush(stdout);
- if (write (tdL.cl, &nr, sizeof(int)) <= 0){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- return nr;
- }
- int log_inreg_decon(thData tdL){
- int nr=-1;
- fflush (stdout);
- if (read (tdL.cl, &nr,sizeof(int)) <= 0){
- printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
- close (tdL.cl);
- return(-1);
- }
- return scriere_un_int(tdL,nr);
- }
- static void *user(void * arg){
- char array[500],array1[600]="";
- char temporary_username[30]="";
- int nr;
- fflush (stdout);
- pthread_detach(pthread_self());
- struct thData tdL;
- tdL= *((struct thData*)arg);
- sqlite3 *db;
- char *zErrMsg = 0;
- int rc;
- char *sql;
- rc = sqlite3_open_v2("retele.db", &db,SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX,NULL);
- if(rc){
- fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
- exit(0);
- }
- else{
- fprintf(stderr, "Opened database successfully\n");
- }
- char* parola=(char*)malloc(30);
- char* login=(char*)malloc(30);
- while(1){
- nr=0;
- while(nr!=1 && nr!=2 && nr!=3 && nr!=4){
- nr=log_inreg_decon(tdL);
- if(nr==-1){
- close (((struct thData*)arg)->cl);
- return(NULL);
- }
- }
- if (nr==4){//a fost aleasa deconectarea
- close (((struct thData*)arg)->cl);
- return(NULL);
- }
- fflush (stdout);
- login=citire(tdL);
- parola=citire(tdL);
- if(nr==1 || nr ==3){//logarea sau stergerea contului
- sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("' AND password='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from USERS WHERE username='");
- strcat(sql,login);
- strcat(sql,"' AND password='");
- strcat(sql,parola);
- }
- else if(nr==2){//inregistrarea
- sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from USERS WHERE username='");
- strcat(sql,login);
- }
- strcat(sql,"'");
- printf("%s\n",sql );
- rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4 ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- rc=scriere_un_int(tdL,rc);
- if (nr==1){//a fost aleasa logarea
- if(rc==0){//logare nereusita
- if (read (tdL.cl, &nr,sizeof(int)) <= 0){
- printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
- }
- printf("daca vrem sa continuam sau nu %d\n",nr );
- if(nr==1){
- continue;
- }
- else{
- close (((struct thData*)arg)->cl);
- return(NULL);
- }
- }
- else if(rc==4){//logare reusita
- break;
- }
- }
- else if (nr==2){//a fost aleasa inregistrarea
- if (rc==0){//inregistrare reusita
- sql=(char*)realloc(sql,sizeof("INSERT into USERS(username,password) values('")+30+sizeof("','")+30+sizeof("')"));
- strcpy(sql,"INSERT into USERS(username,password) values('");
- strcat(sql,login);
- strcat(sql,"','");
- strcat(sql,parola);
- strcat(sql,"')");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, 0, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- else{
- fprintf(stdout, "Records created successfully\n");
- }
- }
- if (read (tdL.cl, &nr,sizeof(int)) <= 0){
- printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
- }
- if(nr==1){
- continue;
- }
- else{
- close (((struct thData*)arg)->cl);
- return(NULL);
- }
- }
- else if (nr==3){//a fost aleasa stergerea contului
- if (rc==4){//stergerea contului reusita
- sql=(char*)malloc(sizeof("DELETE from USERS WHERE username='")+30+sizeof("' AND password='")+30+sizeof("'"));
- strcpy(sql,"DELETE from USERS WHERE username='");
- strcat(sql,login);
- strcat(sql,"' AND password='");
- strcat(sql,parola);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- else{
- fprintf(stdout, "Records deleted successfully\n");
- }
- }
- if (read (tdL.cl, &nr,sizeof(int)) <= 0){
- printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
- return NULL;
- }
- if(nr==1){
- continue;
- }
- else{
- close (((struct thData*)arg)->cl);
- return(NULL);
- }
- }
- }
- sql=(char*)malloc(sizeof("SELECT * from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from CONNECTED_USERS WHERE username='");
- strcat(sql,login);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4 ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- if(rc==4){
- int descriptor_se_va_inchide;
- sql=(char*)malloc(sizeof("SELECT * from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from CONNECTED_USERS WHERE username='");
- strcat(sql,login);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, select_descriptor_query, (void*)&descriptor_se_va_inchide, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- 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){
- perror ("[client]Eroare la write() spre server.\n");
- close (((struct thData*)arg)->cl);
- return(NULL);
- }
- close(descriptor_se_va_inchide);
- sql=(char*)malloc(sizeof("DELETE from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"DELETE from CONNECTED_USERS WHERE username='");
- strcat(sql,login);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- else{
- fprintf(stdout, "Records deleted successfully\n");
- }
- }
- char buffer[33];
- sprintf(buffer,"%d",tdL.cl);
- sql=(char*)realloc(sql,sizeof("INSERT into CONNECTED_USERS(username,descriptor) values('")+30+sizeof("',")+sizeof(int)+sizeof(")"));
- strcpy(sql,"INSERT into CONNECTED_USERS(username,descriptor) values('");
- strcat(sql,login);
- strcat(sql,"',");
- strcat(sql,buffer);
- strcat(sql,")");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, 0, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- else{
- fprintf(stdout, "Records created successfully\n");
- }
- int user_cu_cine_vb=-1,online=-1;
- char nume_cu_cine_vb[30]="";
- 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"));
- strcpy(sql,"SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES,USERS WHERE USERNAME='");
- strcat(sql,login);
- strcat(sql,"' AND TO_USER=USERNAME AND TIME_SENT>LAST_LOG_OUT");
- rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4 ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- rc=scriere_un_int(tdL,rc);
- if (rc==4){
- if(read (tdL.cl,&nr,sizeof(int)) <= 0){
- printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
- return NULL;
- }
- if(nr==1){
- rc = sqlite3_exec(db, sql, select_messages_query, (void*)&tdL.cl, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4 ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- }
- }
- while(1){
- if(read (tdL.cl,array,500) <= 0){
- printf("[Thread %d]Eroare la read() de la client cu descriptor %d.\n",tdL.idThread,tdL.cl);
- break;
- }
- if(strcmp(array,".logout")==0){//urmeaza deconectarea
- break;
- }
- else if(strcmp(array,".delete")==0){//urmeaza stergerea contului
- sql=(char*)malloc(sizeof("DELETE from USERS WHERE username='")+30+sizeof("' AND password='")+30+sizeof("'"));
- strcpy(sql,"DELETE from USERS WHERE username='");
- strcat(sql,login);
- strcat(sql,"' AND password='");
- strcat(sql,parola);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- else{
- fprintf(stdout, "Records deleted successfully\n");
- if (write (tdL.cl, "Stergerea contului a fost efectuata cu succes\n", sizeof("Stergerea contului a fost efectuata cu succes\n")) <= 0){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- }
- break;
- }
- else if (strcmp(array,".meniu")==0){//se afiseaza lista de shortcut-uri si instrumente ale aplicatiei
- continue;
- }
- else if (strcmp(array,".history")==0){//se afiseaza tot istoricul userului
- 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("'"));
- strcpy(sql,"SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES WHERE TO_USER='");
- strcat(sql,login);
- strcat(sql,"' OR FROM_USER='");
- strcat(sql,login);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, select_messages_query, (void*)&tdL.cl, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- continue;
- }
- else if (array[0]=='@'){//mesajul incepe cu @, se presupune ca se alege user nou cu care se va vorbi
- char*ret;
- ret = strrchr(array,'@');
- char username[30]="";
- if(strcmp(ret,"@")==0 && array[1]!='\0'){//se alege user nou cu care se va vorbi
- int user_posibil_cu_cine_vb=0;
- int i=1;
- while(strcmp(array+i,"@")!=0){
- strncat(username,(array+i),1);
- i++;
- }
- printf("Username ales va fi %s\n",username);
- fflush(stdout);
- sql=(char*)malloc(sizeof("SELECT * from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from CONNECTED_USERS WHERE username='");
- strcat(sql,username);
- strcat(sql,"'");
- strcpy(nume_cu_cine_vb,username);
- rc = sqlite3_exec(db, sql, select_descriptor_query, (void*)&user_posibil_cu_cine_vb, &zErrMsg);
- rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- if(user_posibil_cu_cine_vb!=0){
- fflush(stdout);
- if (write (tdL.cl, "Userul ales este online\n", sizeof("Userul ales este online\n")) <= 0){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- user_cu_cine_vb=user_posibil_cu_cine_vb;
- online=1;
- fflush(stdout);
- }
- else{
- sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from USERS WHERE username='");
- strcat(sql,username);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- if(rc==4){
- online=0;
- user_cu_cine_vb=0;
- fflush(stdout);
- if (write (tdL.cl, "Userul ales este offline\n", sizeof("Userul ales este offline\n")) <= 0){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- }
- else{
- 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){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- strcpy(nume_cu_cine_vb,"");
- user_cu_cine_vb=-1;
- online=-1;
- }
- }
- continue;
- }
- }
- 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
- char * nume_din_istoric=array+5;
- char*ret;
- ret = strrchr(nume_din_istoric,'@');
- char username[30]="";
- if(strcmp(ret,"@")==0 && array[6]!='\0'){//se alege user nou cu care se va vorbi
- int user_posibil_cu_cine_vb=0;
- int i=1;
- while((nume_din_istoric+i)[0]!='@' && (nume_din_istoric+i+1)!=NULL){
- strncat(username,(nume_din_istoric+i),1);
- i++;
- }
- sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from USERS WHERE username='");
- strcat(sql,username);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- if(rc==4){
- fflush(stdout);
- 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("')"));
- strcpy(sql,"SELECT FROM_USER 'DE LA',TO_USER 'CATRE',TIME_SENT 'LA ORA',MESSAGE 'MESAJ' from MESSAGES WHERE (TO_USER='");
- strcat(sql,login);
- strcat(sql,"' AND FROM_USER='");
- strcat(sql,username);
- strcat(sql,"') or (TO_USER='");
- strcat(sql,username);
- strcat(sql,"' AND FROM_USER='");
- strcat(sql,login);
- strcat(sql,"')");
- rc = sqlite3_exec(db, sql, select_messages_query, (void*)&tdL.cl, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- }
- else{
- 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){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- }
- continue;
- }
- }
- 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
- bzero(array1,sizeof(array1));
- int poz_delim=strcspn(array,">");
- int poz_delim_1=strcspn(array+poz_delim+1,">");
- if(poz_delim<(sizeof(array)-1) && poz_delim>1 && array[poz_delim+1]=='<' && array[poz_delim+2]!='>' && poz_delim_1<(sizeof(array)-1)){
- char username[30]="";
- int i=1;
- while(i<poz_delim){
- strncat(username,(array+i),1);
- i++;
- }
- sql=(char*)malloc(sizeof("SELECT * from USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"SELECT * from USERS WHERE username='");
- strcat(sql,username);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, select_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- if(rc==4){
- fflush(stdout);
- char numar_secunde[30]="";
- i=poz_delim+2;
- while(array[i]!='>'){
- strncat(numar_secunde,(array+i),1);
- i++;
- }
- sql=(char*)malloc(sizeof("SELECT * from MESSAGES WHERE FROM_USER='")+30+sizeof("' AND TIME_SENT=")+30);
- strcpy(sql,"SELECT * from MESSAGES WHERE FROM_USER='");
- strcat(sql,username);
- strcat(sql,"' AND TIME_SENT=");
- strcat(sql,numar_secunde);
- rc = sqlite3_exec(db, sql, select_query, (void*)&tdL.cl, &zErrMsg);
- if( rc != SQLITE_OK && rc!=4){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- if(rc==4){
- strcpy(array,(array+poz_delim_1+poz_delim+2));
- fflush(stdout);
- strcat(array1,"(Raspuns pentru mesaj trimis de ");
- strcat(array1,"<");
- strcat(array1,username);
- strcat(array1,"> la ");
- strcat(array1,"<");
- strcat(array1,numar_secunde);
- strcat(array1,"> )");
- strcat(array1,array);
- strcpy(temporary_username,username);
- }
- else{
- 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){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- continue;
- }
- }
- else{
- 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){
- printf("[Thread %d] ",tdL.idThread);
- perror (" Eroare la write() catre client.\n");
- }
- continue;
- }
- }
- }
- if(user_cu_cine_vb!=-1 || strcmp(array1,"\0")!=0){
- //se trimite la user descriptorul caruia se pastreaza in user_cu_cine_vb
- time_t timer;
- time(&timer);
- char buffer[33];
- sprintf(buffer,"%d",timer);
- if(online==1){
- char mesaj[532]="";
- strcpy(mesaj,"<");
- strcat(mesaj,login);
- strcat(mesaj,">");
- strcat(mesaj,array);
- strcat(mesaj,"<");
- strcat(mesaj,buffer);
- strcat(mesaj,">");
- if(write(user_cu_cine_vb,mesaj,532)<=0){
- perror ("[client]Eroare la write() spre alt client.\n");
- exit;
- }
- }
- if (strcmp(array1,"\0")!=0){
- strcpy(array,array1);
- strcpy(array1,"\0");
- }
- if (strcmp(temporary_username,"\0")!=0){
- char aux[30]="";
- strcpy(aux,nume_cu_cine_vb);
- strcpy(nume_cu_cine_vb,temporary_username);
- strcpy(temporary_username,aux);
- }
- sql=(char*)malloc(sizeof("INSERT into MESSAGES(from_user,to_user,time_sent,message) values('")+30+sizeof("','")+30+sizeof("',")+sizeof(int)+sizeof(",' ")+500+sizeof(" ')"));
- strcpy(sql,"INSERT into MESSAGES(from_user,to_user,time_sent,message) values('");
- strcat(sql,login);
- strcat(sql,"','");
- strcat(sql,nume_cu_cine_vb);
- strcat(sql,"',");
- strcat(sql,buffer);
- strcat(sql,",' ");
- strcat(sql,array);
- strcat(sql," ')");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, 0, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- if (strcmp(temporary_username,"\0")!=0){
- char aux[30]="";
- strcpy(aux,nume_cu_cine_vb);
- strcpy(nume_cu_cine_vb,temporary_username);
- strcpy(temporary_username,aux);
- }
- fflush(stdout);
- }
- else{
- 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){
- perror ("[client]Eroare la write() spre alt client.\n");
- exit;
- }
- }
- }
- close (((struct thData*)arg)->cl);
- sql=(char*)malloc(sizeof("DELETE from CONNECTED_USERS WHERE username='")+30+sizeof("'"));
- strcpy(sql,"DELETE from CONNECTED_USERS WHERE username='");
- strcat(sql,login);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- else{
- fprintf(stdout, "Records deleted successfully\n");
- }
- time_t timer;
- time(&timer);
- sprintf(buffer,"%d",timer);
- sql=(char*)malloc(sizeof("UPDATE USERS set LAST_LOG_OUT = ")+33+sizeof( " WHERE username='")+30+sizeof("'"));
- strcpy(sql,"UPDATE USERS set LAST_LOG_OUT = ");
- strcat(sql,buffer);
- strcat(sql," WHERE username='");
- strcat(sql,login);
- strcat(sql,"'");
- rc = sqlite3_exec(db, sql, nothing_to_do_query, NULL, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- }
- else{
- fprintf(stdout, "Records updated successfully\n");
- }
- return(NULL);
- };
- int main ()
- {
- struct sockaddr_in server; // structura folosita de server
- struct sockaddr_in from;
- int sd; //descriptorul de socket
- int pid;
- pthread_t *th=NULL; //Identificatorii thread-urilor care se vor crea
- int i=0;
- /* crearea unui socket */
- if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1){
- perror ("[server]Eroare la socket().\n");
- return errno;
- }
- /* utilizarea optiunii SO_REUSEADDR */
- int on=1;
- setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
- /* pregatirea structurilor de date */
- bzero (&server, sizeof (server));
- bzero (&from, sizeof (from));
- /* umplem structura folosita de server */
- /* stabilirea familiei de socket-uri */
- server.sin_family = AF_INET;
- /* acceptam orice adresa */
- server.sin_addr.s_addr = htonl (INADDR_ANY);
- /* utilizam un port utilizator */
- server.sin_port = htons (PORT);
- /* atasam socketul */
- if (bind (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1){
- perror ("[server]Eroare la bind().\n");
- return errno;
- }
- /* punem serverul sa asculte daca vin clienti sa se conecteze */
- if (listen (sd, 2) == -1){
- perror ("[server]Eroare la listen().\n");
- return errno;
- }
- /* servim in mod concurent clientii...folosind thread-uri */
- while (1){
- int client;
- thData * td; //parametru functiei executata de thread
- int length = sizeof (from);
- printf ("[server]Asteptam la portul %d...\n",PORT);
- fflush (stdout);
- /* acceptam un client (stare blocanta pina la realizarea conexiunii) */
- if ( (client = accept (sd, (struct sockaddr *) &from, &length)) < 0){
- perror ("[server]Eroare la accept().\n");
- continue;
- }
- int idThread; //id-ul threadului
- int cl; //descriptorul intors de accept
- td=(struct thData*)malloc(sizeof(struct thData));
- td->idThread=i++;
- td->cl=client;
- th=(pthread_t*) realloc(th,i*sizeof(pthread_t));
- pthread_create(&th[i-1], NULL, &user,td);
- printf("descriptorul clientului este %d\n",client );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement