没有合适的资源?快使用搜索试试~ 我知道了~
首页实战提升:C++ Boost.Asio网络编程教程
实战提升:C++ Boost.Asio网络编程教程
需积分: 10 0 下载量 78 浏览量
更新于2024-07-17
收藏 7.63MB PDF 举报
"《Boost.Asio C++网络编程》是一本由John Torjo编著的专业书籍,专为提升C++网络编程技能提供实践案例。该书是针对Boost.Asio库的深入指南,Boost.Asio是一款强大的C++库,用于编写高效的异步网络应用程序,支持多种协议(如TCP、UDP、HTTP等)和并发处理。本书旨在帮助读者理解并掌握Boost.Asio的核心概念,包括事件驱动编程模型、异步IO操作、连接管理以及错误处理等关键知识点。 通过阅读这本书,你将学习如何在C++环境中利用Boost.Asio进行高效网络编程,例如如何创建服务器和客户端,实现数据传输,以及如何处理多路复用和并发连接。书中提供的实例涵盖了一系列实际应用场景,例如网络服务端的实时通信、WebSocket服务器开发、网络监控工具的构建等,这些都是现代软件开发中不可或缺的技术。 尽管作者和Packt Publishing已尽力确保信息的准确性,但书中内容并非保证无误,且所有版权需按照Packt Publishing的规定执行,未经许可不得复制或传播。此外,由于技术发展和更新,部分商标信息可能存在误差,读者应自行核实。 如果你是一名C++开发者,想要深入理解和应用Boost.Asio库进行网络编程,或者对高性能网络程序设计有兴趣,那么《Boost.Asio C++网络编程》是一本值得投资的参考资料。通过实践和理解书中的内容,你将在C++网络编程领域迈出坚实的一步。"
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/10947311/bg10.jpg)
Getting Started with
Boost.Asio
First, lets delve into what Boost.Asio is, how to build it, and a few examples
along the way. Boost.Asio is more than a networking library as you're about to
nd out. You'll also discover the most important class that sits at the heart of
Boost.Asio – io_service.
What is Boost.Asio?
In short, Boost.Asio is a cross-platform C++ library mainly for networking and
some other low-level input/output programming.
There have been many implementations that have tackled networking, but Boost.
Asio has by far surpassed them all; it was admitted into Boost in 2005, and has since
been tested extensively by Boost users and used in many projects, such as Remobo
(http://www.remobo.com) that allows you to create your own Instant Private
Network (IPN), libtorrent (http://www.rasterbar.com/products/libtorrent),
which is a library that implements a Bittorrent client, and PokerTH (http://www.
pokerth.net), which is a poker game that supports LAN and Internet games.
Boost.Asio has successfully abstracted the concepts of input and output that work
not just for networking but for COM serial ports, les, and so on. On top of these,
you can do input or output programming synchronously or asynchronously:
read(stream, buffer [, extra options])
async_read(stream, buffer [, extra options], handler)
write(stream, buffer [, extra options])
async_write(stream, buffer [, extra options], handler)
![](https://csdnimg.cn/release/download_crawler_static/10947311/bg11.jpg)
Getting Started with Boost.Asio
[ 6 ]
Downloading the example code
You can download the example code les for all Packt books you
have purchased from your account at http://www.packtpub.com.
If you purchased this book elsewhere, you can visit http://www.
packtpub.com/support and register to have the les e-mailed
directly to you.
As you can see in the preceding code snippet, the functions take a stream instance,
which can be anything (not just a socket, we can read or write to it).
The library is portable and works across most operating systems, and scales well
over thousands of concurrent connections. The networking part was inspired by
Berkeley Software Distribution (BSD) sockets. It provides an API that deals with
Transmission Control Protocol (TCP) sockets, User Datagram Protocol (UDP)
sockets, Internet Control Message Protocol (IMCP) sockets, and is extensible as
you can adapt it to your own protocol if you wish.
History
Boost.Asio was accepted into Boost 1.35 in December 2005, after being developed in
2003. The original author is Christopher M. Kohlhoff, and he can be reached at chris@
kohlhoff.com.
The library has been tested on the following platforms and compilers:
• 32-bit and 64-bit Windows, using Visual C++ 7.1 and above
• Windows using MinGW
• Windows using Cygwin (make sure to dene __USE_232_SOCKETS)
• Linux based on 2.4 and 2.6 kernels, using g++ 3.3 and above
• Solaris, using g++ 3.3 and above
• MAC OS X 10.4+, using g++ 3.3 and above
It may also work on the platforms, such as AIX 5.3, HP-UX 11i v3, QNX Neutrino
6.3, Solaris using Sun Studio 11+, True64 v5.1, Windows using Borland C++ 5.9.2+
(consult at www.boost.org for more details).
![](https://csdnimg.cn/release/download_crawler_static/10947311/bg12.jpg)
Chapter 1
[ 7 ]
Dependencies
Boost.Asio depends on the following libraries:
• Boost.System: This library provides operating system support for Boost
libraries (http://www.boost.org/doc/libs/1_51_0/doc/html/boost_
system/index.html)
• Boost.Regex: This library (optional) is used in case you're using the
read_until() or async_read_until() overloads that take a
boost::regex parameter
• Boost.DateTime: This library(optional) is used if you use Boost.Asio timers
• OpenSSL: This library (optional) is used if you decide to use the SSL support
provided by Boost.Asio
Building Boost.Asio
Boost.Asio is a header-only library. However, depending on your compiler and
the size of your program, you can choose to build in Boost.Asio as a source le.
You may want to do this to decrease the compilation times. This can be done in
the following ways:
• In only one of your les, by using #include <boost/asio/impl/src.hpp>
(if you're using SSL, #include <boost/asio/ssl/impl/src.hpp> as well)
• By using #define BOOST_ASIO_SEPARATE_COMPILATION in all your
source les
Do note that Boost.Asio depends on Boost.System and optionally Boost.Regex,
so you'll need to at least build boost libraries, using the following code:
bjam –with-system –with-regex stage
If you want to build the tests as well, you should use the following code:
bjam –with-system –with-thread –with-date_time –with-regex –with-
serialization stage
The library comes with lots of examples, which you can check out, along with the
examples that come with this book.
![](https://csdnimg.cn/release/download_crawler_static/10947311/bg13.jpg)
Getting Started with Boost.Asio
[ 8 ]
Important macros
Use BOOST_ASIO_DISABLE_THREADS if set; it disables threading support in Boost.
Asio, regardless of whether Boost was compiled with threading support.
Synchronous versus asynchronous
First off, asynchronous programming is extremely different than synchronous
programming. In synchronous programming, you do the operations in sequential
order, such as read (request) from socket S, then write (answer) to socket. Each
operation is blocking. Since operations are blocking, in order not to interrupt the
main program while you're reading from or writing to a socket, you'll usually
create one or more threads that deal with socket's input/output. Thus, synchronous
servers/clients are usually multi-threaded.
In contrast, asynchronous programming is event-driven. You start an operation,
but you don't know when it will end; you supply a callback, which the API will
call when the operation ends, together with the operation result. To programmers
that have extensive experience with QT, Nokia's cross-platform library for creating
graphical user interface applications, this is second nature. Thus, in asynchronous
programming, you don't necessary need more than one thread.
You should decide early on in your project (preferably at the start) whether you go
synchronous or asynchronous with networking, as switching midway will be very
difcult and error-prone; not only will the API differ substantially, the semantic of
your program will change completely (asynchronous networking is usually harder
to test and debug than synchronous networking). You'll want to think of either going
for blocking calls and multi-threading (synchronous, usually simpler) or less-threads
and events (asynchronous, usually more complex).
Here's a basic example of a synchronous client:
using boost::asio;
io_service service;
ip::tcp::endpoint ep( ip::address::from_string("127.0.0.1"), 2001);
ip::tcp::socket sock(service);
sock.connect(ep);
First, your program needs at least an io_service instance. Boost.Asio uses
io_service to talk to the operating system's input/output services. Usually one
instance of an io_service will be enough. Next, create the address and port you
want to connect to. Create the socket. Connect the socket to your address and port:
![](https://csdnimg.cn/release/download_crawler_static/10947311/bg14.jpg)
Chapter 1
[ 9 ]
Here is a simple synchronous server:using boost::asio;
typedef boost::shared_ptr<ip::tcp::socket> socket_ptr;
io_service service;
ip::tcp::endpoint ep( ip::tcp::v4(), 2001)); // listen on 2001
ip::tcp::acceptor acc(service, ep);
while ( true) {
socket_ptr sock(new ip::tcp::socket(service));
acc.accept(*sock);
boost::thread( boost::bind(client_session, sock));
}
void client_session(socket_ptr sock) {
while ( true) {
char data[512];
size_t len = sock->read_some(buffer(data));
if ( len > 0)
write(*sock, buffer("ok", 2));
}
}
Again, rst your program needs at least one io_service instance. You then
specify the port you're listening to, and create an acceptor, one object that accepts
client connections.
In the following loop, you create a dummy socket and wait for a client to connect.
Once a connection has been established, you create a thread that will deal with
that connection.
In the client_session thread, read a client's request, interpret it, and answer back.
To create a basic asynchronous client, you'll do something similar to the following:
using boost::asio;
io_service service;
ip::tcp::endpoint ep( ip::address::from_string("127.0.0.1"), 2001);
ip::tcp::socket sock(service);
sock.async_connect(ep, connect_handler);
service.run();
void connect_handler(const boost::system::error_code & ec) {
// here we know we connected successfully
// if ec indicates success
}
www.allitebooks.com
剩余155页未读,继续阅读
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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://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/531f5d3e545a41b690c5ec7a74bdc96a_weixin_42199884.jpg!1)
StardustEncoder
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 岩石滑动与断层冲击地压:声发射特征分析
- 新时代煤炭工业八大战略新取向剖析
- 开采强度对华亭矿区冲击地压危险性的影响分析
- 新安煤矿探采对比:影响安全生产的关键因素与技术支撑
- 210MW循环流化床锅炉引风机变频改造优化与节能效果
- 浅埋煤层开采沉陷预测:方法应用与影响分析
- 优化巷道过陷落柱支护方案:数值模拟与现场试验
- 坚硬顶板定向断裂爆破技术:安全高效采煤新方案
- 北航2019算法课作业解析:投资策略与凸子多边形优化
- 断层破碎带垮冒下堆积体边界抗力分布关键影响因素
- 哈拉沟选煤厂优化末煤系统提高效率与处理能力
- 瓦斯涌出影响下采动煤体蠕变失稳研究
- 提升煤矿装载效率的可控箕斗定量装载设备设计
- 水分调控下烟煤瓦斯吸附特性对比研究
- 重介质悬浮液密度控制硬件设计与选型探讨
- 坚硬顶板巷道支护参数设计:现场测试与数值分析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](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)