Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost C++ UDP example can not transmit over internet
    text
    copied!<p>Hello i am experimenting with Boost C++ UDP client-server.</p> <p>I took the one of the examples that comes with ASIO and modified it a bit.</p> <p>It works very well on local network but when i host the server to one of our servers with ports correctly forwarded, it doesn't work.</p> <p>I run the UDP server and try to transmit with the client from home over the internet but none of the packets arrive to the server. I am sure that the ports are forwarded correctly and that the firewall is not getting in the way.</p> <p>Is there any difference between local network and internet as far as UDP communication is concerned?</p> <p>Following is the code of client and server:</p> <p>----Client----</p> <pre><code>#include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;iostream&gt; #include &lt;boost/asio.hpp&gt; using boost::asio::ip::udp; enum { max_length = 1024 }; int main(int argc, char* argv[]) { char request[max_length]; try { boost::asio::io_service io_service; udp::socket s(io_service, udp::endpoint(udp::v4(), 0)); udp::resolver resolver(io_service); udp::resolver::query query(udp::v4(), "MyPublicIp", "3002"); udp::resolver::iterator iterator = resolver.resolve(query); using namespace std; // For strlen. std::cout &lt;&lt; "Write to me: "; while(1) { std::cin.getline(request, max_length); size_t request_length = strlen(request); s.send_to(boost::asio::buffer(request, request_length), *iterator); char reply[max_length]; udp::endpoint sender_endpoint; size_t reply_length = s.receive_from(boost::asio::buffer(reply, max_length), sender_endpoint); std::cout &lt;&lt; Say:"; std::cout.write(reply, reply_length); std::cout &lt;&lt; "\n"; std::cout &lt;&lt; Say What?" ; } } catch (std::exception&amp; e) { std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n"; } std::cin.getline(request, max_length); return 0; } </code></pre> <p>------Server------------</p> <pre><code>#include &lt;boost/chrono.hpp&gt; #include &lt;cstdlib&gt; #include &lt;iostream&gt; #include &lt;boost/bind.hpp&gt; #include &lt;boost/asio.hpp&gt; #include &lt;boost\thread\thread.hpp&gt; #include &lt;ctime&gt; using boost::asio::ip::udp; class server { public: server(boost::asio::io_service&amp; io_service, short port) : io_service_(io_service), socket_(io_service, udp::endpoint( boost::asio::ip::address_v4::any(), port)) { std::fill(data_, data_ + max_length, 0); socket_.async_receive_from( boost::asio::buffer(data_, max_length), sender_endpoint_, boost::bind(&amp;server::handle_receive_from, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void handle_receive_from(const boost::system::error_code&amp; error, size_t bytes_recvd) { if (!error &amp;&amp; bytes_recvd &gt; 0) { td::cout &lt;&lt; "Connection from: " &lt;&lt; sender_endpoint_.address().to_string() &lt;&lt; " " &lt;&lt; sender_endpoint_.port() &lt;&lt; std::endl; socket_.async_send_to( boost::asio::buffer(data_, strlen(data_)), sender_endpoint_, boost::bind(&amp;server::handle_send_to, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } else { socket_.async_receive_from( boost::asio::buffer(data_, max_length), sender_endpoint_, boost::bind(&amp;server::handle_receive_from, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } } void handle_send_to(const boost::system::error_code&amp; /*error*/, size_t /*bytes_sent*/) { std::fill(data_, data_ + max_length, 0); socket_.async_receive_from( boost::asio::buffer(data_, max_length), sender_endpoint_, boost::bind(&amp;server::handle_receive_from, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } private: boost::asio::io_service&amp; io_service_; udp::socket socket_; udp::endpoint sender_endpoint_; enum { max_length = 300 }; char data_[max_length]; }; int main(int argc, char* argv[]) { try { std::cout &lt;&lt; "Starting server" &lt;&lt; std::endl; boost::asio::io_service io_service; short port = 3002; using namespace std; server s(io_service, port); std::cout &lt;&lt; "Server started. Listening on port:" &lt;&lt;port &lt;&lt; std::endl; io_service.run(); } catch (std::exception&amp; e) { std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n"; } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload