没有合适的资源?快使用搜索试试~ 我知道了~
首页Boost.Asio实战:构建高效跨平台分布式应用
Boost.Asio实战:构建高效跨平台分布式应用
![](https://csdnimg.cn/release/wenkucmsfe/public/img/star.98a08eaa.png)
《Boost.Asio C++网络编程实战指南》是一本由Dmytro Radchuk编写的实用教程,着重于利用备受推崇的Boost.Asio库进行C++网络编程。该书提供了超过25个实践案例,旨在帮助读者创建强大、跨平台且高效的分布式应用程序。通过一步步的指导,读者将掌握如何执行基本的Boost.Asio操作,包括TCP和UDP协议的基础知识。 第1章概述了Boost.Asio的基本概念,让读者对这个库有深入理解。随后的章节深入探讨了输入/输出操作,展示了如何有效地进行数据传输。在第3章和第4章,读者将学习如何设计和实现客户端和服务器应用,从简单的同步模式扩展到多线程和可扩展的解决方案。书中特别强调了在分布式应用开发中的理论背景,这对于构建高效系统至关重要。 第5章涉及HTTP和SSL/TLS技术,讲解如何提升分布式应用的安全性,通过添加SSL支持来保护通信。此外,读者还将学会轻松实现一个HTTP客户端,以及利用iostreams、scatter-gather缓冲和定时器等高级功能。这不仅限于基础操作,而是引导读者步入更深层次的技术实践。 该书不仅包含丰富的实例,还提供了理论知识的扩展内容,涵盖了分布式应用的设计与构建原理。无论你是初学者还是经验丰富的开发者,都能从中受益匪浅,通过这本书的指导,可以显著增强对Boost.Asio库的熟练程度,从而提高开发效率和应用质量。 《Boost.Asio C++网络编程实战指南》是一本实用的参考书,适合网络编程爱好者和专业人员使用,无论是为了学习新技术,还是寻求解决实际项目中的问题,都是一份宝贵的资源。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/9434918/bg10.jpg)
1
1
The Basics
In this chapter, we will cover:
f Creating an endpoint
f Creating an active socket
f Creating a passive socket
f Resolving a DNS name
f Binding a socket to an endpoint
f Connecting a socket
f Accepting connections
Introduction
Computer networks and communication protocols signicantly increase capabilities of
modern software, allowing different applications or separate parts of the same application
to communicate with each other to achieve a common goal. Some applications have
communication as their main function, for example, instant messengers, e-mail servers
and clients, le download software, and so on. Others have the network communication
layer as a fundamental component, on top of which the main functionality is built. Some
of the examples of such applications are web browsers, network le systems, distributed
database management systems, media streaming software, online games, ofine games
with multiplayer over the network option support, and many others. Besides, nowadays
almost any application in addition to its main functionality provides supplementary functions,
involving network communication. The most prominent examples of such functions are
online registration and automatic software update. In the latter case, the update package
is downloaded from the application developer's remote server and installed on the user's
computer or mobile device.
![](https://csdnimg.cn/release/download_crawler_static/9434918/bg11.jpg)
The Basics
2
The application that consists of two or more parts, each of which runs on a separate
computing device, and communicates with other parts over a computer network is called
a distributed application. For example, a web server and a web browser together can be
considered as one complex distributed application. The browser running on a user's computer
communicates with the web server running on a different remote computer in order to achieve
a common goal—to transmit and display a web page requested by the user.
Distributed applications provide signicant benets as compared to traditional applications
running on a single computer. The most valuable of them are the following:
f Ability to transmit data between two or more remote computing devices. This is
absolutely obvious and the most valuable benet of distributed software.
f Ability to connect computers in a network and install special software on them,
creating powerful computing systems that can perform tasks that can't otherwise
be performed on a single computer in an adequate amount of time.
f Ability to effectively store and share data in a network. In a computer network, a
single device can be used as data storage to store big amounts of data and other
devices can easily request some portions of that data when necessary without the
need to keep the copy of all data on each device. As an example, consider large
datacenters hosting hundreds of millions of websites. The end user can request the
web page they need anytime by sending the request to the server over the network
(usually, the Internet). There is no need to keep the copy of the website on the user's
device. There is a single storage of the data (a website) and millions of users can
request the data from that storage if and when this information is needed.
For two applications running on different computing devices to communicate with each other,
they need to agree on a communication protocol. Of course, the developer of the distributed
application is free to implement his or her own protocol. However, this would be rarely the
case at least for two reasons. First, developing such a protocol is an enormously complex and
time-consuming task. Secondly, such protocols are already dened, standardized, and even
implemented in all popular operating systems including Windows, Mac OS X, and majority of
the distributions of Linux.
These protocols are dened by the TCP/IP standard. Don't be fooled by the standard's
name; it denes not only TCP and IP but many more other protocols, comprising a TCP/IP
protocol stack with one or more protocols on each level of the stack. Distributed software
developers usually deal with transport level protocols such as TCP or UDP. Lower layer
protocols are usually hidden from the developer and are handled by the operating system
and network devices.
![](https://csdnimg.cn/release/download_crawler_static/9434918/bg12.jpg)
Chapter 1
3
In this book, we only touch upon TCP and UDP protocols that satisfy the needs of most
developers of distributed software. If the reader is not familiar with the TCP/IP protocol stack,
the OSI model, or TCP and UDP protocols, it's highly advised to read some theory on these
topics. Though this book provides some brief information about them, it is mostly focused on
practical aspects of using TCP and UDP protocols in distributed software development.
The TCP protocol is a transport layer protocol with the following characteristics:
f It's reliable, which means that this protocol guarantees delivery of the messages in
proper order or a notication that the message has not been delivered. The protocol
includes error handling mechanisms, which frees the developer from the need to
implement them in the application.
f It assumes logical connection establishment. Before one application can
communicate with another over the TCP protocol, it must establish a logical
connection by exchanging service messages according to the standard.
f It assumes the point-to-point communication model. That is, only two applications
can communicate over a single connection. No multicast messaging is supported.
f It is stream-oriented. This means that the data being sent by one application to
another is interpreted by the protocol as a stream of bytes. In practice, it means that
if a sender application sends a particular block of data, there is no guarantee that it
will be delivered to the receiver application as the same block of data in a single turn,
that is, the sent message may be broken into as many parts as the protocol wants
and each of them will be delivered separately, though in correct order.
The UDP protocol is a transport layer protocol having different (in some sense opposite)
characteristics from those of the TCP protocol. The following are its characteristics:
f It's unreliable, which means that if a sender sends a message over a UDP protocol,
there is no guarantee that the message will be delivered. The protocol won't try to
detect or x any errors. The developer is responsible for all error handling.
f It's connectionless, meaning that no connection establishment is needed before the
applications can communicate.
f It supports both one-to-one and one-to-many communication models. Multicast
messages are supported by the protocol.
f It's datagram oriented. This means that the protocol interprets data as messages of a
particular size and will try to deliver them as a whole. The message (datagram) either
will be delivered as a whole, or if the protocol fails to do that won't be delivered at all.
Because the UDP protocol is unreliable, it is usually used in reliable local networks. To use
it for communication over the Internet (which is an unreliable network), the developer must
implement error handling mechanisms in its application.
![](https://csdnimg.cn/release/download_crawler_static/9434918/bg13.jpg)
The Basics
4
When there is a need to communicate over the Internet, the TCP
protocol is most often the best choice due to its reliability.
As it has already been mentioned, both TCP and UDP protocols and the underlying
protocols required by them are implemented by most popular operating systems. A
developer of a distributed application is provided an API through which it can use protocols
implementation. The TCP/IP standard does not standardize the protocol API implementation;
therefore, several API implementations exist. However, the one based on Berkeley Sockets
API is the most widely used.
Berkeley Sockets API is the name of one of the many possible implementations of TCP and
UDP protocols' API. This API was developed at the Berkeley University of California, USA
(hence the name) in the early 1980s. It is built around a concept of an abstract object called
a socket. Such a name was given to this object in order to draw the analogy with a usual
electrical socket. However, this idea seems to have somewhat failed due to the fact that
Berkeley Sockets turned out to be a signicantly more complex concept.
Now Windows, Mac OS X, and Linux operating systems all have this API implemented
(though with some minor variations) and software developers can use it to consume
TCP and UDP protocols' functionality when developing distributed applications.
Though very popular and widely used, Sockets API has several aws. First, because it was
designed as a very generic API that should support many different protocols, it is quite
complex and somewhat difcult to use. The second aw is that this is a C-style functional
API with a poor type system, which makes it error prone and even more difcult to use. For
example, Sockets API doesn't provide a separate type representing a socket. Instead, the
built-in type int is used, which means that by mistake any value of the int type can be
passed as an argument to the function expecting a socket, and the compiler won't detect
the mistake. This may lead to run-time crashes, the root cause of which is hard to nd.
![](https://csdnimg.cn/release/download_crawler_static/9434918/bg14.jpg)
Chapter 1
5
Network programming is inherently complex and doing it with a low-level C-style socket API
makes it even more complex and error prone. Boost.Asio is an O-O C++ library that is, just like
raw Sockets API, built around the concept of a socket. Roughly speaking, Boost.Asio wraps
raw Sockets API and provides the developer with O-O interface to it. It is intended to simplify
network programming in several ways as follows:
f It hides the raw C-style API and providing a user with an object-oriented API
f It provides a rich-type system, which makes code more readable and allows it to
catch many errors at compilation time
f As Boost.Asio is a cross-platform library, it simplies development of cross-platform
distributed applications
f It provides auxiliary functionality such as scatter-gather I/O operations, stream-based
I/O, exception-based error handling, and others
f The library is designed so that it can be relatively easily extended to add new
custom functionality
This chapter introduces essential Boost.Asio classes and demonstrates how to perform basic
operations with them.
Creating an endpoint
A typical client application, before it can communicate with a server application to consume
its services, must obtain the IP address of the host on which the server application is running
and the protocol port number associated with it. A pair of values consisting of an IP address
and a protocol port number that uniquely identies a particular application running on a
particular host in a computer network is called an endpoint.
The client application will usually obtain the IP address and the port number identifying the
server application either from the user directly through the application UI or as command-line
arguments or will read it from the application's conguration le.
The IP address can be represented as a string containing an address in dot-decimal notation
if it is an IPv4 address (for example, 192.168.10.112) or in hexadecimal notation if it is
an IPv6 address (for example, FE36::0404:C3FA:EF1E:3829). Besides, the server IP
address can be provided to the client application in an indirect form, as a string containing a
DNS name (for example, localhost or www.google.com). Another way to represent an IP
address is an integer value. The IPv4 address is represented as a 32-bit integer and IPv6 as a
64-bit integer. However, due to poor readability and memorability this representation is used
extremely rarely.
剩余247页未读,继续阅读
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/e66324aa22894c0997bffffafc974f36_ramissue.jpg!1)
ramissue
- 粉丝: 354
- 资源: 1487
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 解决本地连接丢失无法上网的问题
- BIOS报警声音解析:故障原因与解决方法
- 广义均值移动跟踪算法在视频目标跟踪中的应用研究
- C++Builder快捷键大全:高效编程的秘密武器
- 网页制作入门:常用代码详解
- TX2440A开发板网络远程监控系统移植教程:易搭建与通用解决方案
- WebLogic10虚拟内存配置详解与优化技巧
- C#网络编程深度解析:Socket基础与应用
- 掌握Struts1:Java MVC轻量级框架详解
- 20个必备CSS代码段提升Web开发效率
- CSS样式大全:字体、文本、列表样式详解
- Proteus元件库大全:从基础到高级组件
- 74HC08芯片:高速CMOS四输入与门详细资料
- C#获取当前路径的多种方法详解
- 修复MySQL乱码问题:设置字符集为GB2312
- C语言的诞生与演进:从汇编到系统编程的革命
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)