boost::asio 学习草稿
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio/
可以多个线程拥有io_service service; service.run();
但是不能一个线程运行多个service.run();
Boost.Asio c++ 网络编程翻译(1)
http://blog.csdn.net/mmoaay/article/details/39371939
// Server.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <windows.h> using namespace boost::asio; const size_t defaultLoopTime = 10000;
const size_t defaultSendLength = 1024 * 10 * 50;
const short int defaultPort = 8001; /**************************************************************
技术博客
http://www.cnblogs.com/itdef/ 技术交流群
群号码:324164944 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/ int main()
{
io_service service;
ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::tcp::v4(), defaultPort)); boost::shared_ptr<char> msgPtr(new char[defaultSendLength]);
for (size_t i = 0; i < defaultSendLength; i++)
{
msgPtr.get()[i] = "0123456789abcdef"[i % 16];
}
ip::tcp::socket sock(service);
acceptor.accept(sock); try {
for (int i = 0; i < defaultLoopTime; i++)
{
boost::system::error_code ec;
size_t bytes = write(sock, buffer(msgPtr.get(), defaultSendLength), ec);
assert(bytes == defaultSendLength);
if (ec == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (ec)
throw boost::system::system_error(ec); // Some other error.
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
} system("pause");
return 0;
}
// Client.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <iostream> #include <windows.h> /**************************************************************
技术博客
http://www.cnblogs.com/itdef/ 技术交流群
群号码:324164944 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/ using namespace boost::asio;
using namespace std; const size_t defaultLoopTime = 10000;
const size_t defaultSendLength = 1024 * 10 * 50;
const short int defaultPort = 8001; int main()
{
io_service service;
ip::tcp::endpoint ep(ip::address::from_string("192.168.3.79"), defaultPort);
ip::tcp::socket sock(service);
sock.connect(ep);
time_t timer1;
time(&timer1); try {
for (size_t i = 0; i < defaultLoopTime; i++)
{
boost::shared_ptr<char> msgPtr(new char[defaultSendLength]);
boost::system::error_code ec;
size_t bytes = read(sock, buffer(msgPtr.get(), defaultSendLength), ec);
assert(bytes == defaultSendLength);
if (ec == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (ec)
throw boost::system::system_error(ec); // Some other error.
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return -1;
} time_t timer2;
time(&timer2);
double elapsed = difftime(timer2, timer1);
std::cout << "time elapsed: "<< elapsed << "seconds." << std::endl;
double total_mb = 1.0 * defaultSendLength*defaultLoopTime / 1024 / 1024; std::cout << total_mb/ elapsed << "Mb/s." << std::endl; system("pause");
return 0;
}
boost::asio 学习草稿的更多相关文章
- boost asio 学习(一)io_service的基础
原文 http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio/ 编译环境 b ...
- boost asio 学习(九) boost::asio 网络封装
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...
- boost asio 学习(八) 网络基础 二进制写发送和接收
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...
- boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...
- boost asio 学习(六) 定时器
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=7 6 定时器 ...
- BOOST ASIO 学习专贴
本文已于20170903更新完毕,所有boost asio 代码均为本人手抄.编译器为vs2013,并且所有代码已经上传,本文下方可下载源码 为了学习boost asio库,我是从boost的官方bo ...
- boost::asio 学习
安装 下载-解压 指定安装目录 ./bootstrap.sh --prefix=/usr/local/boost_1_68_0 查看所有必须要编译才能使用的库 ./b2 --show-librarie ...
- boost asio 学习(五) 错误处理
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=6 5. Erro ...
- boost asio 学习(四)使用strand将任务排序
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=5 4. Seri ...
随机推荐
- 【转】ECharts3.x中的点击事件与行为
在ECharts中主要通过 on 方法添加事件处理函数,ECharts中的事件主要分为两种,1)鼠标事件,在鼠标click or hove 时触发鼠标事件: 2)另外一种是在ECharts在做图形 ...
- ldap复制
1.使用yum命令安装openldap,openldap-servers,openldap-clients $ yum install openldap $ yum install openldap- ...
- leetcode134
class Solution { public: inline int get_next(int idx, int size) { ? : idx+; } int aux(int idx, vecto ...
- redis详解(四)-- 高可用分布式集群
一,高可用 高可用(High Availability),是当一台服务器停止服务后,对于业务及用户毫无影响. 停止服务的原因可能由于网卡.路由器.机房.CPU负载过高.内存溢出.自然灾害等不可预期的原 ...
- PadLeft 补零
补零 PadLeft -Caption.Length())+Caption; UnicodeString __fastcall StringOfChar(WideChar Ch, int Count) ...
- tensorflow笔记之反向传播时用到的几种方法
1. 梯度下降, tf.train.GradientDescentOptimizer(0.05),梯度下降的问题在与不一定能获得全局最优解,并且因为要在所有数据上最小化损失,所以损失函数是在所有训练数 ...
- Nginx 反向代理、后端检测模块
简介: Nginx 反向代理模块:ngx_http_proxy_module.ngx_http_upstream_module 后端检测模块:nginx_http_upstream_check_mod ...
- sysbench相关
Sysbench工具是集系统测试和数据库测试一体的测试工具,但是传统的sysbench在数据库测试方面,没有遵循TPC-C测试模型,仅仅支持单个表的数据.而在实际的业务场景中,业务逻辑复杂的多.开源的 ...
- China cuts bank reserves by $100m to cushion US tariffs
China cuts bank reserves by $100m to cushion US tariffs中国央行定向降准释放千亿美元资金China is cutting the amount o ...
- bat cmd dos 通过拖拽参数 上传 截取拖拽上传文件名
echo off setlocal enabledelayedexpansion :: L 小写 for /l %%i in (1,1,10000) do ( :con set /p a= selec ...