Skip to content

Instantly share code, notes, and snippets.

@edouarda
Created February 13, 2018 13:36
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/c0328c85edcf7aee8b8c6f8796859c90 to your computer and use it in GitHub Desktop.
Save edouarda/c0328c85edcf7aee8b8c6f8796859c90 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::tcp::socket s{io_context};
const boost::asio::ip::tcp::endpoint ep{
boost::asio::ip::address::from_string("127.0.0.1"), 666};
s.connect(ep);
static constexpr size_t max_length = 100;
std::cout << "Enter message: ";
std::string request;
std::getline(std::cin, request);
boost::asio::write(
s, boost::asio::buffer(request.data(), request.size() + 1));
std::array<char, max_length> reply;
size_t reply_length = s.read_some(boost::asio::buffer(reply));
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