第十一章 網絡編程

--------------------------------------------------------
Sun 11 Feb 13:30:10 GMT 2018
--------------------------------------------------------
第十一章 網絡編程web

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 網絡app

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

+ Naming scheme.

Internet address which is a uniform format address.socket

+ Delivery mechanismide

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 Internetoop

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

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

相關文章
相關標籤/搜索