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 学习草稿的更多相关文章

  1. boost asio 学习(一)io_service的基础

    原文  http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio/ 编译环境 b ...

  2. boost asio 学习(九) boost::asio 网络封装

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...

  3. boost asio 学习(八) 网络基础 二进制写发送和接收

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...

  4. boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...

  5. boost asio 学习(六) 定时器

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=7 6 定时器 ...

  6. BOOST ASIO 学习专贴

    本文已于20170903更新完毕,所有boost asio 代码均为本人手抄.编译器为vs2013,并且所有代码已经上传,本文下方可下载源码 为了学习boost asio库,我是从boost的官方bo ...

  7. boost::asio 学习

    安装 下载-解压 指定安装目录 ./bootstrap.sh --prefix=/usr/local/boost_1_68_0 查看所有必须要编译才能使用的库 ./b2 --show-librarie ...

  8. boost asio 学习(五) 错误处理

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=6 5. Erro ...

  9. boost asio 学习(四)使用strand将任务排序

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=5 4. Seri ...

随机推荐

  1. django-插件django REST framework,返回序列化的数据

    官网: http://www.django-rest-framework.org 1.安装 pip install djangorestframework 2.在setting.py中注册app 中添 ...

  2. 37. CentOS-6.3安装配置Weblogic-10

    安装说明 安装环境:CentOS-6.3-x64软件:server1001_ccjk_linux32.bin安装方式:bin文件安装 安装位置:/usr/local/weblogic/下载地址:htt ...

  3. 17.在Action获取Scope对象

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 引言:在前面的Action操作中,关键就是Action中的exectue方法 ...

  4. fb 发布桌面应用图标

    1.以src文件夹为根目录,即图标放在src文件内 2.修改app.xml文件icon,按尺寸填入,如下图 同时可修改app应用的名字,接受中文,如下图红色涂鸦: 导出发行版的时候,注意打包内容有么有 ...

  5. CStatic控件SS_NOTIFY属性

    SS_NOTIFY Sends the parent window STN_CLICKED, STN_DBLCLK, STN_DISABLE, and STN_ENABLE notification ...

  6. javascript中 try catch用法

    javascript中 try catch用法 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-08-16我要评论 JS try catch语句一般在什么情况下使用?是必须使 ...

  7. 将Ctrl+Alt+Delete键进行屏蔽,防止误操作重启服务器

    [root@bgw-t ~]# vi  /etc/init/control-alt-delete.conf #exec /sbin/shutdown -r now "Control-Alt- ...

  8. (4)shiro多个realm

    shiro支持多个realm,当设置多个realm的时候,shiro的认证和授权的步骤是怎样的呢. 多个realm认证原理: 发现需要在执行认证的时候,需要策略来处理多个realm存在的情况.默认实现 ...

  9. maven GroupId 和ArtifactId通常填什么

    GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构.ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.

  10. ArcGIS案例学习1_2

    ArcGIS案例学习1_2 联系方式:谢老师,135_4855_4328, xiexiaokui#qq.com 时间:第一天下午 案例1:矢量提取,栅格提取和坐标系投影变换 目的:认识数据类型 教程: ...