1 void ClientEcho(FILE * filein, int socket)
3 char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1];
8 /* initialize file descriptor set */
10 maxfd = max(fileno(filein), socket) + 1;
12 FD_SET(socket, &fset); /* set for the socket */
14 FD_SET(fileno(filein), &fset); /* set for the standard input */
16 select(maxfd, &fset, NULL, NULL, NULL); /* wait for read ready */
17 if (FD_ISSET(fileno(filein), &fset)) { /* if ready on stdin */
18 if (fgets(sendbuff, MAXLINE, filein) == NULL) { /* if no input */
19 eof = 1; /* EOF on input */
20 shutdown(socket, SHUT_WR); /* close write half */
21 FD_CLR(fileno(filein), &fset); /* no more interest on stdin */
22 } else { /* else we have to write to socket */
23 nwrite = FullWrite(socket, sendbuff, strlen(sendbuff));
24 if (nwrite < 0) { /* on error stop */
25 printf("Errore in scrittura: %s", strerror(errno));
30 if (FD_ISSET(socket, &fset)) { /* if ready on socket */
31 nread = read(socket, recvbuff, strlen(sendbuff)); /* do read */
32 if (nread < 0) { /* error condition, stop client */
33 printf("Errore in lettura: %s\n", strerror(errno));
36 if (nread == 0) { /* server closed connection, stop */
40 printf("EOF prematuro sul socket\n");
44 recvbuff[nread] = 0; /* else read is ok, write on stdout */
45 if (fputs(recvbuff, stdout) == EOF) {
46 perror("Errore in scrittura su terminale");