Thread: [asio-users] usage of async_receive_from (in udp broadcast application)
Brought to you by:
chris_kohlhoff
From: Bart De L. <bar...@pa...> - 2009-07-07 21:34:56
|
Hello I recently wrote an application that broadcasts a message on my network, and waits for the responses coming in. Sending the udp broadcast message goes OK, the machines that listen on the network send the responds alright, but i'm stuck getting the responses using "async_receive_from". Here is some code: udp::resolver resolver(io_service); udp::resolver::query query(udp::v4(), "255.255.255.255", "16543"); udp::endpoint receiver_endpoint = *resolver.resolve(query); // Create and open the socket socket_ = new udp::socket(io_service); socket_->open(udp::v4()); // Allow broadcasting asio::socket_base::broadcast option(true); socket_->set_option(option); // Send out our message boost::array<char, 1> send_buf = { 0 }; socket_->send_to(boost::asio::buffer(send_buf), receiver_endpoint); // read the responses in function 'Receive' socket_->async_receive_from(boost::asio::buffer(recv_buffer_), sender_endpoint_, Receive); // But io_service never calls Receive. (but calling "socket_->receive_from" does get me the responds) So here is the question: how do i correctly use async_receive_from? (all variables are defined in the .h (like io_service, socket_, etc )) Thanks for looking into this Bart |
From: Christopher K. <ch...@ko...> - 2009-07-08 05:53:29
|
On Tue, 07 Jul 2009 19:11 +0200, "Bart De Lathouwer" <bar...@pa...> wrote: > Hello > > I recently wrote an application that broadcasts a message on my network, > and waits for the responses coming in. > Sending the udp broadcast message goes OK, the machines that listen on > the network send the responds alright, but i'm stuck getting the > responses using "async_receive_from". > > Here is some code: > > udp::resolver resolver(io_service); > udp::resolver::query query(udp::v4(), "255.255.255.255", "16543"); > udp::endpoint receiver_endpoint = *resolver.resolve(query); > > // Create and open the socket > socket_ = new udp::socket(io_service); > socket_->open(udp::v4()); > > // Allow broadcasting > asio::socket_base::broadcast option(true); > socket_->set_option(option); > > // Send out our message > boost::array<char, 1> send_buf = { 0 }; > socket_->send_to(boost::asio::buffer(send_buf), receiver_endpoint); > > // read the responses in function 'Receive' > socket_->async_receive_from(boost::asio::buffer(recv_buffer_), > sender_endpoint_, Receive); > // But io_service never calls Receive. (but calling > "socket_->receive_from" does get me the responds) > > So here is the question: how do i correctly use async_receive_from? > (all variables are defined in the .h (like io_service, socket_, etc )) You didn't show it, so can you please confirm at which point you call io_service.run()? Thanks. Cheers, Chris |
From: Roger A. <rog...@wa...> - 2009-07-08 06:08:43
|
I have a pretty basic question, but it isn't obvious from your code, so: You are calling io_service::run somewhere, right? If not, your completion handler for the call to async_receive_from (i.e. Receive) will never get called. If you are calling it (perhaps in a different thread) then you also need to be careful that it has not already exited (due to lack of work) before the call to async_receive_from The asio examples are a very good place to start for an illustration of correct usage. From: Bart De Lathouwer [mailto:bar...@pa...] Sent: Wednesday, 8 July 2009 7:12 AM To: asi...@li... Subject: [asio-users] usage of async_receive_from (in udp broadcastapplication) Hello I recently wrote an application that broadcasts a message on my network, and waits for the responses coming in. Sending the udp broadcast message goes OK, the machines that listen on the network send the responds alright, but i'm stuck getting the responses using "async_receive_from". Here is some code: udp::resolver resolver(io_service); udp::resolver::query query(udp::v4(), "255.255.255.255", "16543"); udp::endpoint receiver_endpoint = *resolver.resolve(query); // Create and open the socket socket_ = new udp::socket(io_service); socket_->open(udp::v4()); // Allow broadcasting asio::socket_base::broadcast option(true); socket_->set_option(option); // Send out our message boost::array<char, 1> send_buf = { 0 }; socket_->send_to(boost::asio::buffer(send_buf), receiver_endpoint); // read the responses in function 'Receive' socket_->async_receive_from(boost::asio::buffer(recv_buffer_), sender_endpoint_, Receive); // But io_service never calls Receive. (but calling "socket_->receive_from" does get me the responds) So here is the question: how do i correctly use async_receive_from? (all variables are defined in the .h (like io_service, socket_, etc )) Thanks for looking into this Bart The information contained in this e-mail is likely to be confidential and may be legally privileged. It is intended only for the addressee. If you have received this message in error please notify the sender immediately at the above address. The disclosure, copying or distribution of this message or its contents without the prior approval of Wallingford Software is strictly prohibited. Wallingford Software is not liable for unauthorised disclosures nor for subsequent actions or omissions in reliance upon them. Registered in the UK, company no: 02288719 Wallingford Software Limited, Howbery Park, Wallingford, Oxfordshire, OX10 8BA |
From: Bart De L. <bar...@pa...> - 2009-07-08 08:31:44
|
Hello Christopher, Roger, I simply forgot to call io_service.run() (instant sunburn :-) The most basic acton i forgot to do. My bad. I knew i was missing something.... Thanks for pointing it out Bart ----- Original Message ----- From: Roger Austin To: asi...@li... Sent: Wednesday, July 08, 2009 5:39 AM Subject: Re: [asio-users] usage of async_receive_from (in udpbroadcastapplication) I have a pretty basic question, but it isn't obvious from your code, so: You are calling io_service::run somewhere, right? If not, your completion handler for the call to async_receive_from (i.e. Receive) will never get called. If you are calling it (perhaps in a different thread) then you also need to be careful that it has not already exited (due to lack of work) before the call to async_receive_from The asio examples are a very good place to start for an illustration of correct usage. From: Bart De Lathouwer [mailto:bar...@pa...] Sent: Wednesday, 8 July 2009 7:12 AM To: asi...@li... Subject: [asio-users] usage of async_receive_from (in udp broadcastapplication) Hello I recently wrote an application that broadcasts a message on my network, and waits for the responses coming in. Sending the udp broadcast message goes OK, the machines that listen on the network send the responds alright, but i'm stuck getting the responses using "async_receive_from". Here is some code: udp::resolver resolver(io_service); udp::resolver::query query(udp::v4(), "255.255.255.255", "16543"); udp::endpoint receiver_endpoint = *resolver.resolve(query); // Create and open the socket socket_ = new udp::socket(io_service); socket_->open(udp::v4()); // Allow broadcasting asio::socket_base::broadcast option(true); socket_->set_option(option); // Send out our message boost::array<char, 1> send_buf = { 0 }; socket_->send_to(boost::asio::buffer(send_buf), receiver_endpoint); // read the responses in function 'Receive' socket_->async_receive_from(boost::asio::buffer(recv_buffer_), sender_endpoint_, Receive); // But io_service never calls Receive. (but calling "socket_->receive_from" does get me the responds) So here is the question: how do i correctly use async_receive_from? (all variables are defined in the .h (like io_service, socket_, etc )) Thanks for looking into this Bart The information contained in this e-mail is likely to be confidential and may be legally privileged. It is intended only for the addressee. If you have received this message in error please notify the sender immediately at the above address. The disclosure, copying or distribution of this message or its contents without the prior approval of Wallingford Software is strictly prohibited. Wallingford Software is not liable for unauthorised disclosures nor for subsequent actions or omissions in reliance upon them. Registered in the UK, Company no: 02288719 Wallingford Software Limited, Howbery Park, Wallingford, Oxfordshire, OX10 8BA ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge ------------------------------------------------------------------------------ _______________________________________________ asio-users mailing list asi...@li... https://lists.sourceforge.net/lists/listinfo/asio-users ------------------------------------------------------------------------------ No virus found in this incoming message. Checked by AVG - www.avg.com Version: 8.5.375 / Virus Database: 270.13.7/2222 - Release Date: 07/07/09 05:53:00 |