深入理解计算机:网络编程(一)

客户端——服务端编程模型

简介
  • 定义

    • 一个网络应用是由一个服务器进程和一个或者多个客户端进程组成,服务端管理某种资源,并且通过操作这种资源来为它的客户端提供某种服务。
  • 举例

    • a Web server manages a set of disk files that it retrieves and executes on behalf of clients.
    • An FTP server manages a set of disk files that it stores and retrieves for clients.
    • An email server manages a spool file that it reads and updates for clients.
  • 基本操作

    • 客户端——服务器模型中基本操作是事务。
客户端——服务端事务
  • 事务模型图
    在这里插入图片描述

  • 步骤

    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.
注意
  • 进程点对点
    • 客户端和服务器是进程,而不是常常提到的机器或者主机
  • 事务可以跨主机
    • 一台主机可以同时运行在不同的客户端和服务端,而且一个客户端和服务端的事务可以在同一台或是不同的主机上运行。
网络

I/O设备——网络
  • 简介

    • 对于一个主机而言,网络只是又一种I/O设备,作为数据源和数据接收方。
  • 网络主机的硬件图
    网络主机的硬件图

以太网段
  • 组成

    • 由电缆(通常是双绞线)和一个叫做集线器的小盒子组成
  • 概念图
    以太网段概念图

桥接以太网
  • 组成

    • 使用电缆和叫做网桥(bridge)的小盒子,多个以太网段可以连接成较大的局域网,称为桥接以太网。
  • 概念图
    桥接以太网概念图

路由器、互联网——局域网与广域网互联
  • 局域网

    • 集线器+网桥+电缆
  • 局域网概念图
    局域网概念图

  • 互联网络

    • 在层次的更高级别中,多个不兼容的局域网可以通过叫做路由器的特殊计算机连接起来,组成一个互联网络(internet)
  • 互联网概念图
    互联网概念图

不同主机间网络通信
  • 通信图
    通信图

  • 步骤

    1. The client on host A invokes a system call that copies the data from the client’s virtual address space into a kernel buffer.
    2. The protocol software on host A creates a LAN1 frame by appending an internet header and a LAN1 frame header to the data. The internet header is addressed to internet host B. The LAN1 frame header is addressed to the router. It then passes the frame to the adapter. Notice that the payload of the LAN1 frame is an internet packet, whose payload is the actual user data. This kind of encapsulation is one of the fundamental insights of internetworking.
    3. The LAN1 adapter copies the frame to the network.
    4. When the frame reaches the router, the router’s LAN1 adapter reads it from the wire and passes it to the protocol software.
    5. The router fetches the destination internet address from the internet packet header and uses this as an index into a routing table to determine where to forward the packet, which in this case is LAN2. The router then strips off the old LAN1 frame header, prepends a new LAN2 frame header addressed to host B, and passes the resulting frame to the adapter.
    6. The router’s LAN2 adapter copies the frame to the network.
    7. When the frame reaches host B, its adapter reads the frame from the wire and passes it to the protocol software.
    8. Finally, the protocol software on host B strips off the packet header and frame header. The protocol software will eventually copy the resulting data into the server’s virtual address space when the server invokes a system call that reads the data.
全球IP因特网

IP地址
  • 因特网客户端——服务端应用程序的基本硬件和软件组织
    因特网客户端——服务端应用程序的基本硬件和软件组织

  • 因特网(特性)——一个世界范围的主机集合

    • 主机集合映射为一组32位的IP地址
    • 这组IP地址被映射为一组称为因特网域名(Internet domain name)的标识符
    • 因特网主机上的进程能够通过连接(connection)和任何其他因特网主机上的进程通信
  • IP地址
    IP地址

    • 一个32位无符号整数
    • IP地址通常用点分十进制表示法来表示
    • 例如:128.2.194.242 就是地址0x8002c2f2的点分十进制表示
    • inet_aton 函数将一个点分十进制(cp)转化为一个网络字节顺序的IP地址(inp)
    • inet_ntoa 函数将一个个网络字节顺序的IP地址转化为它所对应的点分十进制串
因特网域名
  • 产生原因

    • IP地址(大整数)很难记忆,另定义了一组更加人性化的域名,以及一种将域名映射到IP地址的机制。
    • 域名是一串用句点分隔的单词(字母、数字和破折号),例如 kittyhawk.cmcl.cs.cmu.edu
  • 域名的层级
    域名的层级

    • 常见的一级域名有com、edu、gov、org和net
    • 一旦一个组织得到了一个二级域名,那么它就可以在这个子域中创建任何新的域名了
  • DNS域名系统
    在这里插入图片描述

    • 由上百万的主机条目结构组成,其中每条定义了一组域名和一组IP地址之间的映射
因特网连接
  • 套接字地址

    • 一个套接字是连接的一个端点,每个套接字都有相应的套接字地址,是由一个因特网地址和一个16位的整数端口组成的,用”地址:端口”来表示
    • 当客户端发起一个连接请求时,客户端套接字地址中的端口是由内核自动分配的,称为临时端口。然而,服务器套接字地址中的端口通常是某个知名的端口,是和这个服务对应的。
  • 连接

    • 一个连接是它两端的套接字地址是唯一确定的,这对套接字地址叫做套接字对,由下列元组来表示(cliaddr:cliport,servaddr:servport)
    • 其中cliaddr是客户端的IP地址,cliport是客户端的端口,servaddr是服务器的IP地址,而servport是服务器的端口。
  • 例如
    套接字对连接

    • Web客户端的套接字地址是128.2.194.242:51213,其中端口号51213是内核分配的临时端口号
    • Web服务端的套接字地址是208.216.181.15:80,其中端口是和Web服务相关联的知名端口号