diff --git a/11.UPD.client-server-exam-database (COMPLETE)/archivio.txt b/11.UPD.client-server-exam-database (COMPLETE)/archivio.txt new file mode 100755 index 0000000..abdac7e --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/archivio.txt @@ -0,0 +1,8 @@ +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 +CAZZO 70 127.0.0.1:43589 +MARIOPANE 42 127.0.0.1:43589 diff --git a/11.UPD.client-server-exam-database (COMPLETE)/archivio.txt~ b/11.UPD.client-server-exam-database (COMPLETE)/archivio.txt~ new file mode 100755 index 0000000..0f37b98 --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/archivio.txt~ @@ -0,0 +1,8 @@ +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 + +CAZZO 70 127.0.0.1:43589 diff --git a/11.UPD.client-server-exam-database (COMPLETE)/client b/11.UPD.client-server-exam-database (COMPLETE)/client new file mode 100755 index 0000000..32fd581 Binary files /dev/null and b/11.UPD.client-server-exam-database (COMPLETE)/client differ diff --git a/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.c b/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.c new file mode 100644 index 0000000..c72d250 --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.c @@ -0,0 +1,148 @@ +#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]; + struct sockaddr_in addresses; + unsigned int addresses_len; +}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_DGRAM, 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; + m.addresses_len = sizeof (m.addresses); + + // get local address after connect + getsockname(sockfd, (struct sockaddr *) &(m.addresses) , &(m.addresses_len) ); + // getpeername per ottenere invece informazioni dall'altro lato di una connessione + + // send local address to the server + for (written = write (sockfd, &m, sizeof (m) ); written == -1; written = write (sockfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + + + + //// 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.UPD.client-server-exam-database (COMPLETE)/daytimecli.c~ b/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.c~ new file mode 100644 index 0000000..239a866 --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.c~ @@ -0,0 +1,31 @@ +/* Client che interroga un daytime server */ + +#include "../basic.h" + +int main(int argc, char **argv) { + int sockfd, n; + char recvline[MAXLINE + 1]; + 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); + } + exit(0); +} diff --git a/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.o b/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.o new file mode 100644 index 0000000..2546125 Binary files /dev/null and b/11.UPD.client-server-exam-database (COMPLETE)/daytimecli.o differ diff --git a/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.c b/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.c new file mode 100644 index 0000000..bd4c1a1 --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.c @@ -0,0 +1,207 @@ + + +#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]; + struct sockaddr_in addresses; + unsigned int addresses_len; +}t_message; + +int main(int argc, char **argv) { + +//////////////////////////////////////////////////////////// SIGNALS + +///////SIGPIPE +struct sigaction sigpipestructure; + sigpipestructure.sa_handler = sigpipeaction; + sigpipestructure.sa_flags = 0; + +sigaction (SIGPIPE, &sigpipestructure, NULL); +//////////////////////////////////////////////////////////// END SIGNALS + + + int listenfd,n; + struct sockaddr_in servaddr; + char buff[INET_ADDRSTRLEN]; // client ip grabbed redeable + short port; // client port redeable + + if (argc != 2 ){ + printf("usage: daytimesrv \n"); + exit(0); + } + if( (listenfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ + printf("socket error\n"); + exit(0); + } + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard address */ + servaddr.sin_port = htons(atoi(argv[1])); /* server port */ + + if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0){ + printf("bind error\n"); exit(0); + } + + + t_message m; // buffer message structure + int ridden, written; + + + for (;;){ + + + + // read address from client + for (ridden = read (listenfd,&m,sizeof(m)); ridden <= 0;ridden = read (listenfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {exit(0);} + } + + + struct sockaddr_in client_addr; + unsigned int client_addr_len = sizeof(client_addr); + + client_addr = m.addresses; + + if (connect(listenfd, (struct sockaddr *) &client_addr, client_addr_len) < 0){ + printf("connect error\n"); + exit(0); + } + + + + + sprintf ( m.data, "Benvenuto" ); + //write with error check + for (written = write (listenfd, &m, sizeof (m) ); written == -1; written = write (listenfd, &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 + //fprintf (stdout,"%s%c%hu\n",buff,':',port); // print client address and port + + + for ( ; ; ) {// the server child reserve other requests from same client + + for (ridden = read (listenfd,&m,sizeof(m)); ridden <= 0;ridden = read (listenfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {exit(0);} + } + + int fd; + + if (strcmp (m.data, "AGGIUNGI" ) == 0 ){ + fd = open ("archivio.txt",O_RDWR | O_APPEND); + 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" ); + for (written = write (listenfd, &m, sizeof (m) ); written == -1; written = write (listenfd, &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" ); + for (written = write (listenfd, &m, sizeof (m) ); written == -1; written = write (listenfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + + close(fd); + } + } else { + if (strcmp (m.data, "VERIFICA" ) == 0 ){ + + fd = open ("archivio.txt",O_RDONLY); + if (fd < 0){ + sprintf ( m.data, "Non posso leggere il database" ); + for (written = write (listenfd, &m, sizeof (m) ); written == -1; written = write (listenfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + } else { + + char line [1000]; + char line_nome [50]; + char line_voto [50]; + char line_ip_port [50]; + int returned,found; + + found = 0; + FILE * fdfile = fdopen(fd,"r"); + returned = 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); + + if (strcmp (line_nome, m.nome) == 0){ + found = 1; + + sprintf ( m.data, "%s ha sostenuto l'esame con voto %s. Informazione fornita da %s ", line_nome, line_voto, line_ip_port ); + for (written = write (listenfd, &m, sizeof (m) ); written == -1; written = write (listenfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + + break; + } + + } + + if (found == 0){ + sprintf ( m.data, "%s non ha sostenuto l'esame", m.nome ); + for (written = write (listenfd, &m, sizeof (m) ); written == -1; written = write (listenfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + } + close(fd); + } + + } else { + + sprintf ( m.data, "Bye" ); + for (written = write (listenfd, &m, sizeof (m) ); written == -1; written = write (listenfd, &m, sizeof (m)) ){ + if (errno == EINTR){continue;} else {exit(0);} + } + m.addresses.sin_family = AF_UNSPEC; + connect(listenfd, (struct sockaddr *) &m.addresses, sizeof(m.addresses)); + break; // end requests from client + + } + } + + } + + + } +} diff --git a/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.c~ b/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.c~ new file mode 100644 index 0000000..5a71bf1 --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.c~ @@ -0,0 +1,41 @@ +/* stampa l'ip del client */ + +#include "../basic.h" +#include + +int main(int argc, char **argv) { + int listenfd, connfd, n; + struct sockaddr_in servaddr; + char buff[MAXLINE]; + time_t ticks; + + if (argc != 2 ){ + printf("usage: daytimesrv \n"); + exit(0); +} + if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ + printf("socket error\n"); + exit(0); +} + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard address */ + servaddr.sin_port = htons(atoi(argv[1])); /* server port */ + + if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0){ + printf("bind error\n"); exit(0); +} + if( listen(listenfd, 5) < 0 ) + { printf("listen error\n"); + exit(0); +} + for ( ; ; ) { + if( (connfd = accept(listenfd, (struct sockaddr *) NULL, NULL)) < 0) + { printf("accept error\n"); exit(0);} else { + } + + n=read(connfd, buff, MAXLINE); + fprintf (stdout,%s\n,buff); + close(connfd); + } +} diff --git a/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.o b/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.o new file mode 100644 index 0000000..6056017 Binary files /dev/null and b/11.UPD.client-server-exam-database (COMPLETE)/daytimesrv.o differ diff --git a/11.UPD.client-server-exam-database (COMPLETE)/makefile b/11.UPD.client-server-exam-database (COMPLETE)/makefile new file mode 100644 index 0000000..a9beab6 --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = +all : server client + +server : daytimesrv.o ../fun-corso-reti.o + $(CC) -o server daytimesrv.o ../fun-corso-reti.o $(CFLAGS) +client : daytimecli.o ../fun-corso-reti.o + $(CC) -o client daytimecli.o ../fun-corso-reti.o $(CFLAGS) +daytimesrv.o : daytimesrv.c ../basic.h + $(CC) -c -o daytimesrv.o daytimesrv.c $(CFLAGS) +fun-corso-reti.o : ../fun-corso-reti.c ../basic.h + $(CC) -c -o ../fun-corso-reti.o ../fun-corso-reti.c $(CFLAGS) +daytimecli.o : daytimecli.c ../basic.h + $(CC) -c -o daytimecli.o daytimecli.c $(CFLAGS) diff --git a/11.UPD.client-server-exam-database (COMPLETE)/makefile~ b/11.UPD.client-server-exam-database (COMPLETE)/makefile~ new file mode 100644 index 0000000..7a44636 --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/makefile~ @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS =-std=c99 +all : server client + +server : daytimesrv.o ../fun-corso-reti.o + $(CC) -o server daytimesrv.o ../fun-corso-reti.o $(CFLAGS) +client : daytimecli.o ../fun-corso-reti.o + $(CC) -o client daytimecli.o ../fun-corso-reti.o $(CFLAGS) +daytimesrv.o : daytimesrv.c ../basic.h + $(CC) -c -o daytimesrv.o daytimesrv.c $(CFLAGS) +fun-corso-reti.o : ../fun-corso-reti.c ../basic.h + $(CC) -c -o ../fun-corso-reti.o ../fun-corso-reti.c $(CFLAGS) +daytimecli.o : daytimecli.c ../basic.h + $(CC) -c -o daytimecli.o daytimecli.c $(CFLAGS) diff --git a/11.UPD.client-server-exam-database (COMPLETE)/readme b/11.UPD.client-server-exam-database (COMPLETE)/readme new file mode 100644 index 0000000..2624fed --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/readme @@ -0,0 +1,16 @@ + +SONO STATI CALCOALTI ERRORI SU READ,WRITE,CONNECT,SIGNALS +IL SERVER RISPONDE SOLO A UN CLIENT ALLA VOLTA E LE READ/WRITE SONO BLOCCANTI (SENZA SELECT) +PROTOCOLLO USATO UDP CONNESSO - 1 a 1 + +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.UPD.client-server-exam-database (COMPLETE)/readme~ b/11.UPD.client-server-exam-database (COMPLETE)/readme~ new file mode 100644 index 0000000..8f597cb --- /dev/null +++ b/11.UPD.client-server-exam-database (COMPLETE)/readme~ @@ -0,0 +1,16 @@ + +SONO STATI CALCOALTI ERRORI SU READ,WRITE,CONNECT,SIGNALS +IL SERVER RISPONDE A SOLO UNA ISTANZA E LE READ/WRITE SONO BLOCCANTI (SENZA SELECT) +PROTOCOLLO USATO UDP CONNESSO - 1 a 1 + +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.UPD.client-server-exam-database (COMPLETE)/server b/11.UPD.client-server-exam-database (COMPLETE)/server new file mode 100755 index 0000000..eeea01e Binary files /dev/null and b/11.UPD.client-server-exam-database (COMPLETE)/server differ