需要 boost:asio 异步服务器的示例,带有接收器和接受器
我看过很多好的例子,但我没有找到 如果有人可以帮助请帮助我,这是我当前的课程
#include "StdAfx.h"
#include "TNetwork.h"
//-----------------------------------------------------------------------------------------------------------------------
CNetwork::CNetwork(io_service & io_service) : m_IoService(io_service), m_Acceptor(new ip::tcp::acceptor(io_service))
{
m_AcceptMutex.initialize();
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::setConnector(void (*THISFUNC) (int))
{
m_Connector = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::setReceiver(void (*THISFUNC) (int, u_char * , int))
{
m_Receiver = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::setDisconnector(void (*THISFUNC) (int))
{
m_Disconnector = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------
bool CNetwork::start(USHORT Port)
{
DWORD dwStartTick = GetTickCount();
// ----
printf("[%s][Starting ...]\n", __FUNCTION__);
// ----
ip::tcp::endpoint selfpoint(ip::tcp::v4(), Port);
// ----
m_Acceptor->open(selfpoint.protocol());
m_Socket = new ip::tcp::socket(m_IoService);
// ----
try
{
m_Acceptor->bind(selfpoint);
m_Acceptor->listen();
}
catch(std::exception & e)
{
printf("[%s][%s]\n", __FUNCTION__, e.what());
// ----
return false;
}
// ----
printf("[%s][Started OK , within %u milisecond(s)]\n", __FUNCTION__, (GetTickCount() - dwStartTick));
// ----
ip::tcp::socket * temp = new ip::tcp::socket(m_IoService);
m_Acceptor->async_accept( * temp, boost::bind(& CNetwork::acceptor, this, temp, boost::asio::placeholders::error));
// ----
return true;
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::acceptor(ip::tcp::socket * socket, const boost::system::error_code & error)
{
if(error <= 0)
{
m_AcceptMutex.lock();
// ----
printf("[%s][New connection : %d.]\n", __FUNCTION__, m_Clients.size());
m_Clients.push_back(socket);
// ----
if(m_Connector != NULL)
{
m_Connector(m_Clients.size());
}
// ----
m_AcceptMutex.unlock();
// ----
//socket->async_receive(boost::asio::buffer(m_buffers),
}
else
{
printf("[%s][%s]\n", __FUNCTION__, error.message());
}
// ----
ip::tcp::socket * temp = new ip::tcp::socket(m_IoService);
// ----
// ----
m_Acceptor->async_accept( * temp, boost::bind(& CNetwork::acceptor, this, temp, boost::asio::placeholders::error));
}
//-----------------------------------------------------------------------------------------------------------------------
,但我不知道如何接收 以及我将如何发送给所有客户列表,
有人有一个好的例子请分享,或者 解释一下如何继续我的课程,我是新手,有提升 我试着去学习它。
I have been looked alot of good exmaple but i didnt find
if some one can help please help me, this is my current class
#include "StdAfx.h"
#include "TNetwork.h"
//-----------------------------------------------------------------------------------------------------------------------
CNetwork::CNetwork(io_service & io_service) : m_IoService(io_service), m_Acceptor(new ip::tcp::acceptor(io_service))
{
m_AcceptMutex.initialize();
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::setConnector(void (*THISFUNC) (int))
{
m_Connector = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::setReceiver(void (*THISFUNC) (int, u_char * , int))
{
m_Receiver = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::setDisconnector(void (*THISFUNC) (int))
{
m_Disconnector = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------
bool CNetwork::start(USHORT Port)
{
DWORD dwStartTick = GetTickCount();
// ----
printf("[%s][Starting ...]\n", __FUNCTION__);
// ----
ip::tcp::endpoint selfpoint(ip::tcp::v4(), Port);
// ----
m_Acceptor->open(selfpoint.protocol());
m_Socket = new ip::tcp::socket(m_IoService);
// ----
try
{
m_Acceptor->bind(selfpoint);
m_Acceptor->listen();
}
catch(std::exception & e)
{
printf("[%s][%s]\n", __FUNCTION__, e.what());
// ----
return false;
}
// ----
printf("[%s][Started OK , within %u milisecond(s)]\n", __FUNCTION__, (GetTickCount() - dwStartTick));
// ----
ip::tcp::socket * temp = new ip::tcp::socket(m_IoService);
m_Acceptor->async_accept( * temp, boost::bind(& CNetwork::acceptor, this, temp, boost::asio::placeholders::error));
// ----
return true;
}
//-----------------------------------------------------------------------------------------------------------------------
void CNetwork::acceptor(ip::tcp::socket * socket, const boost::system::error_code & error)
{
if(error <= 0)
{
m_AcceptMutex.lock();
// ----
printf("[%s][New connection : %d.]\n", __FUNCTION__, m_Clients.size());
m_Clients.push_back(socket);
// ----
if(m_Connector != NULL)
{
m_Connector(m_Clients.size());
}
// ----
m_AcceptMutex.unlock();
// ----
//socket->async_receive(boost::asio::buffer(m_buffers),
}
else
{
printf("[%s][%s]\n", __FUNCTION__, error.message());
}
// ----
ip::tcp::socket * temp = new ip::tcp::socket(m_IoService);
// ----
// ----
m_Acceptor->async_accept( * temp, boost::bind(& CNetwork::acceptor, this, temp, boost::asio::placeholders::error));
}
//-----------------------------------------------------------------------------------------------------------------------
but i dont know how to receive
and how i will do send to all client list
someone has a good a exmaple please share it , or
explain me how to continue my class im new with boost
i trying to leran it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 async_read() 免费函数。
迭代您的
m_Clients
容器并调用 async_write()。不相关,但不要这样做
,这样做很容易出错,而且不安全。此处使用 RAII 来解锁析构函数中的互斥体。您已经在使用 boost,因此请使用 boost::mutex::scoped_lock。
use the async_read() free function.
iterate through your
m_Clients
container and invoke async_write() on eachsocket
.unrelated, but don't do this
Doing so is error prone and not exception safe. Use RAII here to unlock the mutex in a destructor. You're already using boost, so use boost::mutex::scoped_lock.