开源项目(库)之boost::asio学习(二)

时间:2022-09-08 23:26:36

在上篇博文中,我们学习了boost::asio库的同步I/O机制,里面的代码比较的简单,但是也是很重要,在这篇博文中,我们继续学习boost::asio库的异步方式,异步方式与同步方式的最大的不同,想必大家都很清楚,简单地来讲就是一个需要等待,一个不会等待,至于两者之间一些细节的不同,大家就放狗搜吧,好了,下面来看代码,代码如下:

服务器端代码

#ifndef __ASERVER__H
#define __ASERVER__H
#include <iostream>
#include <string>
#include <unistd.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>

using namespace boost::asio;
using boost::system::error_code;
using ip::tcp;

class CService
{
public:
CService(io_service& io_serv):m_ioserv(io_serv),m_acceptor(io_serv,tcp::endpoint(tcp::v4(),9999)){}
~CService(){}
void start()
{
boost::shared_ptr<tcp::socket> pSocket(new tcp::socket(m_ioserv));
m_acceptor.async_accept(*pSocket,boost::bind(&CService::accept_handler,this,pSocket,_1));
}
void accept_handler(boost::shared_ptr<tcp::socket> pSocket,error_code ecode)
{
if(ecode) return;
start();
pid_t forkid;
std::cout<<pSocket->remote_endpoint().address()<<std::endl;
forkid = fork();
if(forkid ==0)
{
for(;;)
{
#define MAX_BUFFERSIZE 1024
char buff[MAX_BUFFERSIZE];
bzero(buff,sizeof(buff));
pSocket->async_read_some(buffer(buff),boost::bind(&CService::read_handler,this,buff,_1,_2));
boost::shared_ptr<std::string> msg(new std::string(buff));
pSocket->async_write_some(buffer(*msg),boost::bind(&CService::write_handler,this,msg,_1,_2));
}
}
}
void write_handler(boost::shared_ptr<std::string> msg,error_code ecode,size_t bytes_transferred)
{
if(ecode)
std::cout<<"send failed"<<std::endl;
else
std::cout<<*msg<<"send success"<<std::endl;
}
void read_handler(char* buffer,error_code ecode,size_t bytes_transferred)
{
std::cout<<buffer<<std::endl;
}
private:
io_service& m_ioserv;
ip::tcp::acceptor m_acceptor;
};
#endif

客户端代码

#ifndef __ACLIENT__H
#define __ACLIENT__H
#include <iostream>
#include <string>
#include <unistd.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
using namespace boost::asio;
using boost::system::error_code;
using ip::tcp;

class AClient
{
public:
AClient(io_service& io_serv):m_ioserv(io_serv),pSocket(new tcp::socket(m_ioserv))
{
}
~AClient(){}
void init()
{
ip::tcp::endpoint epoint(ip::address_v4::from_string("127.0.0.1"),9999);
pSocket->async_connect(epoint,boost::bind(&AClient::handle_connect,this,_1));
}
void handle_connect(error_code ecode)
{
if(ecode)
{
std::cout<<boost::system::system_error(ecode).what()<<std::endl;
pSocket->close();
return;
}
#define MAX_BUFFERSIZE 1024
char buff[MAX_BUFFERSIZE];
bzero(buff,sizeof(buff));
while(1)
{
std::cin.getline(buff,sizeof(buff));
pSocket->async_write_some(buffer(buff),boost::bind(&AClient::handle_write,this,buff,_1,_2));
}
}
void handle_write(char* buffs,error_code ecode,size_t bytes_transferred)
{
char buff[MAX_BUFFERSIZE];
bzero(buff,sizeof(buff));
pSocket->async_read_some(buffer(buff),boost::bind(&AClient::handle_read,this,buff,_1,_2));
}
void handle_read(char* buffer,error_code code,size_t bytes_transferred)
{
std::cout<<buffer<<std::endl;
}
private:
io_service& m_ioserv;
boost::shared_ptr<tcp::socket> pSocket;
};
#endif

我们来讲这个程序进行简化下,主要是来看看其中的一些异步接口的调用方式,代码如下:

服务器端代码

 

#ifndef __ASERVER__H
#define __ASERVER__H

#include <iostream>
#include <string>
#include <unistd.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>

using namespace boost::asio;
using boost::system::error_code;
using ip::tcp;

class CService
{
public:
CService(io_service& io_serv):m_ioserv(io_serv),m_acceptor(io_serv,tcp::endpoint(tcp::v4(),9999)){}
~CService(){}
void start()
{
boost::shared_ptr<tcp::socket> pSocket(new tcp::socket(m_ioserv));
m_acceptor.async_accept(*pSocket,boost::bind(&CService::accept_handler,this,pSocket,_1));
}
void accept_handler(boost::shared_ptr<tcp::socket> pSocket,error_code ecode)
{
if(ecode) return;
start();
std::cout<<pSocket->remote_endpoint().address()<<std::endl;
boost::shared_ptr<std::string> msg(new std::string("my name is zmyer"));
pSocket->async_write_some(buffer(*msg),boost::bind(&CService::write_handler,this,msg,_1,_2));
}
void write_handler(boost::shared_ptr<std::string> msg,error_code ecode,size_t bytes_transferred)
{
if(ecode)
std::cout<<"send failed"<<std::endl;
else
std::cout<<*msg<<"send success"<<std::endl;
}
private:
io_service& m_ioserv;
ip::tcp::acceptor m_acceptor;
};
#endif

客户端代码

#ifndef __ACLIENT__H
#define __ACLIENT__H
#include <iostream>
#include <string>
#include <unistd.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
using namespace boost::asio;
using boost::system::error_code;
using ip::tcp;

class AClient
{
public:
AClient(io_service& io_serv):m_ioserv(io_serv),pSocket(new tcp::socket(m_ioserv))
{
}
~AClient(){}
void init()
{
ip::tcp::endpoint epoint(ip::address_v4::from_string("127.0.0.1"),9999);
pSocket->async_connect(epoint,boost::bind(&AClient::handle_connect,this,_1));
}
void handle_connect(error_code ecode)
{
if(ecode)
{
std::cout<<boost::system::system_error(ecode).what()<<std::endl;
pSocket->close();
return;
}
#define MAX_BUFFERSIZE 1024
char buff[MAX_BUFFERSIZE];
bzero(buff,sizeof(buff));
pSocket->async_read_some(buffer(buff),boost::bind(&AClient::handle_read,this,buff,_1,_2));
}
void handle_read(char* buffer,error_code code,size_t bytes_transferred)
{
std::cout<<buffer<<std::endl;
}
private:
io_service& m_ioserv;
boost::shared_ptr<tcp::socket> pSocket;
};
#endif

服务器测试代码

#include "AServer.h"

int main(int argc,char* argv[])
{
io_service ioserv;
CService server(ioserv);
server.start();
ioserv.run();
return 0;
}

客户端测试代码

#include "AClient.h"

int main(int argc,char* argv[])
{
io_service ioserv;
AClient aclient(ioserv);
aclient.init();
return 0;
}

总结

      本篇博文通过使用boost::asio库的异步I/O接口,简单地实现了两个案例,在这两个案例中,我们一方面需要了解boost::asio的用法,另一方面我们还是需要思考下boost::asio的设计思路,力争搞懂boost::asio一些实现方面的东西,这方面得需要看源码,先不管这些了,目前先好好地学会如何使用boost::asio吧,好了,本篇博文到此,结束。

如果需要,请注明转载,多谢