[Csapp notes] [11th Chapter Network Programming]

Source: Internet
Author: User
Tags icann

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

    • With this model, an application is made up of a 服务器 process
      And one or more clients 进程 .

      • 服务器Manages a resource and provides some kind of service to its clients by manipulating that resource.

        • WEBThe server, on behalf of the 客户端 retrieval, executes the disk contents.
        • FTPServer for 客户端 storage and retrieval.
        • 电子邮件Server for 客户端 read and update.
      • 客户端-服务器The basic operations in the model are 事务(transaction) .

        • A 客户端-服务器 transaction consists of four steps
          • When a client needs a service, it sends a request to the server and sends one 事务 .
          • After the server receives the request, it interprets it and operates its resources in an appropriate manner.
          • The server sends one to 响应 the client and waits for the next request.
          • The client receives 响应 and processes it.
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 .

  • For the physical, the network is a hierarchical system composed of geographical distance.

    • The lowest level of
    • is LAN (local area network) : Within a building or campus range.

      • To date, the most popular LAN technology is Ethernet (Ethernet) .

        • By Xerox PARC Company filed in the mid 1970s.

          • Ethernet has proven to be highly adaptable, from 3 MB/s to ten GB/s .
        • One Ethernet segment (Ethernet segment)

          • includes some cable (usually twisted pair) and a small box called hub .

            • Each cable has the same maximum bit bandwidth
              • typically 100mb/s or 1gb/s .
              • one end is connected to the host's adapter , and the end is connected to a hub's port . The
            • hub replicates every bit that is received from one port to all other ports, without discrimination.
              • So each bit is visible to each host.
          • Ethernet segment typically spans a few small areas.

            • For example, a room or a floor of a building.

Extension Introduction以太网

Each 以太网适配器(网卡) has a globally unique 48 bit address, which is stored on the adapter ROM ( MAC ).

    • A host can send a section , called 帧(frame) , to 网段 any other host within this.

      • Each includes

        • Some fixed number of 头部(header) bits
          • The source used to represent this , and the destination address and the length of this.
        • This is the data bit thereafter 有效载荷 .
      • This is visible to each host adapter , but only the destination host actually reads it.

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.
              • 包头
                • 包的大小
                • 源主机and 目的主机 Address
              • 有效载荷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.

  1. The client running on A the host makes system calls from the client 虚拟地址空间 to the copy 内核缓冲区 .
  2. 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.
  3. LAN1The adapter copies that to the network.

  4. arrives at the router, the router LAN1适配器 reads it from the cable, and transmits it to the Protocol software.
  5. 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.
  6. Copy of LAN2适配器 the router to the network

  7. When Host B arrives, its adapter reads the frame from the cable and transmits it to the Protocol software.
  8. 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 discussUDP

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 leaf node is opposite to the 路径 root 域名 .
    • Hierarchy first layer: Unnamed root node
    • Second layer of hierarchy:一级域名(first-level domain name)

      • Defined by the non-profit organization ICANN (Internet Corporation for Assigned Names and Numbers, the Special Name Number Association).
      • Common first-level domains:,, com edu gov , org and net .
    • Third layer of hierarchy:二级域名(second-level)

      • For example: cmu.edu .
      • These domain names are ICANN assigned by each authorized agent on a first-come-first-served basis.
      • Once an organization gets a two-level domain name, it can create any new domain name in the subdomain.

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.

      • Called临时端口
    • then , the server socket address 端口 is usually a well-known port , corresponding to this service.

      • For example:

        • Web servers typically use ports80
        • e-mail server uses ports25
      • On the Unix machine, the file /etc/services contains a comprehensive list of the services provided by this machine and their well-known port numbers.

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.

    • Used to return the next completed connection from the completed Connection 队列 team header.

      • If the completed connection is empty, then the process enters the block (assuming that the socket is the default blocking mode)
    • Returns a value of three

      • 已连接标示符
      • 客户端地址
      • 客户度地址长度

监听描述符and 已连接描述符 the difference between a lot of people confused.

    • 监听描述符is an endpoint that is a client connection request .

      • It is created once and exists throughout the life cycle of the server .
    • 已连接描述符is an endpoint of a connection that has been established between the client and the server.

      • Each time the server receives a connection request, it is created once.
      • It exists only in the process of server service for a client.
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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.