X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FTCP_countd.c;h=8c4c0b36f0f53a6cddec405b8b947f69bd28996b;hp=cf761e77401e00ece7a7ed5996019c829d2b624b;hb=fa15a3f1ecd64efd8440e46d398fd9976abc3d25;hpb=e6ca5b03eaf1e9ef82eb75f722487efe97abaf88 diff --git a/sources/TCP_countd.c b/sources/TCP_countd.c index cf761e7..8c4c0b3 100644 --- a/sources/TCP_countd.c +++ b/sources/TCP_countd.c @@ -19,30 +19,31 @@ /**************************************************************** * * Program countd: Elementary TCP server for teaching purpose, count - * number of request to the server itself + * number of request to the server itself, use POSIX semaphores and + * shared memory * * Author: Simone Piccardi * Feb. 2007 * - * Usage: countdd -h give all info + * Usage: countd -h give all info * ****************************************************************/ /* * Include needed headers */ -#include /* predefined types */ -#include /* include unix standard library */ -#include /* IP addresses conversion utiliites */ -#include /* socket library */ -#include /* include standard I/O library */ -#include +#include /* primitive system data types */ +#include /* unix standard library */ +#include /* IP addresses conversion utilities */ +#include /* socket constants, types and functions */ +#include /* standard I/O library */ +#include /* date and time constants, types and functions */ #include #include #include /* syslog system functions */ -#include /* signal functions */ -#include /* error code */ -#include /* error strings */ -#include +#include /* signal constants, types and functions */ +#include /* error definitions and routines */ +#include /* C strings library */ +#include /* C standard library */ #include "Gapil.h" @@ -61,8 +62,6 @@ int main(int argc, char *argv[]) * Variables definition */ int list_fd, conn_fd; - int waiting = 0; - int keepalive = 0; int reuse = 0; int compat = 0; pid_t pid; @@ -75,7 +74,7 @@ int main(int argc, char *argv[]) */ int i; opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "hkrdicw:")) != -1) { + while ( (i = getopt(argc, argv, "hrdic")) != -1) { switch (i) { /* * Handling options @@ -88,9 +87,6 @@ int main(int argc, char *argv[]) case 'i': demonize = 0; break; - case 'k': - keepalive = 1; - break; case 'r': reuse = 1; break; @@ -100,9 +96,6 @@ int main(int argc, char *argv[]) case 'd': debugging = 1; break; - case 'w': - waiting = strtol(optarg, NULL, 10); - break; case '?': /* unrecognized options */ printf("Unrecognized options -%c\n",optopt); usage(); @@ -124,7 +117,7 @@ int main(int argc, char *argv[]) SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */ } /* create and bind socket */ - if ( (list_fd = sockbindopt(argv[optind], "echo", 6, + if ( (list_fd = sockbindopt(argv[optind], "1234", 6, SOCK_STREAM, reuse)) < 0) { return 1; } @@ -149,7 +142,6 @@ int main(int argc, char *argv[]) PrintErr("listen error"); exit(1); } - if (waiting) sleep(waiting); /* handle echo to client */ while (1) { /* accept connection */ @@ -176,11 +168,6 @@ int main(int argc, char *argv[]) } if (pid == 0) { /* child */ close(list_fd); /* close listening socket */ - if (keepalive) { /* enable keepalive ? */ - setsockopt(conn_fd, SOL_SOCKET, SO_KEEPALIVE, - &keepalive, sizeof(keepalive)); - } - ServEcho(conn_fd); /* handle echo */ if (debugging) { snprintf(debug, MAXLINE, "Closed connection %s\n", ipaddr); if (demonize) { @@ -201,48 +188,17 @@ int main(int argc, char *argv[]) * routine to print usage info and exit */ void usage(void) { - printf("Elementary echo server\n"); + printf("Elementary connection counter server\n"); printf("Usage:\n"); - printf(" echod [-h] \n"); + printf(" countd [-h] \n"); printf(" -h print this help\n"); printf(" -d write debug info\n"); - printf(" -k enable SO_KEEPALIVE\n"); printf(" -r enable SO_REUSEADDR\n"); printf(" -i use interactively\n"); printf(" -c disable BSD semantics\n"); printf(" -w N wait N sec. before calling accept\n"); exit(1); } -/* - * routine to handle echo for connection - */ -void ServEcho(int sockfd) { - char buffer[MAXLINE]; - int nread, nwrite; - char debug[MAXLINE+20]; - /* main loop, reading 0 char means client close connection */ - while ( (nread = read(sockfd, buffer, MAXLINE)) != 0) { - if (nread < 0) { - PrintErr("Errore in lettura"); - return; - } - nwrite = FullWrite(sockfd, buffer, nread); - if (nwrite) { - PrintErr("Errore in scrittura"); - return; - } - if (debugging) { - buffer[nread] = 0; - snprintf(debug, MAXLINE+20, "Letti %d byte, %s", nread, buffer); - if (demonize) { /* daemon mode */ - syslog(LOG_DEBUG, debug); - } else { - printf("%s", debug); - } - } - } - return; -} /* * routine to print error on stout or syslog */ @@ -255,95 +211,3 @@ void PrintErr(char * error) { return; } - -#define MAXLINE 80 -#define BACKLOG 10 -/* Program begin */ -void usage(void); -int main(int argc, char *argv[]) -{ -/* - * Variables definition - */ - int list_fd, conn_fd; - int i; - struct sockaddr_in serv_add; - char buffer[MAXLINE]; - time_t timeval; - /* - * Input section: decode parameters passed in the calling - * Use getopt function - */ - opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "h")) != -1) { - switch (i) { - /* - * Handling options - */ - case 'h': - printf("Wrong -h option use\n"); - usage(); - return(0); - break; - case '?': /* unrecognized options */ - printf("Unrecognized options -%c\n",optopt); - usage(); - default: /* should not reached */ - usage(); - } - } - /* *********************************************************** - * - * Options processing completed - * - * Main code beginning - * - * ***********************************************************/ - /* create socket */ - if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - perror("Socket creation error"); - exit(-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(13); /* daytime port is 13 */ - serv_add.sin_addr.s_addr = htonl(INADDR_ANY); /* connect from anywhere */ - /* bind socket */ - if (bind(list_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) { - perror("bind error"); - exit(-1); - } - /* listen on socket */ - if (listen(list_fd, BACKLOG) < 0 ) { - perror("listen error"); - exit(-1); - } - /* write daytime to client */ - while (1) { - if ( (conn_fd = accept(list_fd, (struct sockaddr *) NULL, NULL)) <0 ) { - perror("accept error"); - exit(-1); - } - timeval = time(NULL); - snprintf(buffer, sizeof(buffer), "%.24s\r\n", ctime(&timeval)); - if ( (write(conn_fd, buffer, strlen(buffer))) < 0 ) { - perror("write error"); - exit(-1); - } - close(conn_fd); - } - - /* normal exit */ - exit(0); -} -/* - * routine to print usage info and exit - */ -void usage(void) { - printf("Simple daytime server\n"); - printf("Usage:\n"); - printf(" daytimed [-h] \n"); - printf(" -h print this help\n"); - exit(1); -}