一个基于boost 库 asio 的聊天程序,该怎么解决
一个基于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,
一个基于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,