Messi alcuni riferimenti giusti all'urgent data e scritta la versione di
[gapil.git] / sources / TCP_echo.c
index 86b78c2762a1806840bb757680d6ee0c49d7b317..c8c5c1ac97910381607852c971c5ce1c3203d11c 100644 (file)
@@ -26,7 +26,7 @@
  *
  * Usage: echo -h give all info's
  *
- * $Id: TCP_echo.c,v 1.8 2003/08/17 23:03:44 piccardi Exp $
+ * $Id: TCP_echo.c,v 1.9 2003/10/18 16:30:23 piccardi Exp $
  *
  ****************************************************************/
 /* 
@@ -40,6 +40,8 @@
 #include <errno.h>      /* include error codes */
 #include <string.h>     /* include erroro strings definitions */
 
+#include "macros.h"
+
 #define MAXLINE 256
 void usage(void);
 void ClientEcho(FILE * filein, int socket);
@@ -138,26 +140,41 @@ void ClientEcho(FILE * filein, int socket)
 {
     char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1];
     int nread, nwrite; 
-    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;
+               }
+           }
        }
-       recvbuff[nread] = 0;
-       if (fputs(recvbuff, stdout) == EOF) {
-           perror("Errore in scrittura su terminale");
-           return;
+       if (FD_ISSET(socket, &fset)) { /* if ready on socket */ 
+           nread = read(socket, recvbuff, strlen(sendbuff)); /* do read */
+           if (nread < 0) {  /* error condition, stop client */
+               printf("Errore in lettura: %s\n", strerror(errno));
+               return;
+           }
+           if (nread == 0) { /* server closed connection, stop */
+               printf("EOF sul socket\n");
+               return;
+           }
+           recvbuff[nread] = 0;   /* else read is ok, write on stdout */
+           if (fputs(recvbuff, stdout) == EOF) {
+               perror("Errore in scrittura su terminale");
+               return;
+           }
        }
     }
-    return;
 }