【问题标题】:need example of boost:asio async server with receiver and acceptor需要 boost:asio 异步服务器的示例,带有接收器和接收器
【发布时间】:2011-09-04 06:19:10
【问题描述】:

我看了很多很好的例子,但我没有找到 如果有人可以帮助请帮助我,这是我目前的课程

#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));
}
//-----------------------------------------------------------------------------------------------------------------------

但我不知道如何接收 以及我将如何发送到所有客户列表

有人有一个很好的例子请分享它,或者 向我解释如何通过 boost 继续我的新课程 我试着去学习它。

【问题讨论】:

    标签: c++ sockets boost boost-asio


    【解决方案1】:

    我不知道如何接收?

    使用async_read()免费功能。

    以及我将如何发送到所有客户列表?

    遍历您的 m_Clients 容器并在每个 socket 上调用 async_write()


    不相关,但不要这样做

    m_AcceptMutex.lock();
    // ...
    m_AcceptMutex.unlock();
    

    这样做容易出错且不安全。在此处使用 RAII 来解锁析构函数中的互斥锁。您已经在使用 boost,所以请使用 boost::mutex::scoped_lock

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多