#9406 closed Bugs (invalid)
[asio] The asio handle shouldnot be called
Reported by: | Owned by: | chris_kohlhoff | |
---|---|---|---|
Milestone: | To Be Determined | Component: | asio |
Version: | Boost 1.55.0 | Severity: | Problem |
Keywords: | Cc: |
Description
I want to write a UDP server, which can send and receive package. The following is the demo code:
Begin of code #include "stdafx.h" #include "boost/bind.hpp" #include "boost/asio.hpp"
class UDPServer { public:
UDPServer()
:m_socket(m_io_service), m_timer(m_io_service)
{ }
virtual ~UDPServer() {
}
void Initialize(const std::string& strIP, int nPort) {
boost::system::error_code ec; boost::asio::ip::address ad = boost::asio::ip::address::from_string(strIP, ec); boost::asio::ip::udp::endpoint ep(ad, nPort); m_socket.open(ep.protocol()); m_socket.bind(ep, ec); {
TEST CODE: boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("172.18.8.111", ec), 8001); m_vServerGroup.push_back(ep);
}
}
void Start() {
StartAsyncReceiveFrom(); NoticeSelfUp(); while (true) {
boost::system::error_code ec; m_io_service.poll(ec);
}
}
void SendUDPMsg(const char* pMsg, int nLen, boost::asio::ip::udp::endpoint& dest_ep) {
m_socket.async_send_to(
boost::asio::buffer(pMsg, nLen), dest_ep, boost::bind(&UDPServer::HandleSendTo, this, _1, _2, boost::ref(dest_ep)));
}
protected:
void NoticeSelfUp() {
size_t count = m_vServerGroup.size(); char* pBuff = "this is up"; for (size_t i = 0; i < count; ++i) {
SendUDPMsg(pBuff, strlen(pBuff), m_vServerGroup[i]);
} m_timer.expires_from_now(boost::posix_time::seconds(5)); m_timer.async_wait(boost::bind(&UDPServer::NoticeSelfUp, this));
}
void HandleReceiveFrom(const boost::system::error_code& ec, size_t bytes_recvd) {
if (ec) {
std::cout<<"HandleReceiveFrom error: \t"<<ec<<std::endl;
} else {
TODO
}
void HandleSendTo(const boost::system::error_code& ec, size_t bytes_sent, boost::asio::ip::udp::endpoint& ep) {
if (ec) {
std::cout<<"HandleReceiveFrom error: \t"<<ec<<"\t"<<ep<<std::endl;
} else {
TODO
}
}
void StartAsyncReceiveFrom() {
m_socket.async_receive_from(boost::asio::buffer(m_buff, max_length),
m_sender_point, boost::bind(&UDPServer::HandleReceiveFrom, this, _1, _2));
}
private:
boost::asio::io_service m_io_service; boost::asio::ip::udp::socket m_socket; boost::asio::ip::udp::endpoint m_sender_point; boost::asio::deadline_timer m_timer; std::vector<boost::asio::ip::udp::endpoint> m_vServerGroup; enum { max_length = 1024 }; char m_buff[max_length];
};
int _tmain(int argc, _TCHAR* argv[]) {
UDPServer server; server.Initialize("172.18.8.111", 8000); server.Start(); return 0;
}
End of code
What's more, localmachine IP is 172.18.8.111, the port number 8001 is not opened. After I call SendUDPMsg, the handle UDPServer::HandleReceiveFrom is called, but there is no receive operation for the socket, I just called the send function. In my opinion, the HandleReceiveFrom should not be called!
Attachments (1)
Change History (4)
by , 9 years ago
Attachment: | asio_error.cpp added |
---|
follow-up: 2 comment:1 by , 9 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
asio_error.cpp, line 85
comment:2 by , 9 years ago
Replying to chris_kohlhoff:
asio_error.cpp, line 85
Thank you for your replying, but I don't understand your meaning. How should I do to fix this problem? How to set to invalid? Can you write some code?
comment:3 by , 9 years ago
Change line 85 from:
std::cout<<"HandleReceiveFrom error: \t"<<ec<<"\t"<<ep<<std::endl;
to:
std::cout<<"HandleSendTo error: \t"<<ec<<"\t"<<ep<<std::endl;
test code for this issue