Boost
C++ Libraries
...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
This version of Boost is under active development. You are currently in the master branch. The current version is 1.92.0.
This tutorial program shows how to use asio to implement a server application with UDP.
int main() { try { boost::asio::io_context io_context;
Create an ip::udp::socket object to receive requests on UDP port 13.
udp::socket socket(io_context, udp::endpoint(udp::v4(), 13));
Wait for a client to initiate contact with us. The remote_endpoint object will be populated by ip::udp::socket::receive_from().
for (;;) { std::array<char, 1> recv_buf; udp::endpoint remote_endpoint; socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint);
Determine what we are going to send back to the client.
std::string message = make_daytime_string();
Send the response to the remote_endpoint.
boost::system::error_code ignored_error; socket.send_to(boost::asio::buffer(message), remote_endpoint, 0, ignored_error); } }
Finally, handle any exceptions.
catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }
See the full source listing
Return to the tutorial index