CSAPP English Learning Series: Chapter 11: network programming

Network applications rely on many of the concepts that you have already
learned in our study of systems. For example, processes, signals, byte ordering,
memory mapping, and dynamic storage allocation all play important roles. There
are new concepts to master as well. You will need to understand the basic client-server programming model and how to write client-server programs that use the
services provided by the Internet. At the end, we will tie all of these ideas together
by developing a tiny but functional Web server that can serve both static and
dynamic content with text and graphics to real Web browsers.

Beauty[ ˈ k ɑː nsept]
n. Concept; concept

Beauty[ ˌæ l əˈ ke ɪʃ n]
n. Distribute (things)

Beauty[ ˈɡ r æ f ɪ ks]
n. Cartography;
11.1 The Client-Server Programming Model

1. When a client needs service, it initiates a transaction by sending a request to
the server. For example, when a Web browser needs a file, it sends a request
to a Web server.
2. The server receives the request, interprets it, and manipulates its resources in
the appropriate way. For example, when a Web server receives a request from
a browser, it reads a disk file.
3. The server sends a response to the client and then waits for the next request.
For example, a Web server sends the file back to a client.
4. The client receives the response and manipulates it. For example, after a Web
browser receives a page from the server, it displays it on the screen.

Beauty[ ɪˈ n ɪʃ ie ɪ t]
Vt. start, initiate

Us [tr] æ n ˈ z æ k ʃ n]
n. Transaction, business, transaction

Beauty[ ɪ n ˈ t ɜː rpr ə t]
v. Interpretation; understand

Beauty [M] əˈ n ɪ pjule ɪ t]
Vt. operation, processing

Beauty[ əˈ pro ʊ prie ɪ t]
Adj; appropriate
11.2 Networks

The protocol must provide two basic capabilities:
Naming scheme——Different LAN technologies have different and incompatible
ways of assigning addresses to hosts. The internet protocol smoothes these
differences by defifining a uniform format for host addresses. Each host
is then assigned at least one of these internet addresses that uniquely
identififies it.
Delivery mechanism——Different networking technologies have different and
incompatible ways of encoding bits on wires and of packaging these bits
into frames. The internet protocol smoothes these differences by defifining
a uniform way to bundle up data bits into discrete chunks called packets. A
packet consists of a header, which contains the packet size and addresses
of the source and destination hosts, and a payload, which contains data
bits sent from the source host.

Beauty[ ˌ ke ɪ p əˈ b ɪ l ə ti]
n. Ability

Beauty [k] ə m ˈ p æ t ə bl]
Adj. Compatible

SMU ːð]
v. To smooth

Beauty[ ˈ ju ː n ɪ f ɔː rm]
Consistent, uniform

Mei [D] ɪˈ l ɪ v ə ri]
n. Deliver (goods)

Beauty[ ˈ mek ə n ɪ z ə m]
n. Mechanism, function

Beauty[ ˈ wa ɪə r]
n. Metal wire; wire

Mei [D] ɪˈ skri ː t]
Separate, unrelated
11.3 The Global IP Internet

TCP/IP is actually a family of protocols, each of which contributes different
capabilities. For example, IP provides the basic naming scheme and a delivery
mechanism that can send packets, known as datagrams, from one Internet host to
any other host. The IP mechanism is unreliable in the sense that it makes no effort
to recover if datagrams are lost or duplicated in the network. UDP (Unreliable
Datagram Protocol) extends IP slightly, so that datagrams can be transferred
from process to process, rather than host to host. TCP is a complex protocol that
builds on IP to provide reliable full duplex (bidirectional) connections between
processes. To simplify our discussion, we will treat TCP/IP as a single monolithic
protocol. We will not discuss its inner workings, and we will only discuss some of
the basic capabilities that TCP and IP provide to application programs. We will
not discuss UDP.

Beauty[ ɡ r æ m]
n. Mung bean

Beauty[ ˈ sla ɪ tli]
Adv. slightly

Beauty [k] ə m ˈ pleks]
Adj. Complex; Compound

Beauty[ ˈ dupleks]
n. A two span house

Beauty[ ˌ ba ɪ d ɪˈ r ɛ k ʃə n ə l]

Beauty[ ˌ m ɑ n əˈ l ɪθɪ k]
Adj; Huge
11.3.1 IP Addresses

//“h” stands for host and “n” stands for network
//Small end order to large end order (network byte order)
uint32_t htonl(uint32_t hostlong); 

//“n” stands for network and “p” stands for presentation
//Turn into IP structure (IPv4 structure is in)_ addr)
int inet_pton(AF_INET, const char *src, void *dst); 

//Turn IP structure into
const char *inet_ntop(AF_INET, const void *src, char *dst, socklen_t size);

struct in_addr s;
inet_pton(AF_INET, "", &s);
const char *ptr = inet_ntop(AF_INET, &s, str, INET_ADDRSTRLEN);
11.3.2 Internet Domain Names

The Internet defifines a mapping between the set of domain names and the
set of IP addresses. Until 1988, this mapping was maintained manually in a sin-
gle text fifile called HOSTS.TXT. Since then, the mapping has been maintained in a
distributed worldwide database known as DNS (Domain Name System). Concep-
tually, the DNS database consists of millions of host entries, each of which defifines
the mapping between a set of domain names and a set of IP addresses. In a math-
ematical sense, think of each host entry as an equivalence class of domain names
and IP addresses. We can explore some of the properties of the DNS mappings
with the Linux nslookup program, which displays the IP addresses associated with
a domain name.

Beauty ɪ n ˈ te ɪ n]
v. Keep, keep

Mei [D] ɪˈ str ɪ bju ː t]
v. Distribution

Beauty [k] ə n'sept ʃʊ rl ɪ]

Beauty[ ˌ m æθəˈ m æ t ɪ kl]
Mathematical; mathematical; exact

Beauty[ ɪˈ kw ɪ v ə l ə ns]
n. Equal, equal
11.3.3 Internet Connections

Internet clients and servers communicate by sending and receiving streams of
bytes over connections. A connection is point-to-point in the sense that it connects
a pair of processes. It isfull duplex in the sense that data can flflow in both directions
at the same time. And it is reliable in the sense that—barring some catastrophic
failure such as a cable cut by the proverbial careless backhoe operator—the stream
of bytes sent by the source process is eventually received by the destination process
in the same order it was sent.

Beauty[ ˈ dupleks]
n. A two span house

Beauty [R] ɪˈ la ɪə bl]

Beauty[ ˈ b ɑ r ɪŋ]
Not included

Beauty[ ˌ k æ t əˈ str ɑː f ɪ k]

Mei [PR] əˈ v ɜ rbi ə l]
Adj; Well known

Beauty[ ɪˈ vent ʃ u ə li]
Finally, finally
11.4 The Sockets Interface

The sockets interface is a set of functions that are used in conjunction with the Unix
I/O functions to build network applications. It has been implemented on most
modern systems, including all Unix variants as well as Windows and Macintosh
systems. Figure 11.12 gives an overview of the sockets interface in the context of a
typical client-server transaction. You should use this picture as a road map when
we discuss the individual functions.

Beauty [k] ə n ˈ d ʒʌŋ k ʃ n]
n. Combination, simultaneous occurrence

Beauty[ ˈ veri ə nt]
n. Variants

Beauty[ ˌɪ nd ɪˈ v ɪ d ʒ u ə l]
Adj; individual
11.4.1 Socket Address Structures

The connect, bind, and accept functions require a pointer to a protocol-
specifific socket address structure. The problem faced by the designers of the sockets
interface was how to defifine these functions to accept any kind of socket address
structure. Today, we would use the generic void * pointer, which did not exist in
C at that time. Their solution was to defifine sockets functions to expect a pointer to
a generic sockaddr structure (Figure 11.13) and then require applications to cast
any pointers to protocol-specific structures to this generic structure.

Mei [sp əˈ s ɪ f ɪ k]
Adj; distinctive
11.4.2 The socket Function

where AF_INET indicates that we are using 32-bit IP addresses and SOCK_
STREAM indicates that the socket will be an end point for a connection. However,
the best practice is to use the getaddrinfo function (Section 11.4.7) to generate
these parameters automatically, so that the code is protocol-independent. We will
show you how to use getaddrinfo with the socket function in Section 11.4.8.

Beauty[ ˈɪ nd ɪ ke ɪ t]
v. To indicate or suggest

Beauty[ ˈ d ʒ en ə re ɪ t]
Vt. production
11.4.3 The connect Function

The connect function attempts to establish an Internet connection with the server
at socket address addr, where addrlen is sizeof(sockaddr_in). The connect
function blocks until either the connection is successfully established or an error
occurs. If successful, the clientfd descriptor is now ready for reading and writing,
and the resulting connection is characterized by the socket pair.

Beauty[ əˈ tempt]
v. Attempts

Beauty[ ɪˈ st æ bl ɪʃ]
v. Creation; establish

Beauty[ əˈ k ɜː r]
v. Occurrence

Beauty[ ˈ k æ r ə kt ə ra ɪ z]
v. Make characteristic of
11.4.4 The bind Function

The bind function asks the kernel to associate the server’s socket address in addr
with the socket descriptor sockfd. The addrlen argument is sizeof(sockaddr_
in). As with socket and connect, the best practice is to use getaddrinfo to
supply the arguments to bind.

Beauty[ əˈ s əʊʃ ie ɪ t]
v. Association, connection

Mei [D] ɪˈ skr ɪ pt ɚ]
n. Descriptor
11.4.5 The listen Function

The listen function converts sockfd from an active socket to a listening socket
that can accept connection requests from clients. The backlog argument is a hint
about the number of outstanding connection requests that the kernel should queue
up before it starts to refuse requests. The exact meaning of the backlog argument
requires an understanding of TCP/IP that is beyond our scope. We will typically
set it to a large value, such as 1024.

Beauty [H] ɪ nt]
n. Clue, sign

Sko ʊ p]
n. Scope; Vision
11.4.6 The accept Function

In step 1, the server calls accept and waits for a connection request to arrive on listening
descriptor 3. In step 2, the client calls the connect function, which sends a connection
request to listenfd. In step 3, the accept function opens a new connected de-
scriptor connfd (which we will assume is descriptor 4), establishes the connection
between clientfd and connfd, and then returns connfd to the application. The
client also returns from the connect, and from this point, the client and server
can pass data back and forth by reading and writing clientfd and connfd, re-

Beauty [R] ɪˈ spekt ɪ vli]
11.4.7 Host and Service Conversion

The getaddrinfo function converts string representations of hostnames, host
addresses, service names, and port numbers into socket address structures. It is
the modern replacement for the obsolete gethostbyname and getservbyname
functions. Unlike these functions, it is reentrant and works with any protocol.
The getnameinfo function is the inverse of getaddrinfo. It converts a socket ad-
dress structure to the corresponding host and service name strings. It is the modern
replacement for the obsolete gethostbyaddr and getservbyport functions, and
unlike those functions, it is reentrant and protocol-independent.

Beauty[ ˌɑː bs əˈ li ː t]
Abandoned; abandoned; Old fashioned

Beauty [R] ɪ' entr ə nt]
n. Go in again; Reentrant

Beauty[ ˌɪ n ˈ v ɜː rs]
On the contrary

Beauty[ ˌ k ɔː r əˈ sp ɑː nd]
v. In accordance with; amount to
11.4.8 Helper Functions for the Sockets Interface

The open_clientfd function establishes a connection with a server running on
host hostname and listening for connection requests on port number port. It
returns an open socket descriptor that is ready for input and output using the
Unix I/O functions. Figure 11.18 shows the code for open_clientfd.
The open_listenfd function returns a listening descriptor that is ready to receive
connection requests on port port. Figure 11.19 shows the code for open_listenfd.

Beauty[ ɪˈ st æ bl ɪʃ]
v. Creation; establish
11.4.9 Example Echo Client and Server

After the loop terminates, the client closes the descriptor. This results in an
EOF notifification being sent to the server, which it detects when it receives a return
code of zero from its rio_readlineb function. After closing its descriptor, the
client terminates. Since the client’s kernel automatically closes all open descriptors
when a process terminates, the close in line 24 is not necessary. However, it is good
programming practice to explicitly close any descriptors that you have opened.

Mei [D] ɪˈ tekt]
v. Discovery; find out

Beauty[ ɪ k'spl ɪ s ɪ tl ɪ]
Clearly, clearly
11.5.1 Web Basics

The World Wide Web was invented by Tim Berners-Lee, a software engineer working at CERN, a Swiss
physics lab. In 1989, Berners-Lee wrote an internal memo proposing a distributed hypertext system
that would connect a “web of notes with links.” The intent of the proposed system was to help CERN
scientists share and manage information. Over the next two years, after Berners-Lee implemented
the fifirst Web server and Web browser, the Web developed a small following within CERN and a few
other sites. A pivotal event occurred in 1993, when Marc Andreesen (who later founded Netscape and
Andreessen Horowitz) and his colleagues at NCSA released a graphical browser called mosaic for all
three major platforms: Linux, Windows, and Macintosh. After the release of mosaic, interest in the
Web exploded, with the number of Web sites increasing at an exponential rate. By 2015, there were
over 975,000,000 sites worldwide.

Beauty[ ɪ n ˈ vent]
v. Invention

Beauty[ ˈ memo ʊ]
n. Memorandum

Mei [PR] əˈ po ʊ z]
v. Proposal; plan

Beauty[ ɪ n ˈ tent]
n. Intention, purpose

Beauty[ ˈɪ mpl ɪ m ə nt]
Vt. to carry out

Beauty[ ˈ p ɪ v ə tl]

Beauty[ ɪ k ˈ splo ʊ d ɪ d]
It blew up
11.5.2 Web Content

1. Fetch a disk fifile and return its contents to the client. The disk fifile is known
as static content and the process of returning the fifile to the client is known as
serving static content.
2. Run an executable fifile and return its output to the client. The output produced
by the executable at run time is known as dynamic content, and the process of
running the program and returning its output to the client is known as serving
dynamic content.

Mei [PR] əˈ du ː s]
v. Cause; production
11.5.3 HTTP Transactions

Since HTTP is based on text lines transmitted over Internet connections, we can
use the Linux telnet program to conduct transactions with any Web server on
the Internet. The telnet program has been largely supplanted by ssh as a remote
login tool, but it is very handy for debugging servers that talk to clients with text
lines over connections. For example, Figure 11.24 uses telnet to request the home
page from the AOL Web server.

Beauty[ ˈ h æ ndi]
Easy to do; easy to do; facilitate
11.5.4 Serving Dynamic Content

If we stop to think for a moment how a server might provide dynamic content
to a client, certain questions arise. For example, how does the client pass any
program arguments to the server? How does the server pass these arguments
to the child process that it creates? How does the server pass other information
to the child that it might need to generate the content? Where does the child
send its output? These questions are addressed by a de facto standard called CGI
(common gateway interface).

Beauty[ əˈ ra ɪ z]
v. Come into being

Beauty ['f æ kto ʊ]
n. In fact
11.6 Putting It Together: The Tiny Web Server

Next, the child redirects the child’s standard output to the connected fifile
descriptor (line 14) and then loads and runs the CGI program (line 15). Since
the CGI program runs in the context of the child, it has access to the same open
fifiles and environment variables that existed before the call to the execve function.
Thus, everything that the CGI program writes to standard output goes directly to
the client process, without any intervention from the parent process. Meanwhile,
the parent blocks in a call to wait, waiting to reap the child when it terminates
(line 17).

Beauty[ ˌ rid ɪˈ r ɛ kt]
Vt. change to mail

Beauty[ ˌɪ nt ə r ˈ ven ʃ n]
n. Intervene in

Beauty[ ˈ mi ː nwa ɪ l]
At the same time
11.7 Summary

Web servers and their clients (such as browsers) communicate with each other
using the HTTP protocol. A browser requests either static or dynamic content
from the server. A request for static content is served by fetching a fifile from the
server’s disk and returning it to the client. A request for dynamic content is served
by running a program in the context of a child process on the server and returning
its output to the client. The CGI standard provides a set of rules that govern how
the client passes program arguments to the server, how the server passes these
arguments and other information to the child process, and how the child sends
its output back to the client. A simple but functioning Web server that serves
both static and dynamic content can be implemented in a few hundred lines of
C code.

Beauty[ ˈɡʌ v ə rn]
v. Rule; control

This work adoptsCC agreementReprint must indicate the author and the link of this article