- while (fgets(sendbuff, MAXLINE, filein) != NULL) {
- nwrite = FullWrite(socket, sendbuff, strlen(sendbuff));
- if (nwrite < 0) {
- printf("Errore in scrittura: %s", strerror(errno));
- return;
- }
- nread = read(socket, recvbuff, strlen(sendbuff));
- if (nread < 0) {
- printf("Errore in lettura: %s\n", strerror(errno));
- return;
- }
- if (nread == 0) {
- printf("EOF sul socket\n");
- return;
+ int maxfd;
+ fd_set fset;
+ /* initialize file descriptor set */
+ FD_ZERO(&fset);
+ maxfd = max(fileno(stdin), socket) + 1;
+ while (1) {
+ FD_SET(socket, &fset); /* set for the socket */
+ FD_SET(fileno(stdin), &fset); /* set for the standard input */
+ select(maxfd, &fset, NULL, NULL, NULL); /* wait for read ready */
+ if (FD_ISSET(fileno(stdin), &fset)) { /* if ready on stdin */
+ if (fgets(sendbuff, MAXLINE, filein) == NULL) { /* if no input */
+ return; /* we stopped client */
+ } else { /* else we have to write to socket */
+ nwrite = FullWrite(socket, sendbuff, strlen(sendbuff));
+ if (nwrite < 0) { /* on error stop */
+ printf("Errore in scrittura: %s", strerror(errno));
+ return;
+ }
+ }