Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / TCP_echo.c
index be6842babd90358dffab5bbd4526dabe07ee65bf..2d41e936f247cece2222a1795570be13b1bfd7d3 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
  *
  * Usage: echo -h give all info's
  *
- * $Id: TCP_echo.c,v 1.1 2003/06/23 13:58:36 piccardi Exp $
- *
  ****************************************************************/
 /* 
  * Include needed headers
  */
-#include <sys/types.h>   /* predefined types */
-#include <unistd.h>      /* include unix standard library */
-#include <arpa/inet.h>   /* IP addresses conversion utiliites */
-#include <sys/socket.h>  /* socket library */
-#include <stdio.h>      /* include standard I/O library */
+#include <sys/types.h>   /* primitive system data types */
+#include <unistd.h>      /* unix standard library */
+#include <arpa/inet.h>   /* IP addresses conversion utilities */
+#include <sys/socket.h>  /* socket constants, types and functions */
+#include <stdio.h>      /* standard I/O library */
+#include <errno.h>      /* error definitions and routines */
+#include <string.h>     /* C strings library */
+#include <stdlib.h>      /* C standard library */
+#include <netinet/tcp.h> /* TCP constants and types */
+
+/* still not defined in some include, because too new ... */
+#ifndef TCP_CONGESTION
+#define TCP_CONGESTION 13
+#endif
+
+#include "Gapil.h"
+#include "macros.h"
 
 #define MAXLINE 256
 void usage(void);
@@ -50,15 +60,17 @@ int main(int argc, char *argv[])
  * Variables definition  
  */
     int sock, i;
+    socklen_t len;
     int reset = 0;
-    struct sockaddr_in serv_add;
+    int verbosity = 0;
+    char buffer[MAXLINE];
     struct linger ling;
     /*
      * Input section: decode parameters passed in the calling 
      * Use getopt function
      */
     opterr = 0;         /* don't want writing to stderr */
-    while ( (i = getopt(argc, argv, "hr")) != -1) {
+    while ( (i = getopt(argc, argv, "hrv")) != -1) {
        switch (i) {
        /* 
         * Handling options 
@@ -68,6 +80,9 @@ int main(int argc, char *argv[])
            usage();
            return(1);
            break;
+       case 'v':
+           verbosity = 1;
+           break;
        case 'r':
            reset = 1;
            break;
@@ -85,27 +100,24 @@ 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");
+    /* call sockconn to get a connected socket */
+    if ( (sock = sockconn(argv[optind], "echo", 6, SOCK_STREAM)) < 0) {
+       if (errno) perror("Socket creation error");
        return 1;
     }
-    /* extablish connection */
-    if (connect(sock, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
-       perror("Connection error");
-       return 1;
+    /* print some info about the socket, used to test some TCP_* options */
+    if (verbosity) {
+       len = sizeof(buffer);
+       if (getsockopt(sock, SOL_TCP, TCP_CONGESTION, buffer, &len) < 0) {
+           perror("Cannot read congestion algorithm");
+       } else {
+           buffer[len]=0;
+           printf("Congestion algorithm %s\n", buffer);
+       }
     }
     /* 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))) {
@@ -113,7 +125,7 @@ int main(int argc, char *argv[])
            exit(1);
        }
     }
-    /* read daytime from server */
+    /* do read/write operations */
     ClientEcho(stdin, sock);
     /* normal exit */
     return 0;
@@ -134,12 +146,51 @@ void usage(void) {
 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;
 }