boost库之aiso通信

9 篇文章 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值