Completata la parte su getaddinfo con delle funzioni di ausilio per chiamare
[gapil.git] / sources / TCP_echo.c
index 46ec5ca23d3402d8b080c2748197efec7b181c26..f60c518233c4d2e399d9fcc7357c74319a5b571b 100644 (file)
@@ -1,4 +1,4 @@
-/* TCP_echo1.c
+/* TCP_echo.c
  * 
  * 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
@@ -26,7 +26,7 @@
  *
  * Usage: echo -h give all info's
  *
- * $Id: TCP_echo.c,v 1.6 2003/07/28 22:16:36 piccardi Exp $
+ * $Id: TCP_echo.c,v 1.11 2003/10/20 22:44:16 piccardi Exp $
  *
  ****************************************************************/
 /* 
@@ -40,6 +40,9 @@
 #include <errno.h>      /* include error codes */
 #include <string.h>     /* include erroro strings definitions */
 
+#include "Gapil.h"
+#include "macros.h"
+
 #define MAXLINE 256
 void usage(void);
 void ClientEcho(FILE * filein, int socket);
@@ -53,7 +56,6 @@ int main(int argc, char *argv[])
  */
     int sock, i;
     int reset = 0;
-    struct sockaddr_in serv_add;
     struct linger ling;
     /*
      * Input section: decode parameters passed in the calling 
@@ -87,23 +89,9 @@ int main(int argc, char *argv[])
      *               Main code beginning
      * 
      * ***********************************************************/
-    /* create socket */
-    if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-       perror("Socket creation error");
-       return 1;
-    }
-    /* initialize address */
-    memset((void *) &serv_add, 0, sizeof(serv_add)); /* clear server address */
-    serv_add.sin_family = AF_INET;                   /* address type is INET */
-    serv_add.sin_port = htons(7);                    /* echo port is 7 */
-    /* build address using inet_pton */
-    if ( (inet_pton(AF_INET, argv[optind], &serv_add.sin_addr)) <= 0) {
-       perror("Address creation error");
-       return 1;
-    }
-    /* extablish connection */
-    if (connect(sock, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
-       perror("Connection error");
+    /* call sockaddr to get a connected socket */
+    if ( (sock = sockconn(argv[optind], "echo", 6, SOCK_STREAM)) < 0) {
+       if (errno) perror("Socket creation error");
        return 1;
     }
     /* check if resetting on close is required */
@@ -115,7 +103,6 @@ int main(int argc, char *argv[])
            perror("Cannot set linger");
            exit(1);
        }
-       return 0;
     }
     /* do read/write operations */
     ClientEcho(stdin, sock);
@@ -139,19 +126,50 @@ 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));
+    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 */
        }
-        nread = read(socket, recvbuff, strlen(sendbuff));
-       if (nread < 0) {
-           printf("Errore in lettura %s\n", strerror(errno));
+       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;
+               }
+           }
        }
-       recvbuff[nread] = 0;
-       if (fputs(recvbuff, stdout) == EOF) {
-           perror("Errore in scrittura su terminale");
+       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;
 }