From 99ef4679f4d9d22707075c7e05a4dbd3900edcd1 Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Thu, 25 Dec 2003 17:31:09 +0000 Subject: [PATCH] Aggiunte note per le funzioni classiche dei socket per ricordarne il loro suo generico, corretti i listati dei server che usano accept, per dare un input il corretto valore della lunghezza della struttura degli indirizzi, risistemata la suddivisione in parti (con la terza parte per le appendici), inserito lo scheletro iniziale di un server echo basato su select. --- gapil.tex | 7 +- intro.tex | 4 - listati/TCP_cunc_daytimed.c | 1 + listati/TCP_echod_first.c | 1 + listati/TCP_echod_second.c | 1 + network.tex | 3 - sources/Makefile | 3 + sources/TCP_cunc_daytimed.c | 7 +- sources/TCP_echod.c | 7 +- sources/TCP_echod_first.c | 5 +- sources/TCP_echod_second.c | 5 +- sources/select_echod.c | 243 ++++++++++++++++++++++++++++++++++++ tcpsock.tex | 98 +++++++++------ 13 files changed, 329 insertions(+), 56 deletions(-) create mode 100644 sources/select_echod.c diff --git a/gapil.tex b/gapil.tex index 62f70cb..761d1d9 100644 --- a/gapil.tex +++ b/gapil.tex @@ -125,7 +125,8 @@ % distance from margins for boxedminipage %\fboxsep=6pt - +\part{Programmazione di sistema} +\label{part:progr-di-sist} \include{intro} \include{process} \include{prochand} @@ -138,11 +139,15 @@ \include{session} \include{fileadv} \include{ipc} +\part{Programmazione di rete} +\label{part:progr-di-rete} \include{network} \include{socket} \include{tcpsock} \include{tcpsockadv} \appendix +\part{Appendici} +\label{part:appendici} \include{netlayer} \include{trasplayer} \include{errors} diff --git a/intro.tex b/intro.tex index 89eb59c..a7355de 100644 --- a/intro.tex +++ b/intro.tex @@ -8,10 +8,6 @@ %% license is included in the section entitled "GNU Free Documentation %% License". %% -\part{Programmazione di sistema} -\label{part:progr-di-sist} - - \chapter{L'architettura del sistema} \label{cha:intro_unix} diff --git a/listati/TCP_cunc_daytimed.c b/listati/TCP_cunc_daytimed.c index 9af8cc7..17160be 100644 --- a/listati/TCP_cunc_daytimed.c +++ b/listati/TCP_cunc_daytimed.c @@ -18,6 +18,7 @@ int main(int argc, char *argv[]) ... /* write daytime to client */ while (1) { + len = sizeof(client); if ( (conn_fd = accept(list_fd, (struct sockaddr *)&client, &len)) <0 ) { perror("accept error"); diff --git a/listati/TCP_echod_first.c b/listati/TCP_echod_first.c index 81085a7..0da60a6 100644 --- a/listati/TCP_echod_first.c +++ b/listati/TCP_echod_first.c @@ -40,6 +40,7 @@ int main(int argc, char *argv[]) exit(1); } while (1) { /* handle echo to client */ + len = sizeof(cli_add); if ( (conn_fd = accept(list_fd, NULL, NULL)) < 0) { PrintErr("accept error"); exit(1); diff --git a/listati/TCP_echod_second.c b/listati/TCP_echod_second.c index 78557af..26f0987 100644 --- a/listati/TCP_echod_second.c +++ b/listati/TCP_echod_second.c @@ -22,6 +22,7 @@ int main(int argc, char *argv[]) /* handle echo to client */ while (1) { /* accept connection */ + len = sizeof(cli_add); while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len)) < 0) && (errno == EINTR)); if ( conn_fd < 0) { diff --git a/network.tex b/network.tex index a82f3ec..79035f8 100644 --- a/network.tex +++ b/network.tex @@ -8,9 +8,6 @@ %% license is included in the section entitled "GNU Free Documentation %% License". %% -\part{Programmazione di rete} -\label{part:progr-di-rete} - \chapter{Introduzione alla programmazione di rete} \label{cha:network} diff --git a/sources/Makefile b/sources/Makefile index 1e4b12c..151d779 100644 --- a/sources/Makefile +++ b/sources/Makefile @@ -73,6 +73,9 @@ echo: TCP_echo.c echod: TCP_echod.c $(CC) $(CFLAGJ) $(CFLAGS) $^ -o $@ +sechod: select_echod.c + $(CC) $(CFLAGJ) $(CFLAGS) $^ -o $@ + daytimed: TCP_cunc_daytimed.c $(CC) $(CFLAGJ) $^ -o $@ diff --git a/sources/TCP_cunc_daytimed.c b/sources/TCP_cunc_daytimed.c index 14896fa..9c33203 100644 --- a/sources/TCP_cunc_daytimed.c +++ b/sources/TCP_cunc_daytimed.c @@ -26,7 +26,7 @@ * * Usage: daytimed -h give all info * - * $Id: TCP_cunc_daytimed.c,v 1.3 2003/05/02 09:55:14 piccardi Exp $ + * $Id: TCP_cunc_daytimed.c,v 1.4 2003/12/25 17:31:09 piccardi Exp $ * ****************************************************************/ /* @@ -113,8 +113,9 @@ int main(int argc, char *argv[]) } /* write daytime to client */ while (1) { - if ( (conn_fd = accept(list_fd, (struct sockaddr *)&client, &len)) - <0 ) { + len = sizeof(client); + if ( (conn_fd = + accept(list_fd, (struct sockaddr *)&client, &len)) <0 ) { perror("accept error"); exit(-1); } diff --git a/sources/TCP_echod.c b/sources/TCP_echod.c index fd51f98..dd0a9f8 100644 --- a/sources/TCP_echod.c +++ b/sources/TCP_echod.c @@ -26,7 +26,7 @@ * * Usage: echod -h give all info * - * $Id: TCP_echod.c,v 1.12 2003/08/03 18:12:47 piccardi Exp $ + * $Id: TCP_echod.c,v 1.13 2003/12/25 17:31:09 piccardi Exp $ * ****************************************************************/ /* @@ -42,6 +42,8 @@ #include /* signal functions */ #include /* error code */ #include /* error strings */ +#include + #include "Gapil.h" #define BACKLOG 10 @@ -153,9 +155,10 @@ int main(int argc, char *argv[]) /* handle echo to client */ while (1) { /* accept connection */ + len = sizeof(cli_add); while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len)) < 0) && (errno == EINTR)); - if ( conn_fd < 0) { + if (conn_fd < 0) { PrintErr("accept error"); exit(1); } diff --git a/sources/TCP_echod_first.c b/sources/TCP_echod_first.c index b1356fe..bfe826a 100644 --- a/sources/TCP_echod_first.c +++ b/sources/TCP_echod_first.c @@ -26,7 +26,7 @@ * * Usage: echod -h give all info * - * $Id: TCP_echod_first.c,v 1.2 2003/07/27 23:41:04 piccardi Exp $ + * $Id: TCP_echod_first.c,v 1.3 2003/12/25 17:31:09 piccardi Exp $ * ****************************************************************/ /* @@ -42,6 +42,8 @@ #include /* signal functions */ #include /* error code */ #include /* error strings */ +#include + #include "Gapil.h" #define BACKLOG 10 @@ -138,6 +140,7 @@ int main(int argc, char *argv[]) /* handle echo to client */ while (1) { /* accept connection */ + len = sizeof(cli_add); if ( (conn_fd = accept(list_fd, NULL, NULL)) < 0) { PrintErr("accept error"); exit(1); diff --git a/sources/TCP_echod_second.c b/sources/TCP_echod_second.c index 4519b29..617eaf1 100644 --- a/sources/TCP_echod_second.c +++ b/sources/TCP_echod_second.c @@ -26,7 +26,7 @@ * * Usage: echod -h give all info * - * $Id: TCP_echod_second.c,v 1.1 2003/08/03 18:12:47 piccardi Exp $ + * $Id: TCP_echod_second.c,v 1.2 2003/12/25 17:31:09 piccardi Exp $ * ****************************************************************/ /* @@ -42,6 +42,8 @@ #include /* signal functions */ #include /* error code */ #include /* error strings */ +#include + #include "Gapil.h" #define BACKLOG 10 @@ -153,6 +155,7 @@ int main(int argc, char *argv[]) /* handle echo to client */ while (1) { /* accept connection */ + len = sizeof(cli_add); while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len)) < 0) && (errno == EINTR)); if ( conn_fd < 0) { diff --git a/sources/select_echod.c b/sources/select_echod.c new file mode 100644 index 0000000..b1b6483 --- /dev/null +++ b/sources/select_echod.c @@ -0,0 +1,243 @@ +/* select_echod.c + * + * Copyright (C) 2003 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Program select_echod + * Elementary TCP server for echo service (port 7) using select + * + * Author: Simone Piccardi + * Dec. 2003 + * + * Usage: echod -h give all info + * + * $Id: select_echod.c,v 1.1 2003/12/25 17:31:09 piccardi Exp $ + * + ****************************************************************/ +/* + * 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 /* syslog system functions */ +#include /* signal functions */ +#include /* error code */ +#include /* error strings */ +#include + +#include "Gapil.h" + +#define BACKLOG 10 +#define MAXLINE 256 +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 PrintErr(char * error); +/* Program beginning */ +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int waiting = 0; + int compat = 0; + struct sockaddr_in serv_add, cli_add; + socklen_t len; + char buffer[MAXLINE]; + char fd_open[FD_SETSIZE]; + fd_set fset; + int list_fd, fd; + int max_fd, nread, nwrite; + int i, n; + /* + * Input section: decode parameters passed in the calling + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "hdicw:")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': + printf("Wrong -h option use\n"); + usage(); + return(0); + break; + case 'i': + demonize = 0; + break; + case 'c': + compat = 1; + break; + case 'd': + debugging = 1; + break; + case 'w': + waiting = strtol(optarg, NULL, 10); + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + /* Main code begin here */ + if (compat) { /* install signal handler */ + Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */ + } else { + SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */ + } + /* 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(7); /* echo port is 7 */ + 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); + } + /* release privileges and go daemon */ + if (setgid(65534) !=0) { /* first give away group privileges */ + perror("cannot give away group privileges"); + exit(1); + } + if (setuid(65534) !=0) { /* and only after user ... */ + perror("cannot give away user privileges"); + exit(1); + } + if (demonize) { /* go daemon */ + openlog(argv[0], 0, LOG_DAEMON); /* open logging */ + if (daemon(0, 0) != 0) { + perror("cannot start as daemon"); + exit(1); + } + } + /* main body */ + if (listen(list_fd, BACKLOG) < 0 ) { + PrintErr("listen error"); + exit(1); + } + if (waiting) sleep(waiting); + /* initialize all needed variables */ + memset(fd_open, 0, FD_SETSIZE); /* clear array of open files */ + max_fd = list_fd; /* set maximum fd to look */ + fd_open[max_fd] = 1; + /* main loop, wait for connection and data inside a select */ + while (1) { + FD_ZERO(&fset); /* clear fd_set */ + for (i = list_fd; i <= max_fd; i++) { + if (fd_open[i]) FD_SET(i, &fset); /* initialize open sockets */ + } + while ( ((n = select(max_fd + 1, &fset, NULL, NULL, NULL)) < 0) + && (errno == EINTR)); + /* there are data */ + if (n < 0) { + PrintErr("select error"); + exit(1); + } + if (FD_ISSET(list_fd, &fset)) { /* if data on listening socket */ + len = sizeof(cli_add); + fd = accept(list_fd, (struct sockaddr *)&cli_add, &len); + if (fd < 0) { + PrintErr("accept error"); + exit(1); + } + fd_open[fd] = 1; + if (max_fd < fd) max_fd = fd; + FD_SET(fd, &fset); + n--; + } + for (i = list_fd + 1; i <= max_fd; i++) { + if (fd_open[i] == 0) continue; + if (FD_ISSET(i, &fset)) { + n--; + nread = read(fd, buffer, MAXLINE); + if (nread < 0) { + PrintErr("Errore in lettura"); + exit(1); + } + if (nread == 0) { + fd_open[i] = 0; + if (max_fd == i) { + while (fd_open[i] == 0) { + i--; + } + max_fd = i; + break; + } + continue; + } + nwrite = FullWrite(fd, buffer, nread); + if (nwrite) { + PrintErr("Errore in scrittura"); + exit(1); + } + } + } + if (n != 0) { + PrintErr("Errore nella gestione dei file descriptor"); + } + } + /* normal exit, never reached */ + exit(0); +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Elementary echo server\n"); + printf("Usage:\n"); + printf(" echod [-h] \n"); + printf(" -h print this help\n"); + printf(" -d write debug info\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 print error on stout or syslog + */ +void PrintErr(char * error) { + if (demonize) { /* daemon mode */ + syslog(LOG_ERR, "%s: %m", error); /* log string and error message */ + } else { + perror(error); + } + return; +} diff --git a/tcpsock.tex b/tcpsock.tex index 852e4a6..35c5ef2 100644 --- a/tcpsock.tex +++ b/tcpsock.tex @@ -635,8 +635,11 @@ precedente in \secref{sec:sock_socket}. \subsection{La funzione \func{bind}} \label{sec:TCP_func_bind} -La funzione \funcd{bind} assegna un indirizzo locale ad un socket. È usata -cioè per specificare la prima parte dalla socket pair. Viene usata sul lato +La funzione \funcd{bind} assegna un indirizzo locale ad un +socket.\footnote{nel nostro caso la utilizzeremo per socket TCP, ma la + funzione è generica e deve essere usata per qualunque tipo di socket + \texttt{SOCK\_STREAM} prima che questo possa accettare connessioni.} È usata +cioè per specificare la prima parte dalla socket pair. Viene usata sul lato server per specificare la porta (e gli eventuali indirizzi locali) su cui poi ci si porrà in ascolto. Il prototipo della funzione è il seguente: \begin{prototype}{sys/socket.h} @@ -746,9 +749,17 @@ staticamente a \const{IN6ADRR\_LOOPBACK\_INIT}. \label{sec:TCP_func_connect} La funzione \funcd{connect} è usata da un client TCP per stabilire la -connessione con un server TCP, il prototipo della funzione è il seguente: +connessione con un server TCP,\footnote{di nuovo la funzione è generica e + supporta vari tipi di socket, la differenza è che per socket senza + connessione come quelli di tipo \texttt{SOCK\_DGRAM} la sua chiamata si + limiterà ad impostare l'indirizzo dal quale e verso il quale saranno inviati + e ricevuti i pacchetti, mentre per socket di tipo \texttt{SOCK\_STREAM} o + \texttt{SOCK\_SEQPACKET}, essa attiverà la procedura di avvio (nel caso del + TCP il \textit{three-way-handsjake}) della connessione.} il prototipo della +funzione è il seguente: \begin{prototype}{sys/socket.h} -{int connect(int sockfd, const struct sockaddr *servaddr, socklen\_t addrlen)} + {int connect(int sockfd, const struct sockaddr *servaddr, socklen\_t + addrlen)} Stabilisce una connessione fra due socket. @@ -848,12 +859,14 @@ necessario effettuare una \func{bind}. \label{sec:TCP_func_listen} La funzione \funcd{listen} serve ad usare un socket in modalità passiva, cioè, -come dice il nome, per metterlo in ascolto di eventuali connessioni; in -sostanza l'effetto della funzione è di portare il socket dallo stato -\texttt{CLOSED} a quello \texttt{LISTEN}. In genere si chiama la funzione in -un server dopo le chiamate a \func{socket} e \func{bind} e prima della -chiamata ad \func{accept}. Il prototipo della funzione, come definito dalla -pagina di manuale, è: +come dice il nome, per metterlo in ascolto di eventuali +connessioni;\footnote{questa funzione può essere usata con socket che + supportino le connessioni, cioè di tipo \texttt{SOCK\_STREAM} o + \texttt{SOCK\_SEQPACKET}.} in sostanza l'effetto della funzione è di portare +il socket dallo stato \texttt{CLOSED} a quello \texttt{LISTEN}. In genere si +chiama la funzione in un server dopo le chiamate a \func{socket} e \func{bind} +e prima della chiamata ad \func{accept}. Il prototipo della funzione, come +definito dalla pagina di manuale, è: \begin{prototype}{sys/socket.h}{int listen(int sockfd, int backlog)} Pone un socket in attesa di una connessione. @@ -972,10 +985,13 @@ trasparente dal protocollo TCP. \label{sec:TCP_func_accept} La funzione \funcd{accept} è chiamata da un server per gestire la connessione -una volta che sia stato completato il \textit{three way handshake}, la -funzione restituisce un nuovo socket descriptor su cui si potrà operare per -effettuare la comunicazione. Se non ci sono connessioni completate il processo -viene messo in attesa. Il prototipo della funzione è il seguente: +una volta che sia stato completato il \textit{three way + handshake},\footnote{la funzione è comunque generica ed è utilizzabile su + socket di tipo \texttt{SOCK\_STREAM}, \texttt{SOCK\_SEQPACKET} e + \texttt{SOCK\_RDM}.} la funzione restituisce un nuovo socket descriptor su +cui si potrà operare per effettuare la comunicazione. Se non ci sono +connessioni completate il processo viene messo in attesa. Il prototipo della +funzione è il seguente: \begin{prototype}{sys/socket.h} {int accept(int sockfd, struct sockaddr *addr, socklen\_t *addrlen)} @@ -1514,21 +1530,21 @@ degli altri esempi. \label{fig:TCP_daytime_cunc_server_code} \end{figure} -Stavolta (\texttt{\small 21--25}) la funzione \func{accept} è chiamata +Stavolta (\texttt{\small 21--26}) la funzione \func{accept} è chiamata fornendo una struttura di indirizzi in cui saranno ritornati l'indirizzo IP e la porta da cui il client effettua la connessione, che in un secondo tempo, -(\texttt{\small 39--43}), se il logging è abilitato, stamperemo sullo standard +(\texttt{\small 40--44}), se il logging è abilitato, stamperemo sullo standard output. Quando \func{accept} ritorna il server chiama la funzione \func{fork} -(\texttt{\small 26--30}) per creare il processo figlio che effettuerà -(\texttt{\small 31--45}) tutte le operazioni relative a quella connessione, +(\texttt{\small 27--31}) per creare il processo figlio che effettuerà +(\texttt{\small 32--46}) tutte le operazioni relative a quella connessione, mentre il padre proseguirà l'esecuzione del ciclo principale in attesa di ulteriori connessioni. Si noti come il figlio operi solo sul socket connesso, chiudendo -immediatamente (\texttt{\small 32}) il socket \var{list\_fd}; mentre il padre -continua ad operare (\texttt{\small 47}) solo sul socket in ascolto chiudendo +immediatamente (\texttt{\small 33}) il socket \var{list\_fd}; mentre il padre +continua ad operare solo sul socket in ascolto chiudendo (\texttt{\small 48}) \var{sock\_fd} al ritorno dalla \func{fork}. Per quanto abbiamo detto in \secref{sec:TCP_func_close} nessuna delle due chiamate a \func{close} causa l'innesco della sequenza di chiusura perché il numero di riferimenti al file @@ -1540,7 +1556,7 @@ lo stesso vale per \var{sock\_fd} dopo il ritorno di \func{accept}, ma dopo la entrambi i socket si trovano con due referenze. Questo fa si che quando il padre chiude \var{sock\_fd} esso resta con una referenza da parte del figlio, e sarà definitivamente chiuso solo quando quest'ultimo, dopo aver completato -le sue operazioni, chiamerà (\texttt{\small 44}) la funzione \func{close}. +le sue operazioni, chiamerà (\texttt{\small 45}) la funzione \func{close}. In realtà per il figlio non sarebbe necessaria nessuna chiamata a \func{close}, in quanto con la \func{exit} finale (\texttt{\small 45}) tutti i @@ -1559,18 +1575,18 @@ per ogni processo) e soprattutto nessuna delle connessioni con i client verrebbe chiusa. Come per ogni server iterativo il lavoro di risposta viene eseguito -interamente dal processo figlio. Questo si incarica (\texttt{\small 33}) di +interamente dal processo figlio. Questo si incarica (\texttt{\small 34}) di chiamare \func{time} per leggere il tempo corrente, e di stamparlo -(\texttt{\small 34}) sulla stringa contenuta in \var{buffer} con l'uso di +(\texttt{\small 35}) sulla stringa contenuta in \var{buffer} con l'uso di \func{snprintf} e \func{ctime}. Poi la stringa viene scritta (\texttt{\small - 35--38}) sul socket, controllando che non ci siano errori. Anche in questo + 36--39}) sul socket, controllando che non ci siano errori. Anche in questo caso si è evitato il ricorso a \func{FullWrite} in quanto la stringa è estremamente breve e verrà senz'altro scritta in un singolo segmento. Inoltre nel caso sia stato abilitato il \textit{logging} delle connessioni, si -provvede anche (\texttt{\small 39--42}) a stampare sullo standard output -l'indirizzo e la porta da cui il client ha effettuato la connessione, usando -i valori contenuti nelle strutture restituite da \func{accept}, eseguendo le +provvede anche (\texttt{\small 40--43}) a stampare sullo standard output +l'indirizzo e la porta da cui il client ha effettuato la connessione, usando i +valori contenuti nelle strutture restituite da \func{accept}, eseguendo le opportune conversioni con \func{inet\_ntop} e \func{atohs}. Ancora una volta l'esempio è estremamente semplificato, si noti come di nuovo @@ -1779,15 +1795,15 @@ alla funzione \code{PrintErr}, riportata in \figref{fig:TCP_PrintErr}, al posto di \func{perror} per la stampa degli errori. Si inizia con il porre (\texttt{\small 37--41}) in ascolto il socket, e poi si -esegue indefinitamente il ciclo principale (\texttt{\small 42--58}). -All'interno di questo si ricevono (\texttt{\small 43--46}) le connessioni, -creando (\texttt{\small 47--50}) un processo figlio per ciascuna di esse. -Quest'ultimo (\texttt{\small 51--55}), chiuso (\texttt{\small 52}) il -\textit{listening socket}, esegue (\texttt{\small 53}) la funzione di gestione -del servizio \code{ServEcho}, ed al ritorno di questa (\texttt{\small 54}) -esce. - -Il padre invece si limita (\texttt{\small 56}) a chiudere il \textit{connected +esegue indefinitamente il ciclo principale (\texttt{\small 42--59}). +All'interno di questo si ricevono (\texttt{\small 43--47}) le connessioni, +creando (\texttt{\small 48--51}) un processo figlio per ciascuna di esse. +Quest'ultimo (\texttt{\small 52--56}), chiuso (\texttt{\small 53}) il +\textit{listening socket}, esegue (\texttt{\small 54}) la funzione di gestione +del servizio \code{ServEcho}, ed al ritorno di questa esce (\texttt{\small + 55}). + +Il padre invece si limita (\texttt{\small 57}) a chiudere il \textit{connected socket} per ricominciare da capo il ciclo in attesa di nuove connessioni. In questo modo si ha un server concorrente. La terminazione del padre non è gestita esplicitamente, e deve essere effettuata inviando un segnale al @@ -2143,7 +2159,7 @@ figli stessi e non risentono di \const{SIGCHLD}. Per questo l'unica modifica sostanziale nel ciclo principale (\texttt{\small 23--42}), rispetto precedente versione di \figref{fig:TCP_ServEcho_first}, è -nella sezione (\texttt{\small 26--30}) in cui si effettua la chiamata di +nella sezione (\texttt{\small 25--31}) in cui si effettua la chiamata di \func{accept}. Quest'ultima viene effettuata (\texttt{\small 26--27}) all'interno di un ciclo di \code{while}\footnote{la sintassi del C relativa a questo ciclo può non essere del tutto chiara. In questo caso infatti si è @@ -2155,12 +2171,12 @@ altri casi si esce in caso di errore effettivo (\texttt{\small 27--29}), altrimenti il programma prosegue. Si noti che in questa nuova versione si è aggiunta una ulteriore sezione -(\texttt{\small 31--39}) di aiuto per il debug del programma, che eseguita con -un controllo (\texttt{\small 31}) sul valore della variabile \var{debugging} +(\texttt{\small 32--40}) di aiuto per il debug del programma, che eseguita con +un controllo (\texttt{\small 33}) sul valore della variabile \var{debugging} impostato dall'opzione \texttt{-d}. Qualora questo sia nullo, come -preimpostato, non accade nulla. altrimenti (\texttt{\small 32}) l'indirizzo +preimpostato, non accade nulla. altrimenti (\texttt{\small 33}) l'indirizzo ricevuto da \var{accept} viene convertito in una stringa che poi -(\texttt{\small 33--38}) viene opportunamente stampata o sullo schermo o nei +(\texttt{\small 34--39}) viene opportunamente stampata o sullo schermo o nei log. -- 2.30.2