【问题标题】:boost::asio doesn't workboost::asio 不起作用
【发布时间】:2010-11-14 03:29:23
【问题描述】:

下面的类

标题:

namespace msgSrv {

class endPoint {

public:
    asio::ip::udp::endpoint ep;
    endPoint(std::string ip, int port);

};

class msgSrv {

private:
    asio::ip::udp::socket *asioSocket;
    asio::io_service *asioIoService;
    int listenPort;
    boost::array<char, 1> rcvBuff;
    asio::ip::udp::endpoint lastRcvdPcktEndp;
    char * sbuff;

public:

    boost::condition_variable cond;
    boost::mutex mut;
    msgSrv(int listenPort);
    virtual ~msgSrv();

    void start();
    void pckRcvd(const asio::error_code& error, std::size_t bytes_transferred);
    void sendTo(const char* buff, int len, endPoint ep);
    void sendHnd(const asio::error_code& error, std::size_t bytes_transferred);

};

}

.cpp

#include "msgSrv.h"

namespace msgSrv {

endPoint::endPoint(const std::string ip, int port) {
    asio::ip::address addr = asio::ip::address::from_string(ip);
    ep = asio::ip::udp::endpoint(addr, port);
}

msgSrv::msgSrv(int listenPort) {
    // TODO Auto-generated constructor stub
    this->listenPort = listenPort;
    try {
        asioIoService = new asio::io_service();
        asioSocket = new asio::ip::udp::socket(*asioIoService,
                asio::ip::udp::endpoint(asio::ip::udp::v4(), listenPort)); //new asio::ip::udp::socket_(*asioIoService, udp::endpoint(udp::v4(), listenPort));
    } catch (std::exception &e) {
        std::cerr << "Error initializing ioservice or socket:" << e.what();
    }
    asioIoService->run();

}

msgSrv::~msgSrv() {
    // TODO Auto-generated destructor stub
    delete asioIoService;
    delete asioSocket;
}

void msgSrv::start() {

    asioSocket->async_receive_from(asio::buffer(rcvBuff), lastRcvdPcktEndp,
            boost::bind(&msgSrv::pckRcvd, this, asio::placeholders::error,
                    asio::placeholders::bytes_transferred));

}

void msgSrv::pckRcvd(const asio::error_code& error,
        std::size_t bytes_transferred) {
    std::cout << "Rcvd! " << lastRcvdPcktEndp.address().to_string() << ":"
            << lastRcvdPcktEndp.port() << "\n";
}

void msgSrv::sendTo(const char* buff, int len, endPoint ep) {
    sbuff = new char[len];
    mempcpy(sbuff, buff, len);
    asioSocket->async_send_to(asio::buffer(sbuff, len), ep.ep, boost::bind(
            &msgSrv::sendHnd, this, asio::placeholders::error,
            asio::placeholders::bytes_transferred));

}

void msgSrv::sendHnd(const asio::error_code& error,
        std::size_t bytes_transferred) {
    std::cout << "Snt!\n";

    delete sbuff;
}

}

以及以下“主”文件:

int main()
{
    msgSrv::msgSrv aa(4450);
    aa.start();
    msgSrv::endPoint ep("127.0.0.1", 4450);
    std::string a("Prova!");
    int len = a.length();
    aa.sendTo(a.c_str(), len, ep);
    std::cout << "sent...\n";
    std::cout << "notified...\n";
}

我得到的是:

terminate called after throwing an instance of 'asio::system_error'
  what():  mutex: Invalid argument
sent...
notified...

怎么了??我什至尝试在 main 中放置一段时间(1),看看是否发生了什么事……我什至尝试在 main 中放置一个由接收处理程序解锁的条件……所有都保持锁定……那又如何???不知道!

【问题讨论】:

    标签: boost network-programming


    【解决方案1】:

    我没有看到你实际上锁定了任何 muxtex,所以这个错误很奇怪。

    但是你的问题是在构造函数中调用asioIoService-&gt;run(),女巫陷入无限循环。解决方案是创建一个新的boost::thread,自己调用asioIoService-&gt;run()。该线程将处理所有作业。您也可以使用多个线程调用 asio::io_service::run(),以同时处理多个作业。

    m_thread = new boost::thread(boost::bind(&asio::io_service::run,asioIoService));
    

    调用asioIoService-&gt;stop()会强制退出asio::io_service::run(),从而关闭线程。您必须加入此线程以确保线程在销毁 msgSrv 类的析构函数中的 asioIoService 指针之前终止。

    【讨论】:

    • 我 7 年级的语言艺术老师用尖刺棒打败了我们所有人,直到我们猜对了……女巫戴着黑帽子……这是为了选择。始终将其拼写为 which,您将在 90% 的情况下正确。
    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2014-06-01
    相关资源
    最近更新 更多