\begin{figure}[!htb]
\footnotesize \centering
\begin{minipage}[c]{15cm}
- \begin{lstlisting}[stepnumber=0]{}
-struct sockaddr {
- sa_family_t sa_family; /* address family: AF_xxx */
- char sa_data[14]; /* address (protocol-specific) */
-};
- \end{lstlisting}
+ \includestruct{listati/sockaddr.h}
\end{minipage}
\caption{La struttura generica degli indirizzi dei socket
\structd{sockaddr}.}
\begin{figure}[!htb]
\footnotesize\centering
\begin{minipage}[c]{15cm}
- \begin{lstlisting}[stepnumber=0]{}
-struct sockaddr_in {
- sa_family_t sin_family; /* address family: AF_INET */
- in_port_t sin_port; /* port in network byte order */
- struct in_addr sin_addr; /* internet address */
-};
-/* Internet address. */
-struct in_addr {
- in_addr_t s_addr; /* address in network byte order */
-};
- \end{lstlisting}
+ \includestruct{listati/sockaddr_in.h}
\end{minipage}
\caption{La struttura degli indirizzi dei socket internet (IPv4)
\structd{sockaddr\_in}.}
\begin{figure}[!htb]
\footnotesize \centering
\begin{minipage}[c]{15cm}
- \begin{lstlisting}[stepnumber=0]{}
-struct sockaddr_in6 {
- uint16_t sin6_family; /* AF_INET6 */
- in_port_t sin6_port; /* port number */
- uint32_t sin6_flowinfo; /* IPv6 flow information */
- struct in6_addr sin6_addr; /* IPv6 address */
- uint32_t sin6_scope_id; /* Scope id (new in 2.4) */
-};
-struct in6_addr {
- uint8_t s6_addr[16]; /* IPv6 address */
-};
- \end{lstlisting}
+ \includestruct{listati/sockaddr_in6.h}
\end{minipage}
\caption{La struttura degli indirizzi dei socket IPv6
\structd{sockaddr\_in6}.}
\begin{figure}[!htb]
\footnotesize \centering
\begin{minipage}[c]{15cm}
- \begin{lstlisting}[stepnumber=0]{}
-#define UNIX_PATH_MAX 108
-struct sockaddr_un {
- sa_family_t sun_family; /* AF_UNIX */
- char sun_path[UNIX_PATH_MAX]; /* pathname */
-};
- \end{lstlisting}
+ \includestruct{listati/sockaddr_un.h}
\end{minipage}
\caption{La struttura degli indirizzi dei socket locali (detti anche
\textit{unix domain}) \structd{sockaddr\_un} definita in \file{sys/un.h}.}
\begin{figure}[!htb]
\footnotesize \centering
\begin{minipage}[c]{15cm}
- \begin{lstlisting}[stepnumber=0]{}
-struct sockaddr_atalk {
- sa_family_t sat_family; /* address family */
- uint8_t sat_port; /* port */
- struct at_addr sat_addr; /* net/node */
-};
-struct at_addr {
- uint16_t s_net;
- uint8_t s_node;
-};
- \end{lstlisting}
+ \includestruct{listati/sockaddr_atalk.h}
\end{minipage}
\caption{La struttura degli indirizzi dei socket AppleTalk
\structd{sockaddr\_atalk}.}
\begin{figure}[!htb]
\footnotesize \centering
\begin{minipage}[c]{15cm}
- \begin{lstlisting}[stepnumber=0]{}
-struct sockaddr_ll {
- unsigned short sll_family; /* Always AF_PACKET */
- unsigned short sll_protocol; /* Physical layer protocol */
- int sll_ifindex; /* Interface number */
- unsigned short sll_hatype; /* Header type */
- unsigned char sll_pkttype; /* Packet type */
- unsigned char sll_halen; /* Length of address */
- unsigned char sll_addr[8]; /* Physical layer address */
-};
- \end{lstlisting}
+ \includestruct{listati/sockaddr_ll.h}
\end{minipage}
\caption{La struttura \structd{sockaddr\_ll} degli indirizzi dei
\textit{packet socket}.}
\secref{sec:ipc_pipes}).
\begin{figure}[htb]
- \centering
- \footnotesize
- \begin{lstlisting}{}
-#include <unistd.h>
-
-ssize_t FullRead(int fd, void *buf, size_t count)
-{
- size_t nleft;
- ssize_t nread;
-
- nleft = count;
- while (nleft > 0) { /* repeat until no left */
- if ( (nread = read(fd, buf, nleft)) < 0) {
- if (errno == EINTR) { /* if interrupted by system call */
- continue; /* repeat the loop */
- } else {
- return(nread); /* otherwise exit */
- }
- } else if (nread == 0) { /* EOF */
- break; /* break loop here */
- }
- nleft -= nread; /* set left to read */
- buf +=nread; /* set pointer */
- }
- return (count - nleft);
-}
- \end{lstlisting}
+ \footnotesize \centering
+ \begin{minipage}[c]{15cm}
+ \includecodesample{listati/FullRead.c}
+ \end{minipage}
+ \normalsize
\caption{Funzione \func{FullRead}, legge esattamente \var{count} byte da un
file descriptor, iterando opportunamente le letture.}
\label{fig:sock_FullRead_code}
\begin{figure}[htb]
\centering
- \footnotesize
- \begin{lstlisting}{}
-#include <unistd.h>
-
-ssize_t FullWrite(int fd, const void *buf, size_t count)
-{
- size_t nleft;
- ssize_t nwritten;
-
- nleft = count;
- while (nleft > 0) { /* repeat until no left */
- if ( (nwritten = write(fd, buf, nleft)) < 0) {
- if (errno == EINTR) { /* if interrupted by system call */
- continue; /* repeat the loop */
- } else {
- return(nwritten); /* otherwise exit with error */
- }
- }
- nleft -= nwritten; /* set left to write */
- buf +=nwritten; /* set pointer */
- }
- return (count);
-}
- \end{lstlisting}
+ \footnotesize \centering
+ \begin{minipage}[c]{15cm}
+ \includecodesample{listati/FullWrite.c}
+ \end{minipage}
+ \normalsize
\caption{Funzione \func{FullWrite}, scrive \var{count} byte su un socket.}
\label{fig:sock_FullWrite_code}
\end{figure}
richiesta.
\begin{figure}[!htb]
- \footnotesize
- \begin{lstlisting}{}
-#include <sys/types.h> /* predefined types */
-#include <unistd.h> /* include unix standard library */
-#include <arpa/inet.h> /* IP addresses conversion utilities */
-#include <sys/socket.h> /* socket library */
-#include <stdio.h> /* include standard I/O library */
-
-int main(int argc, char *argv[])
-{
- int sock_fd;
- int i, nread;
- struct sockaddr_in serv_add;
- char buffer[MAXLINE];
- ...
- /* 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(13); /* daytime post is 13 */
- /* 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 */
- while ( (nread = read(sock_fd, buffer, MAXLINE)) > 0) {
- buffer[nread]=0;
- if (fputs(buffer, stdout) == EOF) { /* write daytime */
- perror("fputs error");
- return -1;
- }
- }
- /* error on read */
- if (nread < 0) {
- perror("Read error");
- return -1;
- }
- /* normal exit */
- return 0;
-}
- \end{lstlisting}
+ \footnotesize \centering
+ \begin{minipage}[c]{15cm}
+ \includecodesample{listati/ElemDaytimeTCPClient.c}
+ \end{minipage}
+ \normalsize
\caption{Esempio di codice di un client elementare per il servizio daytime.}
\label{fig:net_cli_code}
\end{figure}
directory \file{sources}.
\begin{figure}[!htbp]
- \footnotesize
- \begin{lstlisting}{}
-#include <sys/types.h> /* predefined types */
-#include <unistd.h> /* include unix standard library */
-#include <arpa/inet.h> /* IP addresses conversion utilities */
-#include <sys/socket.h> /* socket library */
-#include <stdio.h> /* include standard I/O library */
-#include <time.h>
-#define MAXLINE 80
-#define BACKLOG 10
-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;
- ...
- /* 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);
-}
- \end{lstlisting}
+ \footnotesize \centering
+ \begin{minipage}[c]{15cm}
+ \includecodesample{listati/ElemDaytimeTCPServer.c}
+ \end{minipage}
+ \normalsize
\caption{Esempio di codice di un semplice server per il servizio daytime.}
\label{fig:net_serv_code}
\end{figure}