CSAPP英语学习系列: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.

concept
美[ˈkɑːnsept]
n. 概念;观念

allocation
美[ˌæləˈkeɪʃn]
n. 分配(物)

graphics
美[ˈɡræfɪks]
n. []制图学;
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.

initiate
美[ɪˈnɪʃieɪt]
vt. 开始,发起

transaction
美[trænˈzækʃn]
n. 交易,业务,事务

interpret
美[ɪnˈtɜːrprət]
v. 诠释;领会

manipulate
美[məˈnɪpjuleɪt]
vt. 操作,处理

appropriate
美[əˈproʊprieɪt]
adj. 适当的;合适的
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.

capability
美[ˌkeɪpəˈbɪləti]
n. 能力

compatible
美[kəmˈpætəbl]
adj. 兼容的

smooth
美[smuːð]
v. 使光滑

uniform
美[ˈjuːnɪfɔːrm]
adj. 一致的,统一的

delivery
美[dɪˈlɪvəri]
n. 递送(商品)

mechanism
美[ˈmekənɪzəm]
n. []机制,机能

wire
美[ˈwaɪər]
n. 金属丝;电线

discrete
美[dɪˈskriːt]
adj. 分离的,不相关联的
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.

gram
美[ɡræm]
n. 绿豆

slightly
美[ˈslaɪtli]
adv. 稍微

complex
美[kəmˈpleks]
adj. 复杂的;复合的

duplex
美[ˈdupleks]
n. 连栋式的两栋住宅

bidirectional
美[ˌbaɪdɪˈrɛkʃənəl]
adj. 双向的

monolithic
美[ˌmɑnəˈlɪθɪk]
adj. 整体的;庞大的
11.3.1 IP Addresses

//“h” stands for host and “n” stands for network
//小端序转成大端序(网络字节序)
uint32_t htonl(uint32_t hostlong); 

//“n” stands for network and “p” stands for presentation
//把8.8.8.8转成IP结构体(ipv4结构体是in_addr)
int inet_pton(AF_INET, const char *src, void *dst); 

//把IP结构体转成8.8.8.8
const char *inet_ntop(AF_INET, const void *src, char *dst, socklen_t size);

// 示例
struct in_addr s;
inet_pton(AF_INET, "8.8.8.8", &s);
char str[INET_ADDRSTRLEN];
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.

maintain
美[meɪnˈteɪn]
v. 维持,保持

distribute
美[dɪˈstrɪbjuːt]
v. 分发

conceptually
美[kən'septʃʊrlɪ]
adv. 概念地

mathematical
美[ˌmæθəˈmætɪkl]
adj. 数学的;精确的

equivalence
美[ɪˈkwɪvələns]
n. 相等,对等
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.

duplex
美[ˈdupleks]
n. 连栋式的两栋住宅

reliable
美[rɪˈlaɪəbl]
adj. 可信赖的

barring
美[ˈbɑrɪŋ]
prep. 不包括

catastrophic
美[ˌkætəˈstrɑːfɪk]
adj. 灾难性的

proverbial
美[prəˈvɜrbiəl]
adj. 谚语的;众所周知的

eventually
美[ɪˈventʃuəli]
adv. 最后,终于
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.

conjunction
美[kənˈdʒʌŋkʃn]
n. 结合,同时发生

variant
美[ˈveriənt]
n. 变体

individual
美[ˌɪndɪˈvɪdʒuəl]
adj. 单独的;个人的
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.

specific
美[spəˈsɪfɪk]
adj. 明确的;独特的
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.

indicate
美[ˈɪndɪkeɪt]
v. 表明,暗示

generate
美[ˈdʒenəreɪt]
vt. 产生
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.

attempt
美[əˈtempt]
v. 企图

establish
美[ɪˈstæblɪʃ]
v. 创建;建立

occur
美[əˈkɜːr]
v. 发生

characterize
美[ˈkærəktəraɪz]
v. 使具有特点
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.

associate
美[əˈsəʊʃieɪt]
v. 联想,联系

descriptor
美[dɪˈskrɪptɚ]
n. 描述符
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.

hint
美[hɪnt]
n. 线索,迹象

scope
美[skoʊp]
n. 范围;眼界
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-
spectively.

respectively
美[rɪˈspektɪvli]
adv. 各自地
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.

obsolete
美[ˌɑːbsəˈliːt]
adj. 废弃的;老式的

reentrant
美[rɪ'entrənt]
n. 再进去; 可重入

inverse
美[ˌɪnˈvɜːrs]
adj. 相反的

correspond
美[ˌkɔːrəˈspɑːnd]
v. 符合;相当于
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.

establish
美[ɪˈstæblɪʃ]
v. 创建;建立
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.

detect
美[dɪˈtekt]
v. 发现;查明

explicitly
美[ɪk'splɪsɪtlɪ]
adv. 明白地,明确地
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.

invent
美[ɪnˈvent]
v. 发明

memo
美[ˈmemoʊ]
n. 备忘录

propose
美[prəˈpoʊz]
v. 提议;打算

intent
美[ɪnˈtent]
n. 意图,目的

implement
美[ˈɪmplɪmənt]
vt. 实施,执行

pivotal
美[ˈpɪvətl]
adj. 关键的

exploded
美[ɪkˈsploʊdɪd]
adj. 爆破了
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.

produce
美[prəˈduːs]
v. 引起;生产
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.

handy
美[ˈhændi]
adj. 容易做的;便利
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).

arise
美[əˈraɪz]
v. 出现,产生

facto
美['fæktoʊ]
n. 事实上
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).

redirect
美[ˌridɪˈrɛkt]
vt. 使改寄

intervention
美[ˌɪntərˈvenʃn]
n. 介入,干涉

meanwhile
美[ˈmiːnwaɪl]
adv. 同时
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.

govern
美[ˈɡʌvərn]
v. 统治;支配
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!