Finita la conversione dei listati in file separati. Passato anche alla nuova
[gapil.git] / simpltcp.tex
index b4b9931413d9a08703352b2b39c54ed6c5b90b5b..cd3dce8d21f953e8b9982dbcaabb31b758647561 100644 (file)
@@ -61,61 +61,11 @@ parte, riportata in \figref{fig:TCPsimpl_serv_code}, 
 nel precedente esempio esaminato in \secref{sec:TCPel_cunc_serv}.
 
 \begin{figure}[!htb]
-  \footnotesize
-  \begin{lstlisting}{}
-/* Subroutines declaration */
-void ServEcho(int sockfd);
-/* Program beginning */
-int main(int argc, char *argv[])
-{
-    int list_fd, conn_fd;
-    pid_t pid;
-    struct sockaddr_in serv_add;
-     ...
-    /* 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);
-    }
-    /* handle echo to client */
-    while (1) {
-        /* accept connection */
-        if ( (conn_fd = accept(list_fd, NULL, NULL)) < 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);          /* close listening socket */   
-            SockEcho(conn_fd);       /* handle echo */
-            exit(0);
-        } else {             /* parent */
-            close(conn_fd);          /* close connected socket */
-        }
-    }
-    /* normal exit, never reached */
-    exit(0);
-}
-  \end{lstlisting}
+  \footnotesize \centering
+  \begin{minipage}[c]{15.6cm}
+    \includecodesample{listati/ElemEchoTCPServer.c}
+  \end{minipage} 
+  \normalsize
   \caption{Codice della funzione \code{main} della prima versione del server
     per il servizio \texttt{echo}.}
   \label{fig:TCPsimpl_serv_code}
@@ -142,19 +92,11 @@ dati di cui 
 \func{write}.
 
 \begin{figure}[!htb]
-  \footnotesize
-  \begin{lstlisting}{}
-void ServEcho(int sockfd) {
-    char buffer[MAXLINE];
-    int nread, nwrite;
-    
-    /* main loop, reading 0 char means client close connection */
-    while ( (nread = read(sockfd, buffer, MAXLINE)) != 0) {
-        nwrite = FullWrite(sockfd, buffer, nread);
-    }
-    return;
-}
-  \end{lstlisting}
+  \footnotesize \centering
+  \begin{minipage}[c]{15.6cm}
+    \includecodesample{listati/ServEcho.c}
+  \end{minipage} 
+  \normalsize
   \caption{Codice della prima versione della funzione \code{ServEcho} per la
     gestione del servizio \texttt{echo}.}
   \label{fig:TCPsimpl_server_elem_sub}
@@ -174,42 +116,13 @@ esso ricalca la struttura del precedente client per il servizio
 \texttt{daytime} (vedi \secref{sec:net_cli_sample}) ma, come per il server, lo
 si è diviso in due parti, inserendo la parte relativa alle operazioni
 specifiche previste per il protocollo \texttt{echo} in una funzione a parte.
+
 \begin{figure}[!htb]
-  \footnotesize
-  \begin{lstlisting}{}
-int main(int argc, char *argv[])
-{
-/* 
- * Variables definition  
- */
-    int sock_fd, i;
-    struct sockaddr_in serv_add;
-    ...
-    /* create socket */
-    if ( (sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-        perror("Socket creation error");
-        return -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 */
-    /* build address using inet_pton */
-    if ( (inet_pton(AF_INET, argv[optind], &serv_add.sin_addr)) <= 0) {
-        perror("Address creation error");
-        return -1;
-    }
-    /* extablish connection */
-    if (connect(sock_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
-        perror("Connection error");
-        return -1;
-    }
-    /* read daytime from server */
-    ClientEcho(stdin, sock_fd);
-    /* normal exit */
-    return 0;
-}
-  \end{lstlisting}
+  \footnotesize \centering
+  \begin{minipage}[c]{15.6 cm}
+    \includecodesample{listati/EchoServerWrong.c}
+  \end{minipage} 
+  \normalsize
   \caption{Codice della prima versione del client \texttt{echo}.}
   \label{fig:TCPsimpl_client_elem}
 \end{figure}
@@ -227,21 +140,11 @@ preoccupa di gestire la comunicazione, leggendo una riga alla volta dallo
 ricevuto in risposta dal server.
 
 \begin{figure}[!htb]
-  \footnotesize
-  \begin{lstlisting}{}
-void ClientEcho(FILE * filein, int socket) 
-{
-    char sendbuff[MAXLINE], recvbuff[MAXLINE];
-    int nread; 
-    while (fgets(sendbuff, MAXLINE, filein) != NULL) {
-        FullWrite(socket, sendbuff, strlen(sendbuff)); 
-        nread = FullRead(socket, recvbuff, strlen(sendbuff));        
-        recvbuff[nread] = 0;
-        fputs(recvbuff, stdout);
-    }
-    return;
-}
-  \end{lstlisting}
+  \footnotesize \centering
+  \begin{minipage}[c]{15.6cm}
+    \includecodesample{listati/ClientEcho.c}
+  \end{minipage} 
+  \normalsize
   \caption{Codice della prima versione della funzione \texttt{ClientEcho} per 
     la gestione del servizio \texttt{echo}.}
   \label{fig:TCPsimpl_client_echo_sub}
@@ -429,14 +332,7 @@ Per questo useremo la funzione \code{Signal}, illustrata in
 \figref{fig:sig_Signal_code}, per installare il semplice gestore che
 riceve i segnali dei processi figli terminati già visto in 
 \figref{fig:sig_sigchld_handl}; aggiungendo il seguente codice:
-\begin{lstlisting}{}
-    ...
-    /* install SIGCHLD handler */
-    Signal(SIGCHLD, sigchld_hand);  /* establish handler */
-    /* create socket */
-    ...
-\end{lstlisting}
-
+\includecodesnip{listati/sigchildhand.c}
 \noindent
 all'esempio illustrato in \figref{fig:TCPsimpl_serv_code}, e linkando il tutto
 alla funzione \code{sigchld\_hand}, si risolverà completamente il problema