首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个基于boost 库 asio 的聊天程序解决办法

2012-03-24 
一个基于boost 库 asio 的聊天程序一个基于boost 库 asio 的聊天程序,我在服务器端知道 客户端的IP地址 怎

一个基于boost 库 asio 的聊天程序
一个基于boost 库 asio 的聊天程序,我在服务器端知道 客户端的IP地址 怎么向每个客户端发送数据 比如说 我在服务器端有10 客服端的IP地址 怎么把一条数据发送给这10个客户端??

服务器端源代码如下:

#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;
using namespace std;
#define max_len 1024
string stIp[100];
class clientSession:public boost::enable_shared_from_this<clientSession>
{
  public:
  tcp::socket m_socket;
  char bufRec[max_len];
char bufSed[max_len];
 
typedef boost::shared_ptr<clientSession> session_ptr;
  public:
  clientSession(boost::asio::io_service& ioservice):m_socket(ioservice)
  {
  memset(bufRec,0,sizeof(bufRec));
memset(bufSed,0,sizeof(bufSed));
for(int i=0;i<100;i++)
{
stIp[i]="";
}
  }
  ~clientSession()
  {}
  tcp::socket& socket()
  {
  return m_socket;
  }
  void start()
  {
m_socket.async_read_some(boost::asio::buffer(bufRec,max_len),
  boost::bind(&clientSession::handle_read,shared_from_this(),
  boost::asio::placeholders::error)
);
boost::asio::async_write(m_socket,
  boost::asio::buffer("link successed!"),
  boost::bind(&clientSession::handle_write,shared_from_this(),
  boost::asio::placeholders::error)
);
  }
  private:
  void handle_write(const boost::system::error_code& error)
  {
  if(error)
  {
  m_socket.close();
  }
for(int i=0;i<100,!stIp[i].empty();i++) //在 这里实现 向各个客户端发送某个客户端所说的话
{
m_socket.remote_endpoint().address().from_string(stIp[i].c_str());
boost::asio::async_write(m_socket,
  boost::asio::buffer(bufRec),
  boost::bind(&clientSession::handle_write,shared_from_this(),
  boost::asio::placeholders::error)
);
}
  }
  void handle_read(const boost::system::error_code& error)
  {
  if(!error)
  {
 
cout <<m_socket.remote_endpoint().address()<<" say : "<< bufRec << endl;
  m_socket.async_read_some(boost::asio::buffer(bufRec,max_len),
  boost::bind(&clientSession::handle_read,
shared_from_this(),
boost::asio::placeholders::error)
);
  }
  else
  {
  m_socket.close();
  }
  } 
};
class serverApp
{  
private:
  boost::asio::io_service& m_ioservice;
  tcp::acceptor acceptor_;
 
  typedef boost::shared_ptr<clientSession> session_ptr;
  public:
  serverApp(boost::asio::io_service& ioservice,tcp::endpoint& endpoint):m_ioservice(ioservice),acceptor_(ioservice,endpoint)
  {
  session_ptr new_session(new clientSession(ioservice));
  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)
  {
  //实现对每个客户端的数据处理
  session->start();
  //每一个session就是一个客户端
  session_ptr new_session(new clientSession(m_ioservice));
  acceptor_.async_accept(new_session->socket(),
  boost::bind(&serverApp::handle_accept,
this,
boost::asio::placeholders::error,
new_session)
);
 
cout << "get a new client: "<<session->m_socket.remote_endpoint().address() << endl;
int m=0;
stIp[m++]=session->m_socket.remote_endpoint().address().to_string();
  }
  }  
};
int main(int argc , char* argv[])
{  
cout<<"服务器端启动!"<<endl;
  boost::asio::io_service myIoService;
  tcp::endpoint endPoint(tcp::v4(),8100);
  serverApp sa(myIoService,endPoint);
  myIoService.run();
  return 0;
}

[解决办法]
可以循环对这10个地址发送数据

热点排行
Bad Request.