学习boost::asio一些小例子

# if 0
#include <boost/asio.hpp> 
#include <boost/thread.hpp> 
#include <iostream> 

void handler1(const boost::system::error_code &ec) 
{ 
    std::cout << "5 s." << std::endl; 
} 

void handler2(const boost::system::error_code &ec) 
{ 
    std::cout << "5 s." << std::endl; 
} 

boost::asio::io_service io_service; 

void run() 
{ 
    io_service.run(); 
} 

/*
两个线程调用run运行同一个服务,当第一服务在阻塞时,可以再第二个线程里面执行
但这样会使得使用std::cout混乱,开线程要注意,开过多的线程是没有意义的
*/

int main() 
{ 
    boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(5)); 
    timer1.async_wait(handler1); 
    boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(5)); 
    timer2.async_wait(handler2); 
    boost::thread thread1(run); 
    boost::thread thread2(run); 
    thread1.join(); 
    thread2.join(); 
} 
#endif

#if 0

#include <boost/asio.hpp> 
#include <boost/thread.hpp> 
#include <iostream> 

void handler1(const boost::system::error_code &ec) 
{ 
    std::cout << "5 s." << std::endl; 
} 

void handler2(const boost::system::error_code &ec) 
{ 
    std::cout << "5 s." << std::endl; 
} 

boost::asio::io_service io_service1; 
boost::asio::io_service io_service2; 

void run1() 
{ 
    io_service1.run(); 
} 

void run2() 
{ 
    io_service2.run(); 
} 

/*
第二种方法不是多个线程运行同一个服务,而是创建多个服务,每个服务用同一个线程,如果线程数与内核数相同,在一个核上面不回出现竞争现象
*/
int main() 
{ 
    boost::asio::deadline_timer timer1(io_service1, boost::posix_time::seconds(5)); 
    timer1.async_wait(handler1); 
    boost::asio::deadline_timer timer2(io_service2, boost::posix_time::seconds(5)); 
    timer2.async_wait(handler2); 
    boost::thread thread1(run1); 
    boost::thread thread2(run2); 
    thread1.join(); 
    thread2.join(); 
} 

#endif

//简单客户端请求
#if 0

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <string>

boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::socket sock(io_service);
boost::array<char, 4096> buffer;


/**
 *@brief 读回调,当读完时read_handler内部产生错误,不回循环调用了
 */
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
    if (!ec)
    {
        std::cout << std::string(buffer.data(), bytes_transferred) << std::endl;
        //必须的,因为可能包没接完,和read_handler交替运行
        sock.async_read_some(boost::asio::buffer(buffer), read_handler);
    }
}

void connect_handler(const boost::system::error_code& ec)
{
    if (!ec)
    {
        //发送http请求,
        boost::asio::write(sock, boost::asio::buffer("GET / HTTP 1.1\r\nHost: highscore.de\r\n\r\n"));
        //启动数据的接收,接收数据保存在buffer中,回调read_handler
        sock.async_read_some(boost::asio::buffer(buffer), read_handler);
    }
}

/**
 *@brief 域名解析回调
 *@param it 解析后地址
 */
void resolver_handler(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator it)
{
    if (!ec)
    {
        //访问IO对象sock,建立连接
        sock.async_connect(*it, connect_handler);
    }
}

int main()
{
    boost::asio::ip::tcp::resolver::query query("www.highscore.de", "80");//新建一个查询,域名为...,端口为80
    resolver.async_resolve(query, resolver_handler);//解析域名为ip
    io_service.run();//将控制权交给操作系统进行异步操作
    getchar();
}
#endif

//简单的服务器接收例子
#if 0
#include <boost/asio.hpp>
#include <string>

boost::asio::io_service io_service;
//接收端口配置,从80端口等待v4类型的ip
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 80);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
boost::asio::ip::tcp::socket sock(io_service);
std::string data = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!"; 

void write_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{

}

void accept_handler(const boost::system::error_code& ec)
{
    if (!ec)
    {
        //async_write_some函数也能发送数据,但这个函数只要发送一个字节就会回调函数,
        //因为将整个数据发送完毕异步操作才算完毕,async_write函数在发送完毕调用write_handler
        //这个和libevent机制差不多
        boost::asio::async_write(sock, boost::asio::buffer(data), write_handler);
    }
}

int main()
{
    acceptor.listen();//监听,将接收器配置成接收状态
    acceptor.async_accept(sock, accept_handler);//等待接收
    io_service.run();
}

#endif

 

posted @ 2015-03-18 16:11  zzyoucan  阅读(753)  评论(0编辑  收藏  举报