boost :: asio不起作用

时间:2009-07-26 12:48:14

标签: boost network-programming

使用以下课程

标题:

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...

怎么了?我甚至尝试在主要部分放置一段时间(1),看看是否发生了什么......我甚至试图在接收处理程序解锁的主要条件中放置一个条件......所有都保持锁定...那么什么???不知道!

1 个答案:

答案 0 :(得分:1)

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

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

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

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