Versione finale del client ECHO su TCP, con esempio di uso della funzione
[gapil.git] / sources / TCP_echo.c
index be6842babd90358dffab5bbd4526dabe07ee65bf..691911e0c57c0c23b38ac742d7fa4a3d707d9ff0 100644 (file)
@@ -1,4 +1,4 @@
-/* TCP_echo1.c
+/* TCP_echo.c
  * 
  * Copyright (C) 2001-2003 Simone Piccardi
  * 
  * 
  * Copyright (C) 2001-2003 Simone Piccardi
  * 
@@ -18,7 +18,7 @@
  */
 /****************************************************************
  *
  */
 /****************************************************************
  *
- * Program ElemEchoTCPClient.c
+ * Program TCP_echo.c
  * Simple TCP client for echo service (port 7)
  *
  * Author: Simone Piccardi
  * Simple TCP client for echo service (port 7)
  *
  * Author: Simone Piccardi
@@ -26,7 +26,7 @@
  *
  * Usage: echo -h give all info's
  *
  *
  * Usage: echo -h give all info's
  *
- * $Id: TCP_echo.c,v 1.1 2003/06/23 13:58:36 piccardi Exp $
+ * $Id: TCP_echo.c,v 1.11 2003/10/20 22:44:16 piccardi Exp $
  *
  ****************************************************************/
 /* 
  *
  ****************************************************************/
 /* 
 #include <arpa/inet.h>   /* IP addresses conversion utiliites */
 #include <sys/socket.h>  /* socket library */
 #include <stdio.h>      /* include standard I/O library */
 #include <arpa/inet.h>   /* IP addresses conversion utiliites */
 #include <sys/socket.h>  /* socket library */
 #include <stdio.h>      /* include standard I/O library */
+#include <errno.h>      /* include error codes */
+#include <string.h>     /* include erroro strings definitions */
+
+#include "macros.h"
 
 #define MAXLINE 256
 void usage(void);
 
 #define MAXLINE 256
 void usage(void);
@@ -106,6 +110,7 @@ int main(int argc, char *argv[])
     }
     /* check if resetting on close is required */
     if (reset) {
     }
     /* check if resetting on close is required */
     if (reset) {
+       printf("Setting reset on close \n");
        ling.l_onoff = 1;
        ling.l_linger = 0;      
        if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling))) {
        ling.l_onoff = 1;
        ling.l_linger = 0;      
        if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling))) {
@@ -113,7 +118,7 @@ int main(int argc, char *argv[])
            exit(1);
        }
     }
            exit(1);
        }
     }
-    /* read daytime from server */
+    /* do read/write operations */
     ClientEcho(stdin, sock);
     /* normal exit */
     return 0;
     ClientEcho(stdin, sock);
     /* normal exit */
     return 0;
@@ -134,12 +139,51 @@ void usage(void) {
 void ClientEcho(FILE * filein, int socket) 
 {
     char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1];
 void ClientEcho(FILE * filein, int socket) 
 {
     char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1];
-    int nread; 
-    while (fgets(sendbuff, MAXLINE, filein) != NULL) {
-       FullWrite(socket, sendbuff, strlen(sendbuff)); 
-       nread = FullRead(socket, recvbuff, strlen(sendbuff));
-       recvbuff[nread] = 0;
-       fputs(recvbuff, stdout);
+    int nread, nwrite; 
+    int maxfd;
+    fd_set fset;
+    int eof = 0;
+    /* initialize file descriptor set */
+    FD_ZERO(&fset);
+    maxfd = max(fileno(filein), socket) + 1;
+    while (1) {
+       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(filein), &fset)) {  /* if ready on stdin */
+           if (fgets(sendbuff, MAXLINE, filein) == NULL) { /* if no input */
+               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 */
+                   printf("Errore in scrittura: %s", strerror(errno));
+                   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 */
+               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) {
+               perror("Errore in scrittura su terminale");
+               return;
+           }
+       }
     }
     }
-    return;
 }
 }