Correzioni rimaste indietro ed espansione funzioni del resolver.
[gapil.git] / listati / ClientEcho_third.c
1 void ClientEcho(FILE * filein, int socket) 
2 {
3     char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1];
4     int nread, nwrite; 
5     int maxfd;
6     fd_set fset;
7     /* initialize file descriptor set */
8     FD_ZERO(&fset);
9     maxfd = max(fileno(filein), socket) + 1;
10     while (1) {
11         FD_SET(socket, &fset);         /* set for the socket */
12         FD_SET(fileno(filein), &fset); /* set for the standard input */
13         select(maxfd, &fset, NULL, NULL, NULL); /* wait for read ready */
14         if (FD_ISSET(fileno(filein), &fset)) {  /* if ready on stdin */
15             if (fgets(sendbuff, MAXLINE, filein) == NULL) { /* if no input */
16                 return;                /* we stopped client */
17             } else {                   /* else we have to write to socket */
18                 nwrite = FullWrite(socket, sendbuff, strlen(sendbuff)); 
19                 if (nwrite < 0) {      /* on error stop */
20                     printf("Errore in scrittura: %s", strerror(errno));
21                     return;
22                 }
23             }
24         }
25         if (FD_ISSET(socket, &fset)) { /* if ready on socket */ 
26             nread = read(socket, recvbuff, strlen(sendbuff)); /* do read */
27             if (nread < 0) {  /* error condition, stop client */
28                 printf("Errore in lettura: %s\n", strerror(errno));
29                 return;
30             }
31             if (nread == 0) { /* server closed connection, stop */
32                 printf("EOF sul socket\n");
33                 return;
34             }
35             recvbuff[nread] = 0;   /* else read is ok, write on stdout */
36             if (fputs(recvbuff, stdout) == EOF) {
37                 perror("Errore in scrittura su terminale");
38                 return;
39             }
40         }
41     }
42 }