c++ - udp broadcast using boost::asio under windows -
i'm having problems udp broadcast subsection of application. using boost 1.62.0 under windows 10.
void test_udp_broadcast(void) { boost::asio::io_service io_service; boost::asio::ip::udp::socket socket(io_service); boost::asio::ip::udp::endpoint remote_endpoint; socket.open(boost::asio::ip::udp::v4()); socket.set_option(boost::asio::ip::udp::socket::reuse_address(true)); socket.set_option(boost::asio::socket_base::broadcast(true)); remote_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::any(), 4000); try { socket.bind(remote_endpoint); socket.send_to(boost::asio::buffer("abc", 3), remote_endpoint); } catch (boost::system::system_error e) { std::cout << e.what() << std::endl; } }
i receive: send_to: requested address not valid in context catch.
i've attempted change endpoint any() broadcast(), throws same error on bind().
i program under linux, , code works on normal target. i'm scratching head i'm doing wrong here. can give me poke in right direction?
i believe want bind socket local endpoint any() (if wish receive broadcast packets - see this question), , send remote endpoint using broadcast() (see this question).
the following compiles me , not throw errors:
void test_udp_broadcast(void) { boost::asio::io_service io_service; boost::asio::ip::udp::socket socket(io_service); boost::asio::ip::udp::endpoint local_endpoint; boost::asio::ip::udp::endpoint remote_endpoint; socket.open(boost::asio::ip::udp::v4()); socket.set_option(boost::asio::ip::udp::socket::reuse_address(true)); socket.set_option(boost::asio::socket_base::broadcast(true)); local_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::any(), 4000); remote_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::broadcast(), 4000); try { socket.bind(local_endpoint); socket.send_to(boost::asio::buffer("abc", 3), remote_endpoint); } catch (boost::system::system_error e) { std::cout << e.what() << std::endl; } }
Comments
Post a Comment