第十一章 网络编程

--------------------------------------------------------
Sun 11 Feb 13:30:10 GMT 2018
--------------------------------------------------------
第十一章 网络编程

11.1 The client-Server programming Model

The fundamental operation in the client-server model is
the 'transaction' that consists of 4 steps:

1. When a client needs service, it initiates a
transaction by sending a request to the server.
2. The server receives the request, interprets it, and
manipulates its resources in the appropriate way.
3. The server sends a response to the client and then
waits for the next request.
4. The client receives the response and manipulates it.

11.2 网络

A protocol that governs how hosts and routers cooperate
in order to transfer data. The protocol must provide two
basic capabilities:

+ Naming scheme.

Internet address which is a uniform format address.

+ Delivery mechanism

Defining 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.

11.3 The Global IP Internet

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.

Internet as a worldwide collection of hosts with the
following properties:

+ the set of hosts is mapped to a set of 32-bit ip
address.
+ The set of ip addresses is mapped to a set of
identifiers called 'Internet domain name'.
+ A process on one internet host can communicate with
a process on any other internet host over a connection

11.3.1 IP addresses

Because internet hosts can have different host byte
order, TCP/IP defines a uniform network byte order
(big-ending byte order) for any interger data iterm, such
as an IP address that is carried across the network in a
packet header.

Unix provides the following functions for converting
between network and host byte order.

#include <arpa/inet.h>

// returns value in network byte order
uint32_t htonl( uint32_t hostlong);
uint16_t htons( uint16_t hostshort);

// returns value in host byte order
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

Command 'hostname -i' show dotted-decimal addres of host

Application programs can convert back and forth between
IP address and dotted-decimal string using the functions:

int inet_pton(AF_INET,const char *src, void *dst);

const char *inet_ntop(AF_INET,const void*src,
char *dst, socklen_t size);

11.3.2 internet Domain Names

use command 'nslookup' to find domain name address

11.3.3 internet connections

A socket is an end point of a connection. Each socket
has a corresponding socket address that consists of an
internet address and a 16-bit integer port and is denoted
by the notation 'address:port'.

ports is contained in /etc/services

A connection is uniquely identified by the socket
addresses of its two end (client and server) points. This
pair of socket addresses is known as a 'socket pair':

(clliaddr:cliport, servaddr:servport)

11.4 The Sockets interface

//ip socket address structure
struct sockaddr_in {
uint16_t sin_family; //protocol familly(AF_INET)
uint16_t sin_port;
struct in_add sin_addr;
unsigned char sin_zero[8];
};

//generic socket address structure(for connect,bind
// and accept)
struct sockaddr {
uint16_t sa_family;
char sa_data[14];
};

11.4.2 socket 函数

客户端和服务端使用socket函数来创建一个套接字描述符。

#include <sys/types.h>
#include <sys/socket.h>

int socket(int domain,int type, int protocol);

如:

clientfd = socket(AF_INET,SOCK_STREAM,0);

11.4.3 connect function

客户端通过调用connect函数来建立和服务器的连接。

int connect(int clientfd,const struct sockaddr *addr,
socklen_t addrlen);

addrlen is the sizeof(sockaddr_in).

11.4.4 bind function

bind, listen and accept functions is for server side.

11.4.7 Host and Service Conversion

Linux provides some powerful funcions, 'getaddrinfo'
and getnameinfo, for converting back and forth between
binary socket address structures and the string represent-
tations of hostnames, host addresses, service names and
port numbers.


The getaddrinfo function is the morden replacement for
the obsolete 'gethostbyname' and getservbyname' functions

int getaddrinfo(const char *host,const char *service,
const struct addrinfo *hints,
struct addrinfo **result);

void freeaddrinfo(struct addrinfo *result);

const char *gai_strerror(int errcode);

The getnameinfo function is the inverse of getaddrinfo.
It converts a socket address structure to the correspond-
ing host and service name stings.

It is the modern replacement for the obsolete function:
gethostbyaddr and getservbyport functions.

int getnameinfo(const struct sockaddr *sa,
socklen_t salen, char *host,size_t hostlen,
char *service, size_t servlen, int flags);

11.4.9 echo 客户端和服务器的实例

11.5 Web Servers(Web 服务器)

11.5.1 web 基础

Web服务器和客户端之间的交互是基于HTTP(hypertext
Transfer Protocol).

11.5.2 web内容

对web客户端和服务器而言,内容是与一个MIME( Multipurpose
Internet Mail Extensions,多用途网际邮件扩充协议 )类型相关
的字节序列。

Web服务器两种服务方式:

+ 取一个磁盘文件(静态服务)
+ 运行一个可执行文件,并将输出返回给客户端(动态)

11.5.4 服务动态内容

CGI( Cmmon Gateway Interface,通用网管接口 ) 标准

1,客户端参数传递

GET命令请求的参数在URI中传递。‘?’分开文件名和参数
‘&’分开参数。‘%20’ 用来表示空格。

2,服务器如何将参数传递给子进程

服务器收到以下命令后:

GET /cgi-bin/adder?15000&213 HTTP/1.1

调用fork建立子进程,并调用execve在子进程中执行adder
程序(CGI程序)。子进程将CGI环境变量QUERY_STRING设置
为'15000&213), adder程序用Linux getenv函数引用它。

3. 服务器将其他信息传递给子进程。

CGI defines lots of environment variable that a CGI
program can expect to be set when it runs.

--------------------------------------------------
Environment variable Description
--------------------------------------------------
QUERY_STRING program arguments
SERVER_PORT port that the parent is
listening on
REQUEST_METHOD GET or POST
REMOTE_HOST domain name of client
REMOTE_ADDR dotted-decimal IP client
CONTENT_TYPE POST only: MIME type of
the request body
CONTENT_LENGTH POST only: Size in bytes
of the request body
--------------------------------------------------

4. 子进程输出发送

一个CGI进程将它的动态内容发送到标准输出。在子进程
加载运行CGI程序之前,使用Linux dup2函数将输出重定向
到客户端相关联的已连接描述符。

子进程负责生成Contect-type和content-length响应报头,
以及终止报头的空行。


11.6 Putting it together: the Tiny web server



--------------------------------------------------------

posted @ 2018-03-28 23:34  孤灯下的守护者  阅读(131)  评论(0编辑  收藏  举报