diff --git a/11.client-server-exam-database (COMPLETE)/archivio.txt b/11.client-server-exam-database (COMPLETE)/archivio.txt new file mode 100755 index 0000000..80f8464 --- /dev/null +++ b/11.client-server-exam-database (COMPLETE)/archivio.txt @@ -0,0 +1,7 @@ +MARIO 30 127.0.0.1:37968 +FONZO 15 127.0.0.1:37968 +CARLO 14 127.0.0.1:37981 +MARIO 1950 127.0.0.1:38094 +ARTUR 150 127.0.0.1:38099 +KOMBAT 150 127.0.0.1:38099 + diff --git a/11.client-server-exam-database (COMPLETE)/archivio.txt~ b/11.client-server-exam-database (COMPLETE)/archivio.txt~ new file mode 100755 index 0000000..5b0ab62 --- /dev/null +++ b/11.client-server-exam-database (COMPLETE)/archivio.txt~ @@ -0,0 +1,7 @@ +MARIO 30 127.0.0.1:37968 +FONZO 15 127.0.0.1:37968 +CARLO 14 127.0.0.1:37981 +MARIO 1950 127.0.0.1:38094 +ARTUR 150 127.0.0.1:38099 +KOMBAT 150 127.0.0.1:38099 +ione fornita da 127.0.0.1:38099 150 127.0.0.1:38099 diff --git a/11.client-server-exam-database (COMPLETE)/client b/11.client-server-exam-database (COMPLETE)/client new file mode 100755 index 0000000..561684d Binary files /dev/null and b/11.client-server-exam-database (COMPLETE)/client differ diff --git a/11.client-server-exam-database (COMPLETE)/daytimecli.c b/11.client-server-exam-database (COMPLETE)/daytimecli.c new file mode 100644 index 0000000..e182339 --- /dev/null +++ b/11.client-server-exam-database (COMPLETE)/daytimecli.c @@ -0,0 +1,136 @@ +#include "../basic.h" +#include + +void sigpipeaction (int signal){ + + + //fprintf(stderr, "Sigpipe catturato\n"); + + +} + + +typedef struct message { + char data [50]; + char nome [50]; + char voto [50]; +}t_message; + +int main(int argc, char **argv) { + + + + struct sigaction sigpipestructure; + sigpipestructure.sa_handler = sigpipeaction; + sigpipestructure.sa_flags = 0; + + sigaction (SIGPIPE, &sigpipestructure, NULL); + + + + + + int sockfd; + struct sockaddr_in servaddr; + + if (argc != 3){ + printf("usage: daytimecli \n"); + exit(0); +} + if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){ + printf("socket error\n"); + exit(0); +} + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(atoi(argv[2])); /* server port */ + + if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){ + printf("inet_pton error for %s\n", argv[1]); + exit(0); +} + if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){ + printf("connect error\n"); + exit(0); + } + + + int ridden; + int written; + t_message m; + + + + //// welcome from server - first message + for (ridden = read (sockfd,&m,sizeof(m)); ridden <= 0;ridden = read (sockfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {exit(0);} + } + fprintf(stdout,"Server: %s\n",m.data);// answer from server + + char line [1000]; + char line_data [50]; + char line_nome [50]; + char line_voto [50]; + for(; ;){ + fprintf(stdout, "Inserisci comando: "); + fscanf (stdin,"%999[^\n]%*c",line); + sscanf (line, "%s", line_data); + if (strcmp("AGGIUNGI",line_data) == 0 || strcmp("VERIFICA",line_data) == 0){ + + if (strcmp("AGGIUNGI",line_data) == 0 ){ + sprintf(m.data,"AGGIUNGI"); + sprintf(line_nome,"def"); + sprintf(line_voto,"def"); + + sscanf (line, "%*s %s %s", line_nome,line_voto); + if (strcmp(line_nome,"def") == 0 || strcmp(line_voto,"def") == 0){ + fprintf(stdout,"Comando invalido\n"); + continue; + }else{ + sprintf(m.nome,"%s",line_nome); + sprintf(m.voto,"%s",line_voto); + } + + } else { + sprintf(m.data,"VERIFICA"); + sprintf(line_nome,"def"); + sscanf (line, "%*s %s ", line_nome); + if (strcmp(line_nome,"def") == 0 ){ + fprintf(stdout,"Comando invalido\n"); + continue; + }else{ + sprintf(m.nome,"%s",line_nome); + } + + } + + for (written = write (sockfd, &m, sizeof (m) ); written == -1; written = write (sockfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + for (ridden = read (sockfd,&m,sizeof(m)); ridden <= 0;ridden = read (sockfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {exit(0);} + } + fprintf(stdout,"Server: %s\n",m.data);// answer from server + + + } else { + sprintf(m.data,"Bye"); + + for (written = write (sockfd, &m, sizeof (m) ); written == -1; written = write (sockfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + + for (ridden = read (sockfd,&m,sizeof(m)); ridden <= 0;ridden = read (sockfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {exit(0);} + } + + fprintf(stdout,"Server: %s\n",m.data);// answer from server + break; + + } + } + + + + exit(0); +} diff --git a/11.client-server-exam-database/daytimecli.c~ b/11.client-server-exam-database (COMPLETE)/daytimecli.c~ similarity index 100% rename from 11.client-server-exam-database/daytimecli.c~ rename to 11.client-server-exam-database (COMPLETE)/daytimecli.c~ diff --git a/11.client-server-exam-database (COMPLETE)/daytimecli.o b/11.client-server-exam-database (COMPLETE)/daytimecli.o new file mode 100644 index 0000000..12ce34c Binary files /dev/null and b/11.client-server-exam-database (COMPLETE)/daytimecli.o differ diff --git a/11.client-server-exam-database/daytimesrv.c b/11.client-server-exam-database (COMPLETE)/daytimesrv.c similarity index 62% rename from 11.client-server-exam-database/daytimesrv.c rename to 11.client-server-exam-database (COMPLETE)/daytimesrv.c index 1390bd6..7b67c93 100644 --- a/11.client-server-exam-database/daytimesrv.c +++ b/11.client-server-exam-database (COMPLETE)/daytimesrv.c @@ -17,6 +17,13 @@ void sigchldaction (int signal){ } +void sigpipeaction (int signal){ + + //fprintf(stderr, "Sigpipe catturato\n"); + +} + + typedef struct message { @@ -27,13 +34,22 @@ typedef struct message { int main(int argc, char **argv) { - +//////////////////////////////////////////////////////////// SIGNALS +///////SIGCHLD struct sigaction sigchldstructure; sigchldstructure.sa_handler = sigchldaction; sigchldstructure.sa_flags = 0; sigaction (SIGCHLD, &sigchldstructure, NULL); +///////SIGPIPE +struct sigaction sigpipestructure; + sigpipestructure.sa_handler = sigpipeaction; + sigpipestructure.sa_flags = 0; + +sigaction (SIGPIPE, &sigpipestructure, NULL); +//////////////////////////////////////////////////////////// END SIGNALS + int listenfd, connfd, n; struct sockaddr_in servaddr; @@ -87,13 +103,14 @@ sigaction (SIGCHLD, &sigchldstructure, NULL); close(listenfd); t_message m; // buffer message structure - int ridden, wrote; + int ridden, written; sprintf ( m.data, "Benvenuto" ); - wrote = write (connfd, &m, sizeof (m) ); // scrivo benvenuto sul socket - // check wrote - - + //write with error check + for (written = write (connfd, &m, sizeof (m) ); written == -1; written = write (connfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + inet_ntop(AF_INET, &(client_addr.sin_addr), buff, INET_ADDRSTRLEN); // get client address port = ntohs(client_addr.sin_port);// get client port @@ -101,8 +118,10 @@ sigaction (SIGCHLD, &sigchldstructure, NULL); for ( ; ; ) {// the server child reserve other requests from same client - ridden = read (connfd,&m,sizeof(m)); - // check read. it's blocking + + for (ridden = read (connfd,&m,sizeof(m)); ridden <= 0;ridden = read (connfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {exit(0);} + } int fd; @@ -111,29 +130,33 @@ sigaction (SIGCHLD, &sigchldstructure, NULL); if (fd < 0){ fd = open ("archivio.txt", O_CREAT | O_RDWR | O_APPEND ,00700); } - if (fd < 0){ - sprintf ( m.data, "Non posso modificare il database" ); - wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket - // check wrote - } else { + if (fd < 0){ + sprintf ( m.data, "Non posso modificare il database" ); + for (written = write (connfd, &m, sizeof (m) ); written == -1; written = write (connfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + } else { + // local writes. no checks write (fd,(m.nome), strlen ((m.nome))); write (fd," ", 1); write (fd,(m.voto), strlen ((m.voto)) ); write (fd," ", 1); write (fd,buff, strlen (buff)); write (fd,":", 1); - + char port_char[50]; sprintf (port_char,"%hu",port); write (fd,port_char, strlen (port_char)); write (fd,"\n", 1); - - + + sprintf ( m.data, "AGGIUNTO" ); - wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket - // check wrote - + for (written = write (connfd, &m, sizeof (m) ); written == -1; written = write (connfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + + close(fd); } } else { if (strcmp (m.data, "VERIFICA" ) == 0 ){ @@ -141,8 +164,9 @@ sigaction (SIGCHLD, &sigchldstructure, NULL); fd = open ("archivio.txt",O_RDONLY); if (fd < 0){ sprintf ( m.data, "Non posso leggere il database" ); - wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket - // check wrote + for (written = write (connfd, &m, sizeof (m) ); written == -1; written = write (connfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } } else { char line [1000]; @@ -154,7 +178,7 @@ sigaction (SIGCHLD, &sigchldstructure, NULL); found = 0; FILE * fdfile = fdopen(fd,"r"); returned = fscanf (fdfile,"%999[^\n]%*c",line); - for( ; returned > 0 ; fscanf (fdfile,"%999[^\n]%*c",line) ){ + for( ; returned > 0 ; returned = fscanf (fdfile,"%999[^\n]%*c",line) ){ sscanf (line, "%s %s %s", line_nome, line_voto, line_ip_port); @@ -162,8 +186,9 @@ sigaction (SIGCHLD, &sigchldstructure, NULL); found = 1; sprintf ( m.data, "%s ha sostenuto l'esame con voto %s. Informazione fornita da %s ", line_nome, line_voto, line_ip_port ); - wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket - // check wrote + for (written = write (connfd, &m, sizeof (m) ); written == -1; written = write (connfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } break; } @@ -171,17 +196,20 @@ sigaction (SIGCHLD, &sigchldstructure, NULL); } if (found == 0){ - sprintf ( m.data, "%s non ha sostenuto l'esame", &(m.nome) ); - wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket - // check wrote + sprintf ( m.data, "%s non ha sostenuto l'esame", m.nome ); + for (written = write (connfd, &m, sizeof (m) ); written == -1; written = write (connfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } } - } + close(fd); + } } else { sprintf ( m.data, "Bye" ); - wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket - // check wrote + for (written = write (connfd, &m, sizeof (m) ); written == -1; written = write (connfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } break; // end requests from client diff --git a/11.client-server-exam-database/daytimesrv.c~ b/11.client-server-exam-database (COMPLETE)/daytimesrv.c~ similarity index 100% rename from 11.client-server-exam-database/daytimesrv.c~ rename to 11.client-server-exam-database (COMPLETE)/daytimesrv.c~ diff --git a/11.client-server-exam-database (COMPLETE)/daytimesrv.o b/11.client-server-exam-database (COMPLETE)/daytimesrv.o new file mode 100644 index 0000000..1843690 Binary files /dev/null and b/11.client-server-exam-database (COMPLETE)/daytimesrv.o differ diff --git a/11.client-server-exam-database/makefile b/11.client-server-exam-database (COMPLETE)/makefile similarity index 100% rename from 11.client-server-exam-database/makefile rename to 11.client-server-exam-database (COMPLETE)/makefile diff --git a/11.client-server-exam-database/makefile~ b/11.client-server-exam-database (COMPLETE)/makefile~ similarity index 100% rename from 11.client-server-exam-database/makefile~ rename to 11.client-server-exam-database (COMPLETE)/makefile~ diff --git a/11.client-server-exam-database (COMPLETE)/readme b/11.client-server-exam-database (COMPLETE)/readme new file mode 100644 index 0000000..0cf2403 --- /dev/null +++ b/11.client-server-exam-database (COMPLETE)/readme @@ -0,0 +1,16 @@ + +SONO STATI CALCOALTI ERRORI SU READ,WRITE,CONNECT,SIGNALS +IL SERVER E' RICORSIVO E LE READ/WRITE SONO BLOCCANTI (SENZA SELECT) +PROTOCOLLO USATO TCP + +il server gestisce un file archivio.txt + +il client puo inviare: +BYE - per chiudere la connessione +AGGIUNGI - per far aggiungere un nome al server +VERIFICA - per verificare se un nome è presente nel database del server + +il server risponde: +BYE - A BYE +AGGIUNTO - AL COMANDO AGGIUNGI DEL CLIENT +EVENTUALI INFORMAZIONI SUI DATI - AL COMADNO VERIFICA diff --git a/11.client-server-exam-database (COMPLETE)/readme~ b/11.client-server-exam-database (COMPLETE)/readme~ new file mode 100644 index 0000000..fa0ff7c --- /dev/null +++ b/11.client-server-exam-database (COMPLETE)/readme~ @@ -0,0 +1,14 @@ + +SONO STATI CALCOALTI ERRORI SU READ,WRITE,CONNECT,SIGNALS + +il server gestisce un file archivio.txt + +il client puo inviare: +BYE - per chiudere la connessione +AGGIUNGI - per far aggiungere un nome al server +VERIFICA - per verificare se un nome è presente nel database del server + +il server risponde: +BYE - A BYE +AGGIUNTO - AL COMANDO AGGIUNGI DEL CLIENT +EVENTUALI INFORMAZIONI SUI DATI - AL COMADNO VERIFICA diff --git a/11.client-server-exam-database (COMPLETE)/server b/11.client-server-exam-database (COMPLETE)/server new file mode 100755 index 0000000..24a4eb3 Binary files /dev/null and b/11.client-server-exam-database (COMPLETE)/server differ diff --git a/11.client-server-exam-database/client b/11.client-server-exam-database/client deleted file mode 100755 index 769765d..0000000 Binary files a/11.client-server-exam-database/client and /dev/null differ diff --git a/11.client-server-exam-database/daytimecli.c b/11.client-server-exam-database/daytimecli.c deleted file mode 100644 index f169889..0000000 --- a/11.client-server-exam-database/daytimecli.c +++ /dev/null @@ -1,106 +0,0 @@ -#include "../basic.h" -#include - -void sigpipeaction (int signal){ - - int status, pid; - - fprintf(stderr, "Sigpipe catturato\n"); - - -} - - -int main(int argc, char **argv) { - - - - struct sigaction sigpipestructure; - sigpipestructure.sa_handler = sigpipeaction; - sigpipestructure.sa_flags = 0; - - sigaction (SIGPIPE, &sigpipestructure, NULL); - - - - siginterrupt (SIGPIPE,1); - - - - - int sockfd; - struct sockaddr_in servaddr; - - if (argc != 3){ - printf("usage: daytimecli \n"); - exit(0); -} - if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){ - printf("socket error\n"); - exit(0); -} - bzero(&servaddr, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(atoi(argv[2])); /* server port */ - - if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){ - printf("inet_pton error for %s\n", argv[1]); - exit(0); -} - if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){ - printf("connect error\n"); - exit(0); - } - - struct sockaddr_in r_server_local_addr; - unsigned int r_server_local_addr_len = sizeof(r_server_local_addr); - - char * socket_buffer = (char *) malloc (r_server_local_addr_len) ; - int ridden; - int written; - - - sleep(5); - - int i; - for ( i = 0; i < 5; i ++){ - - ridden = read (sockfd, socket_buffer,r_server_local_addr_len ); - if (ridden < 0){ - fprintf(stdout,"Errore durante la lettura\n"); - exit(-1); - } else { - fprintf(stdout,"Ho letto %d bytes\n",ridden); - } - } - - for ( i = 0; i < 1; i ++){ - - written = write (sockfd, socket_buffer,r_server_local_addr_len /2 ); - if (written < 0){ - fprintf(stdout,"Errore durante la scrittura\n"); - exit(-1); - } else { - fprintf(stdout,"Ho scritto %d bytes\n",written); - } - } - - - - - - memcpy (&r_server_local_addr,socket_buffer, r_server_local_addr_len); - - - char buff[INET_ADDRSTRLEN]; - short port; - - inet_ntop(AF_INET, &(r_server_local_addr.sin_addr), buff, INET_ADDRSTRLEN);// get ip - port = ntohs(r_server_local_addr.sin_port);// get port - - fprintf (stdout,"%s%c%hu\n",buff,':',port); - - - - exit(0); -} diff --git a/11.client-server-exam-database/daytimecli.o b/11.client-server-exam-database/daytimecli.o deleted file mode 100644 index 459d3bd..0000000 Binary files a/11.client-server-exam-database/daytimecli.o and /dev/null differ diff --git a/11.client-server-exam-database/daytimesrv.o b/11.client-server-exam-database/daytimesrv.o deleted file mode 100644 index c157435..0000000 Binary files a/11.client-server-exam-database/daytimesrv.o and /dev/null differ diff --git a/11.client-server-exam-database/readme b/11.client-server-exam-database/readme deleted file mode 100644 index e69de29..0000000 diff --git a/11.client-server-exam-database/readme~ b/11.client-server-exam-database/readme~ deleted file mode 100644 index e69de29..0000000 diff --git a/11.client-server-exam-database/server b/11.client-server-exam-database/server deleted file mode 100755 index 0e3995c..0000000 Binary files a/11.client-server-exam-database/server and /dev/null differ diff --git a/fun-corso-reti.o b/fun-corso-reti.o new file mode 100644 index 0000000..5b8cdd9 Binary files /dev/null and b/fun-corso-reti.o differ diff --git a/test/test2 b/test/test2 new file mode 100755 index 0000000..673113f Binary files /dev/null and b/test/test2 differ diff --git a/test/test2.c b/test/test2.c new file mode 100644 index 0000000..802abaa --- /dev/null +++ b/test/test2.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include + + +int main (int index, char** in_data){ + +char a [100]; +char b; +char c [100]; +char d [100]; +sprintf(d,"eeee"); +sprintf(a,"alfred "); +sscanf (a,"%s%s",c,d); +fprintf(stdout,"%s\n",c); +fprintf(stdout,"%s\n",d); + + + return 0; +} diff --git a/test/test2.c~ b/test/test2.c~ new file mode 100644 index 0000000..522e43e --- /dev/null +++ b/test/test2.c~ @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include + + +int main (int index, char** in_data){ + +char a [100]; +char b; +char c [100]; +char d [100]; +sprint(d,"eeee"); +sprintf(a,"alfred "); +sscanf (a,"%s%s",c,d); +fprintf(stdout,"%s\n",c); +fprintf(stdout,"%s\n",d); + + + return 0; +}