需要 boost:asio 异步服务器的示例,带有接收器和接受器

发布于 2024-12-03 05:58:42 字数 3250 浏览 1 评论 0原文

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

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-12-10 05:58:42

我不知道如何接收?

使用 async_read() 免费函数。

我将如何发送给所有客户列表?

迭代您的 m_Clients 容器并调用 async_write()


不相关,但不要这样做

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

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

I don't know how to receive?

use the async_read() free function.

and how I will do send to all client list?

iterate through your m_Clients container and invoke async_write() on each socket.


unrelated, but don't do this

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文