Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / TCP_echod_first.c
index 55fd5c6981842e28f47d614c293d07c45170d7ae..31d0ded800ef17cb50ce76f02e6c16d9c5192415 100644 (file)
@@ -1,4 +1,4 @@
-/* TCP_echod.c
+/* TCP_echod_first.c
  * 
  * Copyright (C) 2001-2003 Simone Piccardi
  * 
  *
  * Program echod 
  * Elementary TCP server for echo service (port 7)
+ * First version
  *
  * Author: Simone Piccardi
  * Jun. 2001
  *
  * Usage: echod -h give all info
  *
- * $Id: TCP_echod_first.c,v 1.1 2003/05/06 14:05:12 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 <time.h>
+#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 <time.h>        /* date and time constants, types and functions */
 #include <syslog.h>      /* syslog system functions */
-#include <signal.h>      /* signal functions */
+#include <signal.h>      /* signal constants, types and functions */
+#include <errno.h>       /* error definitions and routines */
+#include <string.h>      /* C strings library */
+#include <stdlib.h>      /* C standard library */
+
 #include "Gapil.h"
 
 #define BACKLOG 10
 #define MAXLINE 256
-int demonize  = 1;  /* daemon use option */
-int debugging = 0;  /* debug info printing option */
+int demonize  = 1;  /* daemon use option: default is daemon */
+int debugging = 0;  /* debug info printing option: default is no debug */
 /* Subroutines declaration */
 void usage(void);
 void ServEcho(int sockfd);
@@ -179,14 +182,8 @@ void ServEcho(int sockfd) {
     /* main loop, reading 0 char means client close connection */
     while ( (nread = read(sockfd, buffer, MAXLINE)) != 0) {
        nwrite = FullWrite(sockfd, buffer, nread);
-       if (debugging) {
-           buffer[nread] = 0;
-           snprintf(debug, MAXLINE+20, "Letti %d byte, %s", nread, buffer);
-           if (demonize) {          /* go daemon */
-               syslog(LOG_DEBUG, debug);
-           } else {
-               printf("%s", debug);
-           }
+       if (nwrite) {
+           PrintErr("write error");
        }
     }
     return;
@@ -195,8 +192,8 @@ void ServEcho(int sockfd) {
  * routine to print error on stout or syslog
  */
 void PrintErr(char * error) {
-    if (demonize) {          /* go daemon */
-       syslog(LOG_ERR, error);
+    if (demonize) {                       /* daemon mode */
+       syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
     } else {
        perror(error);
     }