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

 

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
// 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;
}

  

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
// 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;
}

  

posted on   itdef  阅读(293)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
阅读排行:
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 个人数据保全计划:从印象笔记迁移到joplin
· Vue3.5常用特性整理
· 重拾 SSH:从基础到安全加固
· 为什么UNIX使用init进程启动其他进程?

导航

< 2025年1月 >
29 30 31 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 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示