Created
February 13, 2018 14:30
-
-
Save edouarda/d50ca3007c25d18ab1c8c87e9aa6e749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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