Skip to content

Instantly share code, notes, and snippets.

@edouarda
Created February 13, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edouarda/d50ca3007c25d18ab1c8c87e9aa6e749 to your computer and use it in GitHub Desktop.
Save edouarda/d50ca3007c25d18ab1c8c87e9aa6e749 to your computer and use it in GitHub Desktop.
#include <boost/asio.hpp>
#include <iostream>
int main(int argc, char ** argv)
{
try
{
boost::asio::io_context io_context;
boost::asio::ip::udp::socket s{io_context,
boost::asio::ip::udp::endpoint{boost::asio::ip::udp::v4(), 0}};
const boost::asio::ip::udp::endpoint ep{
boost::asio::ip::address::from_string("127.0.0.1"), 8080};
static constexpr size_t max_length = 100;
std::cout << "Enter message: ";
std::string request;
std::getline(std::cin, request);
s.send_to(boost::asio::buffer(request.data(), request.size() + 1), ep);
std::array<char, max_length> reply;
boost::asio::ip::udp::endpoint peer;
size_t reply_length = s.receive_from(boost::asio::buffer(reply), peer);
std::cout << "Reply is: ";
std::cout.write(reply.data(), reply_length);
std::cout << "\n";
}
catch (const boost::system::error_code & ec)
{
std::cerr << ec.message() << std::endl;
}
catch (std::exception & e)
{
std::cerr << e.what() << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment