boost::asio不';行不通
跟下面的班级 标题:boost::asio不';行不通,boost,network-programming,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 listenP
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);
};
}
怎么了??我甚至试着在主体上放一段时间,看看是否有什么事情发生。。。我甚至试图在接收处理程序解锁的main中设置一个条件。。。所有的东西都锁着。。。那又怎么样???不知道 我看不到您实际上锁定了任何muxtex,所以这个错误很奇怪 然而,您的问题是在构造函数内部调用,它落入无限循环中。解决方案是创建一个新的witch调用
asioIoService->run()
本身。此线程将处理所有作业。您还可以使用多个线程调用asio::io_service::run(),以同时处理多个作业
m_thread = new boost::thread(boost::bind(&asio::io_service::run,asioIoService));
调用将强制退出asio::io_service::run(),从而关闭线程。您必须加入此线程,以确保线程在销毁msgSrv类的析构函数中的
asioIoService
指针之前终止。我七年级的语言艺术老师用钉子棍子打了我们所有人,直到我们得到正确的答案。。。女巫戴着一顶黑帽子。。。这是供选择的。把它拼成which,你90%的时候都是对的。
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...
m_thread = new boost::thread(boost::bind(&asio::io_service::run,asioIoService));