当前位置:网站首页>C/S模型与P2P模型
C/S模型与P2P模型
2022-06-13 09:01:00 【牧鸯人】
C/S 模型
TCP/IP协议在设计和实现上并没有客户端和服务器的概念,在通信过程中所有机器都是对等的。但由于资源(视频、新闻、软件等)都被数据提供者所垄断,所以几乎所有的网络应用程序都很自然地采用了C/S模型图所示的C/S (客户端/服务器)模型:所有客户端都通过访问服务器来获取所需的资源。
采用C/S模型的TCP服务器和TCP客户端的工作流程如图所示。
C/S模型的逻辑很简单。服务器启动后,首先创建一个(或多个)监听socket, 并调用bind函数将其绑定到服务器感兴趣的端口上,然后调用listen函数等待客户连接。服务器稳定运行之后,客户端就可以调用connect函数向服务器发起连接了。由于客户连接请求是随机到达的异步事件,服务器需要使用某种I/O模型来监听这一事件。I/O模型有多种,工作流程图中,服务器使用的是I/O复用技术之一的select系统调用。当监听到连接请求后,服务器就调用accept函数接受它,并分配一个逻辑单元为新的连接服务。逻辑单元可以是新创建的子进程、子线程或者其他。工作流程图中,服务器给客户端分配的逻辑单元是由fork系统调用创建的子进程。逻辑单元读取客户请求,处理该请求,然后将处理结果返回给客户端。客户端接收到服务器反馈的结果之后,可以继续向服务器发送请求,也可以立即主动关闭连接。如果客户端主动关闭连接,则服务器执行被动关闭连接。至此,双方的通信结束。需要注意的是,服务器在处理一个客户请求的同时还会继续监听其他客户请求,否则就变成了效率低下的串行服务器了(必须先处理完前一个客户的请求,才能继续处理下一个客户请求)。工作流程图中,服务器同时监听多个客户请求是通过select系统调用实现的。
C/S模型非常适合资源相对集中的场合,并且它的实现也很简单,但其缺点也很明显:服务器是通信的中心,当访问量过大时,可能所有客户都将得到很慢的响应。
P2P 模型
P2P (Peer to Peer,点对点)模型比C/S模型更符合网络通信的实际情况。它摒弃了以服务器为中心的格局,让网络上所有主机重新回归对等的地位。P2P 模型如图两种P2P模型(1)图所示。
P2P模型使得每台机器在消耗服务的同时也给别人提供服务,这样资源能够充分、自由地共享。云计算机群可以看作P2P模型的一一个典范。但P2P模型的缺点也很明显:当用户之间传输的请求过多时,网络的负载将加重。
两种P2P模型(1)图所示的P2P模型存在一个显著的问题,即主机之间很难互相发现。所以实际使用的P2P模型通常带有一个专门的发现服务器,如图8-3b所示。这个发现服务器通常还提供查找服务(甚至还可以提供内容服务),使每个客户都能尽快地找到自己需要的资源。
从编程角度来讲,P2P 模型可以看作C/S模型的扩展:每台主机既是客户端,又是服务器。因此,我们仍然采用C/S模型来讨论网络编程。
边栏推荐
- 【 sécurité 】 comment devenir ingénieur de sécurité de 0 à 1 contre - attaque pour la Fondation zéro
- Jfinal and swagger integration
- Agile development practice summary-4
- Drill down to protobuf - Introduction
- Margin:0 reason why auto does not take effect
- 20220606 关于矩阵的Young不等式
- 消息中间件
- How excel adds hyperlinks to some text in a cell
- 简单实现数据库链接池
- 20211028 adjustment and tracking
猜你喜欢
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
System analysis - detailed description
Neo4j - CQL使用
an error occurred while trying to rename a file in the destination directory code 5
Onnx crop intermediate node
Knowledge points related to system architecture 3
How many TCP connections can a machine create at most?
JUC原子引用与ABA问题
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
Redis fuzzy query batch deletion
随机推荐
pytorch相同结构不同参数名模型加载权重
JS obtain geographic location information according to longitude and latitude and mark it on the map
Loss outputs Nan for the Nan model
20211006 integral, differential and projection belong to linear transformation
网络安全漏洞分析之重定向漏洞分析
Library management system based on wechat applet Rar (thesis + source code)
14. class initialization, default constructor, =default
Cesium view switching, locating, reading files, building data sources, entity control, model control, etc
JUC原子数组
共享模型之不可变
Completely uninstall PostgreSQL under Linux
Download address of QT source code of each version
20220606 Young's inequality for Matrices
How many TCP connections can a machine create at most?
How Simulink adds modules to the library browser
20211108 能观能控,可稳可测
Simulink如何添加模块到Library Browser
An error CV2 is reported when the picture is converted to grayscale cvtColor(img, cv2.COLOR_BGR2GRAY)
[network security] webshell empowerment of new thinking of SQL injection
redis 模糊查询 批量删除