X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=elemtcp.tex;h=fc1d1d754ac362ea8e0caeee8f93579599fce698;hp=efd8a05810496e08f9d12053cdf117608bab15b9;hb=06661f47754a536098afe2b30cb04469918f2fa3;hpb=bdf6e88eeb9b3aef06d57930ec8b89083639e56d diff --git a/elemtcp.tex b/elemtcp.tex index efd8a05..fc1d1d7 100644 --- a/elemtcp.tex +++ b/elemtcp.tex @@ -700,10 +700,7 @@ Per specificare un indirizzo generico con IPv4 si usa il valore \const{INADDR\_ANY}, il cui valore, come visto anche negli esempi precedenti è pari a zero, nell'esempio \figref{fig:net_serv_code} si è usata un'assegnazione immediata del tipo: - -\begin{lstlisting}[stepnumber=0,frame=]{} - serv_add.sin_addr.s_addr = htonl(INADDR_ANY); /* connect from anywhere */ -\end{lstlisting} +\includecodesnip{listati/serv_addr_sin_addr.c} Si noti che si è usato \func{htonl} per assegnare il valore \const{INADDR\_ANY}, benché essendo questo pari a zero il riordinamento sia @@ -741,9 +738,7 @@ Per questo motivo nell'header \file{netinet/in.h} \const{in6addr\_any} (dichiarata come \direct{extern}, ed inizializzata dal sistema al valore \const{IN6ADRR\_ANY\_INIT}) che permette di effettuare una assegnazione del tipo: -\begin{lstlisting}[stepnumber=0,frame=]{} - serv_add.sin6_addr = in6addr_any; /* connect from anywhere */ -\end{lstlisting} +\includecodesnip{listati/serv_addr_sin6_addr.c} in maniera analoga si può utilizzare la variabile \const{in6addr\_loopback} per indicare l'indirizzo di \textit{loopback}, che a sua volta viene inizializzata staticamente a \const{IN6ADRR\_LOOPBACK\_INIT}. @@ -1121,61 +1116,11 @@ rispetto al precedente esempio. Al solito il sorgente completo del server \file{ElemDaytimeTCPCuncServ.c} è allegato nella directory dei sorgenti. \begin{figure}[!htb] - \footnotesize - \begin{lstlisting}{} -#include /* predefined types */ -#include /* include unix standard library */ -#include /* IP addresses conversion utililites */ -#include /* socket library */ -#include /* include standard I/O library */ -#include - -int main(int argc, char *argv[]) -{ - int list_fd, conn_fd; - int i; - struct sockaddr_in serv_add, client; - char buffer[MAXLINE]; - socklen_t len; - time_t timeval; - pid_t pid; - int logging=0; - ... - /* write daytime to client */ - while (1) { - if ( (conn_fd = accept(list_fd, (struct sockaddr *)&client, &len)) - <0 ) { - perror("accept error"); - exit(-1); - } - /* fork to handle connection */ - if ( (pid = fork()) < 0 ){ - perror("fork error"); - exit(-1); - } - if (pid == 0) { /* child */ - close(list_fd); - 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); - } - if (logging) { - inet_ntop(AF_INET, &client.sin_addr, buffer, sizeof(buffer)); - printf("Request from host %s, port %d\n", buffer, - ntohs(client.sin_port)); - } - close(conn_fd); - exit(0); - } else { /* parent */ - close(conn_fd); - } - } - /* normal exit, never reached */ - exit(0); -} - \end{lstlisting} + \footnotesize \centering + \begin{minipage}[c]{15cm} + \includecodesample{listati/ElemDaytimeTCPCuncServ.c} + \end{minipage} + \normalsize \caption{Esempio di codice di un server concorrente elementare per il servizio daytime.} \label{fig:TCPel_serv_code}