Created
January 3, 2016 17:06
-
-
Save ybouhjira/b223fee26711fdb3898f to your computer and use it in GitHub Desktop.
Daytime client
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 <iostream> | |
#include <boost/array.hpp> | |
#include <boost/asio.hpp> | |
using boost::asio::ip::tcp; | |
namespace as = boost::asio; | |
int main(int argc, char* argv[]) | |
{ | |
try | |
{ | |
if (argc != 3) | |
{ | |
std::cerr << "Usage: client <host> <port>" << std::endl; | |
return 1; | |
} | |
boost::asio::io_service io_service; | |
tcp::resolver resolver(io_service); | |
tcp::resolver::query query(argv[1], argv[2]); | |
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); | |
tcp::socket socket(io_service); | |
boost::asio::connect(socket, endpoint_iterator); | |
for (;;) | |
{ | |
boost::array<char, 128> buf; | |
boost::system::error_code error; | |
size_t len = socket.read_some(boost::asio::buffer(buf), error); | |
if (error == boost::asio::error::eof) | |
break; // Connection closed cleanly by peer. | |
else if (error) | |
throw boost::system::system_error(error); // Some other error. | |
std::cout.write(buf.data(), len); | |
} | |
} | |
catch (std::exception& e) | |
{ | |
std::cerr << e.what() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment