diff --git a/7.client-server-select (COMPLETE)/client b/7.client-server-select (COMPLETE)/client new file mode 100755 index 0000000..b7ae703 Binary files /dev/null and b/7.client-server-select (COMPLETE)/client differ diff --git a/7.client-server-select (COMPLETE)/daytimecli.c b/7.client-server-select (COMPLETE)/daytimecli.c new file mode 100644 index 0000000..4d88590 --- /dev/null +++ b/7.client-server-select (COMPLETE)/daytimecli.c @@ -0,0 +1,78 @@ + + +#include "../basic.h" + +typedef struct message { + char data[100]; +}t_message; + +void sigpipeaction (int signal){ + + //fprintf(stderr, "Sigpipe catturato\n"); + +} + +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 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); + } + + t_message m; + fprintf (stdout,"Send Text: "); + fscanf (stdin,"%999[^\n]%*c",m.data); + int written; + for (written = write (sockfd,&m,sizeof(m)); written <= 0;written = write (sockfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {break;} + } + int ridden; + if(written > 0){ + for (ridden = read (sockfd,&m,sizeof(m)); ridden <= 0;ridden = read (sockfd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {break;} + } + + if(ridden > 0){ + + fprintf(stdout,"%s from server\n",m.data); + } else { + + fprintf(stdout,"can't read from server\n",m.data); + } + + + } else { + fprintf(stdout,"can't write to server\n",m.data); + } + + exit(0); +} diff --git a/7.client-server-select/daytimecli.c~ b/7.client-server-select (COMPLETE)/daytimecli.c~ similarity index 100% rename from 7.client-server-select/daytimecli.c~ rename to 7.client-server-select (COMPLETE)/daytimecli.c~ diff --git a/7.client-server-select (COMPLETE)/daytimecli.o b/7.client-server-select (COMPLETE)/daytimecli.o new file mode 100644 index 0000000..65ed820 Binary files /dev/null and b/7.client-server-select (COMPLETE)/daytimecli.o differ diff --git a/7.client-server-select (COMPLETE)/daytimesrv.c b/7.client-server-select (COMPLETE)/daytimesrv.c new file mode 100644 index 0000000..001096f --- /dev/null +++ b/7.client-server-select (COMPLETE)/daytimesrv.c @@ -0,0 +1,187 @@ +#include "../basic.h" +#include + +void sigpipeaction (int signal){ + + //fprintf(stderr, "Sigpipe catturato\n"); + +} + +typedef struct message { + char data[100]; +}t_message; + + +int main(int argc, char **argv) { + + int listenfd, connfd, n; + struct sockaddr_in servaddr; + struct sockaddr_in client_address_struct; + unsigned int client_address_struct_size = sizeof (client_address_struct); + + +//////////////////////////////////////////////////////////// SIGNALS + +///////SIGPIPE +struct sigaction sigpipestructure; + sigpipestructure.sa_handler = sigpipeaction; + sigpipestructure.sa_flags = 0; + +sigaction (SIGPIPE, &sigpipestructure, NULL); +//////////////////////////////////////////////////////////// END SIGNALS + + + 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); + } + + + + fd_set fd_set_1; + FD_ZERO(&fd_set_1); + /* + void FD_CLR(int fd, fd_set *set); + int FD_ISSET(int fd, fd_set *set); + void FD_SET(int fd, fd_set *set); + void FD_ZERO(fd_set *set); + */ + + struct timeval timer; + timer.tv_sec = 99999999; + timer.tv_usec = 0; + + + int clients [FD_SETSIZE - 3]; + int i; + for (i= 0;i< (FD_SETSIZE - 3);i++){ + *(clients + i) = -1 ; + } + + // FD_SET(fileno(stdin),&fd_set_1); // control stdin (fileno = 0) + + // add listenfd to monitor listen socket + FD_SET(listenfd,&fd_set_1); + fprintf(stdout,"Added listen socket %d to monitor\n",listenfd ); + int max_fd = listenfd + 1; + + t_message m; + fd_set backup_set; + + for ( ; ; ) { + + + + fprintf(stdout,"waiting for non blocking fds\n"); + int ready_fds_to_read = 0; + backup_set = fd_set_1; + for (ready_fds_to_read = select( max_fd, &fd_set_1, NULL,NULL,&timer); + ready_fds_to_read <= 0 ; + ready_fds_to_read = select( max_fd, &fd_set_1, NULL,NULL,&timer)){ + + if (ready_fds_to_read == -1 && errno == EINTR ){ continue;} + if (ready_fds_to_read == 0) {timer.tv_sec = 99999999;continue;} + // write code to inform client that server has an internal error + exit(0); + + }// wait for nonblocking fd in read set + fprintf(stdout,"ready: %d\n",ready_fds_to_read); + int flag = 0; + if (FD_ISSET(listenfd,&fd_set_1)){ + ready_fds_to_read--; + for ( ; ; ) { + if( (connfd = accept(listenfd, (struct sockaddr *) &client_address_struct, &client_address_struct_size)) < 0) + { + if(errno == EINTR ) + continue; + printf("accept error on listening socket\n"); + flag = 1; + break; + } + break; + } + if (flag == 0){ + + /// operations on connfd + int free = 0; + for (i= 0;i< (FD_SETSIZE - 3);i++){ + if (*(clients + i) == -1) {*(clients + i) = connfd; free = 1; break;} + } + if (free == 0){ + shutdown (connfd, SHUT_RDWR); + fprintf(stdout,"Can't server %d. Too many connected clients\n",connfd ); + + }else { + FD_SET(connfd,&backup_set); + max_fd = MAX(connfd + 1, max_fd); + fprintf(stdout,"Added connection socket %d to monitor\n",connfd ); + } + } + } + + + // operations on other ready to read sockets in select + + int ready_fd, ready_fd_index; + for (i= 0; i< (FD_SETSIZE - 3) && ready_fds_to_read>0; i++){ + + if ( *(clients + i) != -1 && FD_ISSET(*(clients + i),&fd_set_1)){ + ready_fds_to_read--; + ready_fd = *(clients + i); + ready_fd_index = i; + + int ridden; + for (ridden = read (ready_fd,&m,sizeof(m)); ridden <= 0;ridden = read (ready_fd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {break;} + } + + int written; + if (ridden > 0){ + fprintf(stdout,"%s from socket id %d\n",m.data,ready_fd); + for (written = write (ready_fd,&m,sizeof(m)); written <= 0;written = write (ready_fd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {break;} + } + + } + + // test if client has received the packet and exit(); + //the client implementation should only make a read after the server + // write and exit + for (ridden = read (ready_fd,&m,sizeof(m)); ridden <= 0;ridden = read (ready_fd,&m,sizeof(m))){ + if (errno == EINTR){continue;} else {break;} + } + if (ridden> 0) {fprintf(stdout,"client from socket id %d should be dead\n",ready_fd);} + + shutdown (ready_fd, SHUT_RDWR); // SHUT_RD - SHUT_WR + FD_CLR(ready_fd, &backup_set); + clients[ready_fd_index] = -1; + fprintf(stdout,"shutdown socket id %d\n",ready_fd); + + + + + + } + + } + fd_set_1 = backup_set; + + } + exit(0); +} \ No newline at end of file diff --git a/7.client-server-select/daytimesrv.c~ b/7.client-server-select (COMPLETE)/daytimesrv.c~ similarity index 100% rename from 7.client-server-select/daytimesrv.c~ rename to 7.client-server-select (COMPLETE)/daytimesrv.c~ diff --git a/7.client-server-select (COMPLETE)/daytimesrv.o b/7.client-server-select (COMPLETE)/daytimesrv.o new file mode 100644 index 0000000..68316e9 Binary files /dev/null and b/7.client-server-select (COMPLETE)/daytimesrv.o differ diff --git a/7.client-server-select/makefile b/7.client-server-select (COMPLETE)/makefile similarity index 100% rename from 7.client-server-select/makefile rename to 7.client-server-select (COMPLETE)/makefile diff --git a/7.client-server-select/makefile~ b/7.client-server-select (COMPLETE)/makefile~ similarity index 100% rename from 7.client-server-select/makefile~ rename to 7.client-server-select (COMPLETE)/makefile~ diff --git a/7.client-server-select (COMPLETE)/readme b/7.client-server-select (COMPLETE)/readme new file mode 100644 index 0000000..556cad5 --- /dev/null +++ b/7.client-server-select (COMPLETE)/readme @@ -0,0 +1,6 @@ +SERVER NON RICORSIVO CON UTILIZZO DI SELECT ED CHECK DI ERRORI + +il server attende una connessione dal client +il client si connette al server e invia un messaggio +il server lo legge e lo ritrasmette al client +il client lo legge e poi termina diff --git a/7.client-server-select/readme b/7.client-server-select (COMPLETE)/readme~ similarity index 100% rename from 7.client-server-select/readme rename to 7.client-server-select (COMPLETE)/readme~ diff --git a/7.client-server-select (COMPLETE)/server b/7.client-server-select (COMPLETE)/server new file mode 100755 index 0000000..6b4093f Binary files /dev/null and b/7.client-server-select (COMPLETE)/server differ diff --git a/7.client-server-select/client b/7.client-server-select/client deleted file mode 100755 index 1480f83..0000000 Binary files a/7.client-server-select/client and /dev/null differ diff --git a/7.client-server-select/daytimecli.c b/7.client-server-select/daytimecli.c deleted file mode 100644 index 37ce25d..0000000 --- a/7.client-server-select/daytimecli.c +++ /dev/null @@ -1,47 +0,0 @@ - - -#include "../basic.h" - -typedef struct message { - char data[100]; -}t_message; - -int main(int argc, char **argv) { - 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); - } - - t_message m; - - fscanf(stdin) - - - - - int ridden; - ridden = read (sockfd, &m,sizeof(m) ); - - - - exit(0); -} diff --git a/7.client-server-select/daytimecli.o b/7.client-server-select/daytimecli.o deleted file mode 100644 index 1b032de..0000000 Binary files a/7.client-server-select/daytimecli.o and /dev/null differ diff --git a/7.client-server-select/daytimesrv.c b/7.client-server-select/daytimesrv.c deleted file mode 100644 index 3f47f3d..0000000 --- a/7.client-server-select/daytimesrv.c +++ /dev/null @@ -1,145 +0,0 @@ - - -#include "../basic.h" -#include - -void sigchldaction (int signal){ - - int status, pid; - - for (pid = waitpid(-1,&status,WNOHANG);pid > 0;pid = waitpid(-1,&status,WNOHANG)){ - // server: child terminated - } - - -} - -void sigpipeaction (int signal){ - - //fprintf(stderr, "Sigpipe catturato\n"); - -} - -typedef struct message { - char data[100]; -}t_message; - - -int main(int argc, char **argv) { - int listenfd, connfd, n; - struct sockaddr_in servaddr; - - -//////////////////////////////////////////////////////////// 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 - - - - 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); - } - - - - fd_set fd_set_1; - FD_ZERO(&fd_set_1); - /* - void FD_CLR(int fd, fd_set *set); - int FD_ISSET(int fd, fd_set *set); - void FD_SET(int fd, fd_set *set); - void FD_ZERO(fd_set *set); - */ - - struct timeval timer; - timer.tv_sec = 99999999; - timer.tv_usec = 0; - - - - for ( ; ; ) { - - - if( (connfd = accept(listenfd, (struct sockaddr *) &client_address_struct, &client_address_struct_size)) < 0) - { - if(errno == EINTR || errno==ECONNABORTED) - continue; - printf("accept error\n"); - exit(1); - } - - int split = fork(); - - if ( split < 0) { - printf("fork for iterative server failed\n"); - exit(-1); - } - - if (split == 0){ - close(listenfd); - - - t_message m; - int ready_fds_to_read = 0; - - FD_set(connfd,&fd_set_1); - - for (ready_fds_to_read = select( connfd + 1, fd_set_1, NULL,NULL,&timer); - ready_fds_to_read <= 0 ; - ready_fds_to_read = select( connfd + 1, fd_set_1, NULL,NULL,&timer)){ - - if (ready_fds_to_read == -1 && errno == EINTR ){ continue;} - if (ready_fds_to_read == 0) {timer.tv_sec = 99999999;continue;} - // write code to inform client that server has an internal error - exit(0); - - }// wait for nonblocking fd in read set - - int ridden; - if( FD_ISSET(connfd, fd_set_1) ){ - for (ridden = read (connfd,&m,sizeof(m)); ridden <= 0;ridden = read (connfd,&m,sizeof(m))){ - if (errno == EINTR){continue;} else {exit(0);} - } - - fprint(stdout,%s,m.data); - - - } - - exit(0); - - - }else { - close(connfd); - } - } -} diff --git a/7.client-server-select/daytimesrv.o b/7.client-server-select/daytimesrv.o deleted file mode 100644 index d378ae7..0000000 Binary files a/7.client-server-select/daytimesrv.o and /dev/null differ diff --git a/7.client-server-select/readme~ b/7.client-server-select/readme~ deleted file mode 100644 index e69de29..0000000 diff --git a/7.client-server-select/server b/7.client-server-select/server deleted file mode 100755 index 833ba65..0000000 Binary files a/7.client-server-select/server and /dev/null differ diff --git a/test/test3 b/test/test3 new file mode 100755 index 0000000..a13739b Binary files /dev/null and b/test/test3 differ diff --git a/test/test3.c b/test/test3.c new file mode 100644 index 0000000..e062758 --- /dev/null +++ b/test/test3.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include +#include +#include + + +int main (int index, char** in_data){ + +fprintf(stdout, "%d\n",fileno(stdout)); +fprintf(stdout, "%d\n",fileno(stdin)); +fprintf(stdout, "%d\n",fileno(stderr)); + + + return 0; +} diff --git a/test/test3.c~ b/test/test3.c~ new file mode 100644 index 0000000..802abaa --- /dev/null +++ b/test/test3.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; +}