Versione finale del client ECHO su TCP, con esempio di uso della funzione
[gapil.git] / sources / TCP_echo.c
index c8c5c1ac97910381607852c971c5ce1c3203d11c..691911e0c57c0c23b38ac742d7fa4a3d707d9ff0 100644 (file)
@@ -26,7 +26,7 @@
  *
  * Usage: echo -h give all info's
  *
- * $Id: TCP_echo.c,v 1.9 2003/10/18 16:30:23 piccardi Exp $
+ * $Id: TCP_echo.c,v 1.11 2003/10/20 22:44:16 piccardi Exp $
  *
  ****************************************************************/
 /* 
@@ -142,19 +142,24 @@ void ClientEcho(FILE * filein, int socket)
     int nread, nwrite; 
     int maxfd;
     fd_set fset;
+    int eof = 0;
     /* initialize file descriptor set */
     FD_ZERO(&fset);
-    maxfd = max(fileno(stdin), socket) + 1;
+    maxfd = max(fileno(filein), socket) + 1;
     while (1) {
-       FD_SET(socket, &fset);        /* set for the socket */
-       FD_SET(fileno(stdin), &fset); /* set for the standard input */
+       FD_SET(socket, &fset);         /* set for the socket */
+       if (eof == 0) {
+           FD_SET(fileno(filein), &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 (FD_ISSET(fileno(filein), &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 */
+               eof = 1;               /* EOF on input */
+               shutdown(socket, SHUT_WR);      /* close write half */
+               FD_CLR(fileno(filein), &fset);  /* no more interest on stdin */
+           } else {                   /* else we have to write to socket */
                nwrite = FullWrite(socket, sendbuff, strlen(sendbuff)); 
-               if (nwrite < 0) {     /* on error stop */
+               if (nwrite < 0) {      /* on error stop */
                    printf("Errore in scrittura: %s", strerror(errno));
                    return;
                }
@@ -167,8 +172,12 @@ void ClientEcho(FILE * filein, int socket)
                return;
            }
            if (nread == 0) { /* server closed connection, stop */
-               printf("EOF sul socket\n");
-               return;
+               if (eof == 1) {
+                   return;
+               } else {
+                   printf("EOF prematuro sul socket\n");
+                   return;
+               }
            }
            recvbuff[nread] = 0;   /* else read is ok, write on stdout */
            if (fputs(recvbuff, stdout) == EOF) {