c++ boost asio库初学习

时间:2023-03-08 23:30:47
c++ boost asio库初学习

前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考。

同步server:

 // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
// #include "stdafx.h"
#include "boost/asio.hpp"
#include "boost/bind.hpp" using namespace boost::asio; io_service service;
size_t read_complete(char * buff, const boost::system::error_code & err, size_t bytes) {
if (err) return ;
bool found = std::find(buff, buff + bytes, '\n') < buff + bytes;
// 我们一个一个读取直到读到回车,不缓存
return found ? : ;
}
void handle_connections() {
ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::tcp::v4(), ));
char buff[];
while (true) {
ip::tcp::socket sock(service);
acceptor.accept(sock);
int bytes = read(sock, buffer(buff), boost::bind(read_complete, buff, _1, _2));
std::string msg(buff, bytes);
sock.write_some(buffer(msg));
sock.close();
}
}
int _tmain(int argc, char* argv[]) {
handle_connections();
}

同步client:

 // boost_aiso_test.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
// #include "stdafx.h" #include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/thread.hpp> using namespace boost::asio; io_service service;
ip::address_v4 add;
ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), ); size_t read_complete(char * buf, const boost::system::error_code & err, size_t bytes)
{
if (err) return ;
bool found = std::find(buf, buf + bytes, '\n') < buf + bytes;
// 我们一个一个读取直到读到回车,不缓存
return found ? : ;
}
void sync_echo(std::string msg) {
msg += "\n";
ip::tcp::socket sock(service);
sock.connect(ep);
sock.write_some(buffer(msg));
char buf[];
int bytes = read(sock, buffer(buf), boost::bind(read_complete, buf, _1, _2));
std::string copy(buf, bytes - );
msg = msg.substr(, msg.size() - );
std::cout << "server echoed our " << msg << ": " << (copy == msg ? "OK" : "FAIL") << std::endl;
sock.close();
}
int _tmain(int argc, char* argv[]) {
char* messages[] = { "John says hi", "so does James", "Lucy just got home", "Boost.Asio is Fun!", };
boost::thread_group threads;
for (char ** message = messages; *message; ++message) {
threads.create_thread(boost::bind(sync_echo, *message));
boost::this_thread::sleep(boost::posix_time::millisec());
}
threads.join_all();
}

-------------------------------------------------------- 异步是参考其他博客 --------------------------------------------------------

异步server:

 #include "stdafx.h"

 #include <boost/asio.hpp>
#include <iostream>
using namespace boost::asio;
class Server
{
typedef ip::tcp::socket socket_type;
typedef std::shared_ptr<socket_type> sock_ptr;
public:
Server() :m_acceptor(m_io, ip::tcp::endpoint(ip::tcp::v4(), ))
{
accept();
}
void run()
{
m_io.run();
}
private:
void accept()
{
sock_ptr sock(new socket_type(m_io));
m_acceptor.async_accept(*sock,
std::bind(&Server::accept_handler, this, std::placeholders::_1, sock));
}
void accept_handler(const boost::system::error_code& ec, sock_ptr sock)
{
if (ec)
{
return;
}
std::cout << "client:";
std::cout << sock->remote_endpoint().address() << std::endl;
sock->async_write_some(buffer("hello asio"),
std::bind(&Server::write_handler, this, std::placeholders::_1));
accept();
}
void write_handler(const boost::system::error_code&)
{
std::cout << "send msg complete." << std::endl;
}
private:
io_service m_io;
ip::tcp::acceptor m_acceptor;
};
int main()
{
try
{
std::cout << "server" << std::endl;
Server svr;
svr.run();
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
return ;
}

异步client:

 #include "stdafx.h"

 #include <boost/asio.hpp>
#include <iostream>
using namespace boost::asio;
class Client
{
typedef ip::tcp::socket socket_type;
typedef std::shared_ptr<socket_type> sock_ptr;
public:
Client() :m_ep(ip::address::from_string("127.0.0.1"), )
{
start();
}
void run()
{
m_io.run();
}
private:
void start()
{
sock_ptr sock(new socket_type(m_io));
sock->async_connect(m_ep, std::bind(&Client::connect_handler, this, std::placeholders::_1, sock));
}
void connect_handler(const boost::system::error_code& ec, sock_ptr sock)
{
if (ec)
{
return;
}
std::cout << "receive from:" << sock->remote_endpoint().address() << std::endl;
sock->async_read_some(buffer(m_buf),
std::bind(&Client::read_handler, this, std::placeholders::_1));
}
void read_handler(const boost::system::error_code& ec)
{
if (ec)
{
return;
}
std::cout << m_buf << std::endl;
}
private:
io_service m_io;
ip::tcp::endpoint m_ep;
enum { max_length = };
char m_buf[max_length];
};
int main()
{
try
{
std::cout << "client start." << std::endl;
Client cl;
cl.run();
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
getchar();
return ;
}