boost asio 学习(八) 网络基础 二进制写发送和接收
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-
started-with-boostasio?pg=9
8. Networking basics: binary protocol sending and receiving (TCP)
现在我们了解了boost::asio库和一些简单的tcp网络知识。现在进行一些简单的网路底层封装.通过使用这些分装。我们能重复使用并且将
注意力集中在程序逻辑而不是一再编写网络通讯代码。
重要注意事项:代码完全是出于教育目的。不要在商业系统中使用。
另外 使用这个网络封装代码的开销是需要考虑的。比如说,大量的vector与list的分配。bind与shared_ptr指针的开销。这也是这个代码
仅用来作为教育用途的原因。
有多重类型的函数用来发送和接收。我们基于协议选择不同的类型。
我们的例子中使用async_write and async_read_some.
使用async_write的原因是函数写入所有的数据,而无需担心部分发送的问题。同样的,我们使用async_read_some作为通用函数去读取数
据因为我们没有特定的协议用来接收。
现在我们学习一个完整的使用IO函数的例子。我们扩展7-C的例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | #include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/bind.hpp> #include <boost/lexical_cast.hpp> #include <boost/cstdint.hpp> #include <boost/enable_shared_from_this.hpp> #include <iostream> #include <string> boost::mutex global_stream_lock; void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service ) { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Thread Start" << std::endl; global_stream_lock.unlock(); while ( true ) { try { boost::system::error_code ec; io_service->run( ec ); if ( ec ) { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Error: " << ec << std::endl; global_stream_lock.unlock(); } break ; } catch ( std::exception & ex ) { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Exception: " << ex.what() << std::endl; global_stream_lock.unlock(); } } global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Thread Finish" << std::endl; global_stream_lock.unlock(); } struct ClientContext : public boost::enable_shared_from_this< ClientContext > { boost::asio::ip::tcp::socket m_socket; std::vector< boost::uint8_t > m_recv_buffer; size_t m_recv_buffer_index; std::list< std::vector< boost::uint8_t > > m_send_buffer; ClientContext( boost::asio::io_service & io_service ) : m_socket( io_service ), m_recv_buffer_index( 0 ) { m_recv_buffer.resize( 4096 ); } ~ClientContext() { } void Close() { boost::system::error_code ec; m_socket.shutdown( boost::asio::ip::tcp::socket::shutdown_both, ec ); m_socket.close( ec ); } void OnSend( const boost::system::error_code & ec, std::list< std::vector< boost::uint8_t > >::iterator itr ) { if ( ec ) { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Error: " << ec << std::endl; global_stream_lock.unlock(); Close(); } else { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Sent " << (*itr).size() << " bytes." << std::endl; global_stream_lock.unlock(); } m_send_buffer.erase( itr ); // Start the next pending send if ( !m_send_buffer.empty() ) { boost::asio::async_write( m_socket, boost::asio::buffer( m_send_buffer.front() ), boost::bind( &ClientContext::OnSend, shared_from_this(), boost::asio::placeholders::error, m_send_buffer.begin() ) ); } } void Send( const void * buffer, size_t length ) { bool can_send_now = false ; std::vector< boost::uint8_t > output; std::copy( ( const boost::uint8_t *)buffer, ( const boost::uint8_t *)buffer + length, std::back_inserter( output ) ); // Store if this is the only current send or not can_send_now = m_send_buffer.empty(); // Save the buffer to be sent m_send_buffer.push_back( output ); // Only send if there are no more pending buffers waiting! if ( can_send_now ) { // Start the next pending send boost::asio::async_write( m_socket, boost::asio::buffer( m_send_buffer.front() ), boost::bind( &ClientContext::OnSend, shared_from_this(), boost::asio::placeholders::error, m_send_buffer.begin() ) ); } } void OnRecv( const boost::system::error_code & ec, size_t bytes_transferred ) { if ( ec ) { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Error: " << ec << std::endl; global_stream_lock.unlock(); Close(); } else { // Increase how many bytes we have saved up m_recv_buffer_index += bytes_transferred; // Debug information global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Recv " << bytes_transferred << " bytes." << std::endl; global_stream_lock.unlock(); // Dump all the data global_stream_lock. lock (); for ( size_t x = 0; x < m_recv_buffer_index; ++x ) { std::cout << std::hex << std::setfill( '0' ) << std::setw( 2 ) << ( int )m_recv_buffer[ x ] << " " ; if ( ( x + 1 ) % 16 == 0 ) { std::cout << std::endl; } } std::cout << std::endl << std::dec; global_stream_lock.unlock(); // Discard all the data (virtually, not physically!) m_recv_buffer_index = 0; // Start the next recv cycle Recv(); } } void Recv() { m_socket.async_read_some( boost::asio::buffer( &m_recv_buffer[ m_recv_buffer_index ], m_recv_buffer.size() - m_recv_buffer_index ), boost::bind( &ClientContext::OnRecv, shared_from_this(), _1, _2 ) ); } }; void OnAccept( const boost::system::error_code & ec, boost::shared_ptr< ClientContext > client ) { if ( ec ) { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Error: " << ec << std::endl; global_stream_lock.unlock(); } else { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Accepted!" << std::endl; global_stream_lock.unlock(); // 2 bytes message size, followed by the message client->Send( "\x02\x00Hi" , 6 ); client->Recv(); } } int main( int argc, char * argv[] ) { boost::shared_ptr< boost::asio::io_service > io_service( new boost::asio::io_service ); boost::shared_ptr< boost::asio::io_service::work > work( new boost::asio::io_service::work( *io_service ) ); boost::shared_ptr< boost::asio::io_service::strand > strand( new boost::asio::io_service::strand( *io_service ) ); global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Press [return] to exit." << std::endl; global_stream_lock.unlock(); // 1 worker thread so we do not have to deal with thread safety issues boost::thread_group worker_threads; for ( int x = 0; x < 1; ++x ) { worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) ); } boost::shared_ptr< boost::asio::ip::tcp::acceptor > acceptor( new boost::asio::ip::tcp::acceptor( *io_service ) ); boost::shared_ptr< ClientContext > client( new ClientContext( *io_service ) ); try { boost::asio::ip::tcp::resolver resolver( *io_service ); boost::asio::ip::tcp::resolver::query query( "127.0.0.1" , boost::lexical_cast< std:: string >( 7777 ) ); boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve( query ); acceptor->open( endpoint.protocol() ); acceptor->set_option( boost::asio::ip::tcp::acceptor::reuse_address( false ) ); acceptor->bind( endpoint ); acceptor->listen( boost::asio::socket_base::max_connections ); acceptor->async_accept( client->m_socket, boost::bind( OnAccept, _1, client ) ); global_stream_lock. lock (); std::cout << "Listening on: " << endpoint << std::endl; global_stream_lock.unlock(); } catch ( std::exception & ex ) { global_stream_lock. lock (); std::cout << "[" << boost::this_thread::get_id() << "] Exception: " << ex.what() << std::endl; global_stream_lock.unlock(); } std::cin. get (); boost::system::error_code ec; acceptor->close( ec ); io_service->stop(); worker_threads.join_all(); return 0; } |
这个例子中,我们添加了ClientContext 类。它用来存储所有传入连接的上下文的具体结构。类封装了必须的io接收发送的函数。例子中
,服务器将发送一个具体的信息给传入连接。我们没配置一个具体的客户端去测试,仅仅使用简单的telnet即可。任意数据发送到服务器
将被输出到命令行。
同上一个例子不同的是,每个连接都需要自己的上下文。上下文应包含socket 发送接收缓存以及其他用户数据。另外,例子不是线程安装
的,所以我们限制在一个线程运行。我们将稍后讨论这个问题。现在,对于socket的读写将选择正确的api函数以避免发生问题.
为了正确的使用,我们必须确认上下文和缓存在函数的生存的整个期间都是有效的。这个例子中,我们使用一个vector list来发送,使用一个vector来接收缓存。根据我们实现的协议,我们可能需要做一些小小的改动。比如说,如果我们想处理流中的包。这种情况下,我们将希望使用async_read读取头结构然后使用async_read读取具体尺寸的数据。
在大型程序中,一次只处理一个包的缺点就是效率低下。假设我们有1003字节信息在流中。我们将执行读取包这个逻辑100次。而我们使用async_read_some,效率会更高。
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话