Ancora correzioni sulle varie versioni di server.
[gapil.git] / sources / TCP_echod.c
index 35de97c30f22b9981e7dac777b24da23fb586388..25a457187e4d000071e59623967d67938163a216 100644 (file)
@@ -26,7 +26,7 @@
  *
  * Usage: echod -h give all info
  *
- * $Id: TCP_echod.c,v 1.5 2003/06/19 11:43:13 piccardi Exp $ 
+ * $Id: TCP_echod.c,v 1.10 2003/07/29 22:41:36 piccardi Exp $ 
  *
  ****************************************************************/
 /* 
@@ -41,6 +41,7 @@
 #include <syslog.h>      /* syslog system functions */
 #include <signal.h>      /* signal functions */
 #include <errno.h>       /* error code */
+#include <string.h>      /* error strings */
 #include "Gapil.h"
 
 #define BACKLOG 10
@@ -58,7 +59,7 @@ int main(int argc, char *argv[])
  * Variables definition  
  */
     int list_fd, conn_fd;
-    int waiting;
+    int waiting = 0;
     pid_t pid;
     struct sockaddr_in serv_add, cli_add;
     socklen_t len;
@@ -103,7 +104,7 @@ int main(int argc, char *argv[])
      * 
      * ***********************************************************/
     /* install SIGCHLD handler */
-    Signal(SIGCHLD, HandSigCHLD);  /* establish handler */
+    SignalRestart(SIGCHLD, HandSigCHLD);  /* establish handler */
     /* create socket */
     if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Socket creation error");
@@ -183,8 +184,9 @@ void usage(void) {
     printf("Usage:\n");
     printf("  echod [-h] \n");
     printf("  -h          print this help\n");
-    printf("  -d          print debug info\n");
+    printf("  -d          write debug info\n");
     printf("  -i          use interactively\n");
+    printf("  -w N        wait N sec. before calling accept\n");
     exit(1);
 }
 /*
@@ -197,7 +199,25 @@ void ServEcho(int sockfd) {
     int size;
     /* main loop, reading 0 char means client close connection */
     while ( (nread = read(sockfd, buffer, MAXLINE)) != 0) {
+       if (nread < 0) {
+           snprintf(debug, MAXLINE+20, "Errore in lettura: %s \n", 
+                    strerror(errno));
+           if (demonize) {          /* daemon mode */
+               syslog(LOG_DEBUG, debug);
+           } else {
+               printf("%s", debug);
+           }
+       }
        nwrite = FullWrite(sockfd, buffer, nread);
+       if (nwrite) {
+           snprintf(debug, MAXLINE+20, "Errore in scrittura: %s \n", 
+                    strerror(errno));
+           if (demonize) {          /* daemon mode */
+               syslog(LOG_DEBUG, debug);
+           } else {
+               printf("%s", debug);
+           }       
+       }
        if (debugging) {
            buffer[nread] = 0;
            snprintf(debug, MAXLINE+20, "Letti %d byte, %s", nread, buffer);
@@ -214,8 +234,8 @@ void ServEcho(int sockfd) {
  * routine to print error on stout or syslog
  */
 void PrintErr(char * error) {
-    if (demonize) {          /* daemon mode */
-       syslog(LOG_ERR, error);
+    if (demonize) {                       /* daemon mode */
+       syslog(LOG_ERR, "%s: %m", error); /* log string and error message */
     } else {
        perror(error);
     }