The 11th Chapter Network programming
We need to understand the basic 客户端-服务端 programming model and how to write programs that use the services provided by the Internet 客户端-服务端 .
Finally, we will combine all these concepts to develop a small but fully functional Web server that Web provides static and dynamic text and graphics content for real browsers.
11.1 Client-Server programming model
Each network application is based on 客户端 - 服务器模型 the
11.2 Network
客户端and 服务端 typically run on different hosts, and through 计算机网络 the hardware and software resources to communicate.
- For a host, it
网络 is just another I/O device, as a data source and data receiver .
Extension Introduction以太网
Each 以太网适配器(网卡) has a globally unique 48 bit address, which is stored on the adapter ROM ( MAC ).
Using some 电缆 and called 网桥(bridge) small boxes, multiple 以太网段 can be connected to the larger LAN, called 桥接以太网(bridged Ethernet) .
- Some
电缆 connect bridges with bridges, or bridges with hubs.
- The bandwidth of these cables can be different.
At higher levels of the hierarchy, multiple incompatible LANs can be 路由器(router) connected by a special computer called ainternet(互联网络)
Internet and Internet
We always use lowercase letters to internet denote general concepts, capitalized to Internet denote a specific implementation, such as the global IP Internet.
WAN( Wide-Area Network , WAN)
The key features of the Internet are:
- It can be composed of a variety of LAN and WAN with completely different and incompatible technologies.
Q: How can one 源主机 send data bits across all of these incompatible networks to another one 目的主机 ?
A: The workaround is to run one layer on each host and router 协议软件 , eliminating the difference between different networks.
- This software implements a way
协议 to control how hosts and routers coordinate their work to achieve data transfer.
- Two basic competencies must be provided:
命名机制
- Each master opportunity is assigned at least one
互联网地址(internet address) , and this address uniquely identifies the host.
传送机制
协议By defining a way to bundle data bits into discrete slices (called 包 ).
- One
包 is made 包头 up 有效载荷 of and composed of.
包头
有效载荷Include data bits emitted from the source host
One 客户端 running on the host A , the host is A LAN1 connected, it sends a string of bytes of data to the server running on Host B, and Host B is connected LAN2 . There are 8 steps below.
- The client running on
A the host makes system calls from the client 虚拟地址空间 to the copy 内核缓冲区 .
AA frame is created on the host 协议软件 by attaching the internetwork 包头 and the frame header before the data LAN1 LAN1 .
互联网包头addressed to the Internet Host B. (Final Purpose)
LAN1帧头addressed to 路由器 . Broker
- Packaging
LAN1帧The payload is 互联网络包 .
互联网络包The payload is the actual user data.
- This
封装 is one of the basic methods of network interconnection.
LAN1The adapter copies that 帧 to the network.
帧arrives at the router, the router LAN1适配器 reads it from the cable, and transmits it to the Protocol software.
The router takes the 互联网包头中 destination internetwork address from the fetch, uses it as the index of the router, and determines where to forward the packet.
- The router peels the old
LAN1 frame header, plus a new frame header addressed to the host B LAN2 , and transmits the resulting frame to the adapter.
Copy of LAN2适配器 the router 帧 to the network
帧When Host B arrives, its adapter reads the frame from the cable and transmits it to the Protocol software.
- Finally, the Protocol software on Host B peels the header and the frame head. The server makes a read of this data
系统调用 .
Of course, here, we cover up a lot of very difficult problems.
- What to do if different networks have the
帧 largest values of different sizes.
- How the router knows where to forward it
帧 .
- When the network topology changes, how to notify the router.
- What happens if the bag is lost?
Even so, we can probably understand the essence of the idea of the Internet.
11.3 Global IP Internet
Each Internet host runs the TCP/IP software that implements the Protocol (transmission Control protocol/intelnet Protocol, Transmission Protocol/Internetwork Protocol), which is supported by almost all computer systems 协议 .
- The Internet's client and server mix use
套接字接口 functions and Unix I/O functions to communicate.
套接字函数This is typically implemented as a kernel-only 系统调用 implementation, and calls various kernel-mode TCP/IP functions.
TCP/IPThe protocol is actually one 协议族 , and each protocol provides different functions.
Cases
IPThe protocol provides a basic naming method, and a delivery mechanism.
- The
传递机制 ability to send packets from an Internet host to another host is also called数据报(datagram)
IPThe mechanism is unreliable in a sense, and if the datagram is lost or duplicated in the network, it does not attempt to recover.
UDP(Unreliable Datagram Protocol,不可靠数据报协议)Slightly expanded the IP protocol.
- In this way, it
包 can be 进程 transmitted between, rather than 主机 between.
TCPis a IP complex protocol built on, providing a reliable connection between processes 全双工(双向) .
To simplify the discussion
- We will look
TCP/IP at it as a separate overall agreement.
- does not discuss its internal work, only discusses
TCP and IP provides basic functionality for the application.
- Not discuss
UDP
From the programmer's point of view, we can think of the Internet as a collection of hosts around the world, satisfying the features.
- The host collection is mapped to
32 the address of a set of bits IP .
- This set
IP of addresses can be mapped to a set 因特网域名(Internet domain name) of known identifiers.
- Processes on the Internet host can
连接 communicate with processes on any other host.
11.3.1 IP Address
An IP address is a 32-bit unsigned integer. The network program IP stores the address in one IP地址结构 .
/* Internet address structure */struct in_addr{ unsigned int s_addr;}
Why use structs to store scalar IP addresses
Was an unfortunate product of the early days, but it was too late to change.
Host byte order, and network byte order
Because the Internet host can have different主机字节顺序
TCP/IPDefines a uniform 网络字节顺序(network byte order) (big-endian, x86) for any integer data item.
UnixProvides a function such as the following to implement the transformation.
An IP address is usually expressed as a 点分十进制表示法
- Here, each
字节 (8-bit) is represented by its decimal representation (0~255), and is separated by a period and other bytes.
On a Linux system, you can use hostname commands to determine the dotted decimal of your own host:
linux> hostname -i10.174.204.145
You can use inet_aton the and inet_ntoa functions to convert from one to the other.
11.3.2 Internet domain name
The mapping that is convenient for people to remember IP is 域名 .
域名集合A hierarchy is formed, and each domain name encodes its position in the hierarchy.
The internet defines 域名集合 a IP direct mapping of the address.
HOSTS.TXT
- Until the
1988 year, this mapping was HOSTS.TXT manually maintained by a text file called.
DNS:
- After that, by distributing the world-wide database (
DNS , Domain Name System , the domain Name System), to maintain.
DNSThe database is made up 主机条目结构(host entry structure) of millions.
- Defines a set
域名 of mappings between a group (an official name and an alias) and a set IP of addresses.
Internet Applications gethostbyname gethostbyaddr retrieve arbitrary host entries from the DNS database through calls and functions:
Each host has a locally defined domain namelocalhost
- This domain name is always mapped
本地送回地址(loopback address) : 127.0.0.1 .
localhostThe name provides a convenient and portable way to reference the client and server running on the same machine.
11.3.3 Internet connection
InternetThe server and the client 连接 communicate by sending and receiving on the server 字节流 .
- In the sense of connecting a pair of processes, the connection is
点对点 .
- It is from the point of view where the data can flow in both directions
全双工 .
- and the byte stream emitted from the source process is eventually received by the destination process according to the data being sent, it is
可靠
One 套接字 is 连接的 an endpoint.
Each socket has a corresponding 套接字地址 .
- is made
IP up of an address and a 16-bit integer, which is 端口 used 地址:端口 to represent.
When a client initiates a connection request, The 端口 core is automatically assigned by the kernel in the client socket address.
then , the server socket address 端口 is usually a well-known port , corresponding to this service.
One 连接 is uniquely determined by the socket address at its ends.
- This is called for the socket address
套接字对(socket pair) , which is represented by the following tuple:
(cliaddr:cliport,servaddr:servport)
11.4 Socket Interface
套接字接口(socket interface)is a set of functions that are Unix I/O combined with functions to create network applications.
Gives an overview of a typical client-server transaction in the context of 套接字接口 this guidance.
11.4.1 Socket Address Structure
Different angles:
- From the
Unix kernel point of view, a socket is one of the communication 端点 .
- From the point
Unix of view of the program, the socket is an open file with a corresponding descriptor.
InternetThe socket address () is Internet-sytle stored in the 16-byte structure shown in the type sockaddr_in .
sin_familyThe members are AF_INET , IPv4 or IPv6.
sin_portThe member is a 16-bit port number.
sin_addrThe member is a 32-bit IP address.
IPThe address and port number are always stored in the network byte order (big-endian method).
sin_zerois filled, making sockaddr_in it sockaddr as big as.
sockaddr_inFor programmers to work with, the sockaddr socket function is used, and both can be cast directly.
11.4.2 Socket function
The client and server use socket functions to create a套接字描述符(socket descriptor)
openalmost like
#include<sys/types.h>#include<sys/socket.h>int socket(int domain, int type ,int protocol); 返回:若成功则为非负描述符,出错为-1
We always call the function with such parameters socket :
clientfd = Socket(AF_INET,SOCK_STREAM,0);
AF_INETSurface we are using the IPV4 protocol.
SOCK_STREAMIndicates that the socket is 连接 an endpoint of the Internet.
socketThe returned clientfd descriptor is only partially open and cannot be used for read-write.
- How to complete the job of opening a socket depends on whether we are a client or a server.
- The next section describes how to open a socket when we are a client.
11.4.3 Connect function
connectthe client establishes a connection to the server by calling the function
#include<sys/socket.h>int connect(int sockfd,struct sockaddr *serv_addr,int addrlen); 返回:若成功则为0,若出错则为-1
connectThe function attempts to serv_addr establish an Internet connection to the server with the socket address.
- Which
addrlen is sizeof(sockaddr_in) .
connectThe function blocks until the connection is successfully established or an error occurs.
- If successful, the
sockfd descriptor can be read and written.
- And get the link is portrayed by the socket pair
(x:y,serv_addr.sin_addr,serv_addr.sin_port) .
xThis is the client IP address, which y represents the ephemeral port.
- It uniquely establishes the client process on the client host.
11.4.4 open_clientfd function
open_cilentfdYes socket and connect the wrapper function (not the system comes with)
#include <csapp.h>int open_clientfd(char *hostname, int port); 返回:若成功则为描述符,若`Unix`出错则为-1,DNS出错则为-2.
open_clientfdThe function establishes a connection with the server running on and hostname listens for connection requests on well-known ports port .
- It returns an open socket descriptor.
- This descriptor is ready to be used for
Unix I/O input and output by functions.
11.4.5 bind function
The rest of the socket functions bind , listen and accept is used by the server to establish a link with the client.
#include<sys/socket.h>int bind(int sockfd,struct sockaddr *my_addr,int addrlen); //返回: 若成功则为0,若出错则为-1
bindThe function tells the kernel to my_addr link the server socket address and the socket descriptor in sockfd .
- Parameter
addrlen issizeof(sockaddr_in)
?
11.4.6 Listen function (active socket, listener socket)
The client is the active entity that initiates the connection request. The server is a passive entity that waits for a request from a client connection.
By default , the kernel considers socket the descriptor for the function creation to correspond to 主动套接字(active socket) .
- It exists on a connected client.
The server call listen tells the kernel that the descriptor is used by the server rather than by the client
#include<sys/socket.h>int listen(int sockfd,int backlog); 返回:若成功则为0,若出错则为-1
listenThe function will be sockfd converted from one 主动套接字 to one 监听套接字(listenning socket) .
- The socket can receive connection requests from the client.
backlogParameter implies the number of outstanding connection requests that the kernel should wait in the queue before it starts rejecting the connection request.
backlogThe exact meaning of the parameter requires an understanding of the TCP/IP protocol, which is beyond the scope of our discussion.
- Usually we will set it to a larger value, for example
1024 .
11.4.7 OPEN_LISTENFD function
With socket , bind and the listen function is called open_listenfd the packing function.
The server can use it to create one 监听描述符 .
#include<csapp.h>int open_listenfd(int port) 返回:若成功则为描述符,若Unix出错则为-1
open_listenfdfunction to open and return a listener descriptor
- This descriptor is ready to
port receive requests on well-known ports.
- Creates a
listenfd socket descriptor.
Use setsockopt a function to configure the server so that it can be immediately aborted and restarted.
- By default, a restarted server rejects the client's connection request for approximately 30 seconds, which is a serious impediment to debugging.
Next, initialize the server's 套接字地址结构 .
- Use
INADDR_ANY to tell the kernel that the server will receive any IP address to port port requests.
INADDR_ANYThe wildcard address is the address at the specified address 0.0.0.0 .
- Called
blind , listen . Convert it to 监听套接字 .
11.4.8 Accept function
CSAPPHere is a very problematic, so specifically turned over UNIX 网络编程 the exact words.
监听描述符and 已连接描述符 the difference between a lot of people confused.
11.4.9 echo Client and server examples
The best way to learn the socket interface is to study the sample code.
No way. This code is estimated to have a lot of doubts, or a period of time to chewUnix网络编程把
11.5 Web Server
This piece has been written servlet , there is no need to repeat, later in detail to fill
[Csapp notes] [11th Chapter Network Programming]