第十一章网络编程

孤灯下的守护者 孤灯下的守护者     2022-10-29     576

关键词:

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



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

第十一章网络文件共享服务之samba

Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。在此之前我们已经了解了NFS和FTP,NFS与samba一样,也是在网络中实现文件共享的一种实现,但不幸的是,其不支持windows平台,而本章要提到的samb... 查看详情

第十一章笔记

                  类和对象学习本章用到的单词class:类object:对象return:返回返还,返还值programming:编程,程序设计null:空,空值initial:最初的,初始,初始值type:类型,种类oriented:面向,定向 对象现实世界中客观... 查看详情

第十一章:类和对象

第十一章:类和对象对象概念:现实中客观存在的事物就称为对象,用来描述客观事物的一个实体,由一组属性和方法构成。对象特征静态特征静态特征是可以用某些数据来描述的特征。动态特征动态特征是对象所表现的行为或... 查看详情

第十一章

因为是手机上传,有些图没有上传好,晚上回去重新弄。 查看详情

《java编程思想》学习笔记——第十一章持有对象

    JAVA容器类类库的用途是"保存对象",并将其划分为两个不同的概念:    1)Collection。一个独立元素的序列,这些元素都服从一条或多条规则。List必须按照插入的顺序保存元素,而Set不能用重复元... 查看详情

cprimerplus(第六版)第十一章编程练习答案(代码片段)

前言:这次感觉融起来了,各章的知识都有用到,不过这次时间隔的是够久的。仅供参考,新手勿喷。CH11 Codeanswer1:#include<stdio.h>#defineSIZE100voids_gots(char*);intmain(void) charstr[SIZE]; printf("Enteryourstr 查看详情

网络操作系统第十十一章习题

  第十章习题1.什么是域名系统?描述域名解析的过程。  答:域名系统用于实现IP地址和主机名之间的映射。域名解析过程(1)DNS客户机提出域名解析请求,并将该请求发给本地的域名服务器(2)当本地的域名服务器接收... 查看详情

第十一章

一.分组查询   1.语法    SELECT...FROM...GROUPBY...    --分组查询Groupby    selectCOUNT(*)as学生人数,GradeIdas年级编号fromStudentgroupbyGradeId&n 查看详情

第十一章lamp架构(代码片段)

11.1LAMP架构介绍Q:httpd和php为什么需要组合在一起,不能分开部署在不同的节点使用网络来进行数据协同吗?A:因为我们使用的方式是,php作为httpd的一个模块存在的。?他们两者必须要在一起,才能实现效果。当然,如果你把php做... 查看详情

高数(a)下第十一章(持续更新)

11.1 11.2 查看详情

第十一章linq

DataLibusingSystem;usingSystem.Collections.Generic;namespaceWrox.ProCSharp.LINQ{[Serializable]publicclassTeam{publicTeam(stringname,paramsint[]years){this.Name=name;this.Years=newList<int>(years 查看详情

html第十一章总结

#第十一章总结:本章的标题为:layoutandpositioningArrangingElement##前言:这一章节,通过已经知道的boxmodel的概念,进行讲述关于layout的知识,并且通过这些知识进行创造专业的设计和multicolumnlayouts.##谈谈Flow###对于blockelement*对blockeleme... 查看详情

第十一章多元线性回归与相关分析

  查看详情

2017-2018-120155302第十四周作业

2017-2018-120155302第十四周作业重新精学第十一章网络编程相关知识第十一章网络编程因为之前在刘念老师的课上有所涉及有所讲解所以娄老师并没有着重讲这块知识,但我个人认为此章知识非常重要,是我们学习WEB编程和信息安... 查看详情

第十一章线程

第十一章线程11.3线程标识每个线程也有一个线程ID,线程ID只有在它所属的进程上下文中才有意义。/* 比较两个线程ID,相等返回非0数值,否则返回0*/#include<pthread.h>intpthread_equal(pthread_ttid1,pthread_ttid2); /* 获得自身... 查看详情

第十一章

(1)1.万物皆对象:(地球上有什么?我们会不自觉的将地球上的事物归为具体类别) 2.对象:顾客   ,   收银员;               官方定义:用来... 查看详情

第十一单元管理网络

第十一章 管理网络 一.ip基础知识1ipv432位的二进制组成,为了方便观看,写成10进制ip地址可以取0到255之间的数字例:172.25.254.78/255.255.255.0172.25.254.78:ip地址前三位为网络位(标示区域)最后一位为主机位(区域内的人)... 查看详情

第十一章笔记

                                 &n 查看详情