码迷,mamicode.com
首页 > 其他好文 > 详细

boost库之aiso通信

时间:2015-03-03 20:43:52      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <iostream>
using boost::asio::ip::tcp;

#define max_len 1024


class clientSession
 :public boost::enable_shared_from_this<clientSession>
{
public:
 clientSession(boost::asio::io_service& ioservice):m_socket(ioservice)
 {
  memset(m_data, 0, sizeof(m_data));
 }
 ~clientSession()
 {}
 tcp::socket& socket()
 {
  return m_socket;
 }
 void start()
 {
  //发送远程消息
  boost::asio::async_write(m_socket,
   boost::asio::buffer("link successed!"),
   boost::bind(&clientSession::handle_write,shared_from_this(),
   boost::asio::placeholders::error));
  //获取远程消息
  m_socket.async_read_some(boost::asio::buffer(m_data,max_len),
   boost::bind(&clientSession::handle_read,shared_from_this(),
   boost::asio::placeholders::error));
 }
private:
 void handle_write(const boost::system::error_code& error)
 {
  if(error)
  {
   m_socket.close();
  }
 }
 void handle_read(const boost::system::error_code& error)
 {

  if(!error)
  {
   std::cout << m_data << std::endl;
   m_socket.async_read_some(boost::asio::buffer(m_data,max_len),
    boost::bind(&clientSession::handle_read,shared_from_this(),
    boost::asio::placeholders::error));
  }
  else
  {
   m_socket.close();
  }
 }

private:
 tcp::socket m_socket;
 char m_data[max_len];
};

class serverApp
{
 typedef boost::shared_ptr<clientSession> session_ptr;
public:
 serverApp(boost::asio::io_service& ioservice,tcp::endpoint& endpoint)
  :m_ioservice(ioservice), m_acceptor(ioservice,endpoint)
 {
  session_ptr new_session(new clientSession(ioservice));
  //创建监听,接收到链接后handle_accept
  m_acceptor.async_accept(new_session->socket(),
   boost::bind(&serverApp::handle_accept,this,boost::asio::placeholders::error,
   new_session));
 }
 ~serverApp()
 {
 }
private:
 void handle_accept(const boost::system::error_code& error,session_ptr& session)
 {
  if(!error)
  {
   std::cout << "get a new client!" << std::endl;
   //实现对每个客户端的消息处理
   session->start();

   //new新的会话,继续接收新的客户端
   session_ptr new_session(new clientSession(m_ioservice));
   m_acceptor.async_accept(new_session->socket(),
    boost::bind(&serverApp::handle_accept,this,boost::asio::placeholders::error,
    new_session));
  }
 }
private:
 boost::asio::io_service& m_ioservice;
 tcp::acceptor m_acceptor;
};

int main(int argc , char* argv[])
{
 boost::asio::io_service myIoService;
 short port = 8100;
 tcp::endpoint endPoint(tcp::v4(),port);
 serverApp server(myIoService,endPoint);
 myIoService.run();
 return 0;
}


客户端

#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
using boost::asio::ip::tcp;
class client
{
public:
 client(boost::asio::io_service& io_service,tcp::endpoint& endpoint)
  : socket(io_service)//这里就把socket实例化了
 {
  //连接服务端 connect
  socket.async_connect(endpoint,
   boost::bind(&client::handle_connect,this,boost::asio::placeholders::error)
   );
  memset(m_getBuffer, 0, sizeof(m_getBuffer));
 }
 ~client()
 {}
private:
 void handle_connect(const boost::system::error_code& error)
 {
  if(!error)
  {
   //发送信息
   boost::asio::async_write(socket,boost::asio::buffer("hello,server!"),
    boost::bind(&client::handle_write,this,boost::asio::placeholders::error));
   //获取远程消息
   socket.async_read_some(boost::asio::buffer(m_getBuffer,1024),
    boost::bind(&client::handle_read,this,boost::asio::placeholders::error)
    );
  }
  else
  {
   socket.close();
  }
 }
 void handle_read(const boost::system::error_code& error)
 {
  if(!error)
  {
   std::cout << m_getBuffer << std::endl;
   socket.async_read_some(
    boost::asio::buffer(m_getBuffer,1024),
    boost::bind(&client::handle_read,this,boost::asio::placeholders::error)
    );
  }
  else
  {
   socket.close();
  }
 }
 void handle_write(const boost::system::error_code& error)
 {
 }

private:
 tcp::socket socket;
 char m_getBuffer[1024];
};

int main(int argc,char* argv[])
{
 //IO_SERVICE功能我们可以看似socket
 boost::asio::io_service io_service;
 tcp::endpoint endpoint(boost::asio::ip::address_v4::from_string("192.168.100.58"),8100);
 boost::shared_ptr<client> client_ptr(new client(io_service,endpoint));
 //开始执行回调函数如:handle_connect
 io_service.run();
 return 0;
}


boost库之aiso通信

标签:

原文地址:http://blog.csdn.net/leeboy_wang/article/details/44042123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!