#include <iostream> #include <string> #include "boost/asio.hpp" #include "boost/lexical_cast.hpp" using namespace std; using namespace boost; using boost::asio::ip::tcp; using boost::asio::io_service; class Client { public: Client (const string & hostname, unsigned short port); virtual ~Client (); // methods virtual void send (const string & message); virtual string recv (); virtual void close (); private: io_service * io_service_; tcp::socket * socket_; }; Client::Client (const string & hostname, unsigned short port) { io_service_ = new io_service(); socket_ = new tcp::socket(*io_service_); tcp::resolver resolver(*io_service_); tcp::resolver::query query(hostname, boost::lexical_cast<string, unsigned short>(port)); boost::system::error_code ec; tcp::resolver::iterator iter = resolver.resolve(query, ec); tcp::resolver::iterator end; // pick the first endpoint if (iter != end && ec == 0) { tcp::endpoint endpoint = *iter; std::cout << "Connecting to: " << endpoint << std::endl; socket_->connect(endpoint, ec); if (ec) { std::cerr << "Error: " << ec << std::endl; throw ec; } } } Client::~Client () { delete socket_; delete io_service_; } void Client::send (const string & message) { boost::asio::const_buffers_1 request(message.data(), message.size()); socket_->send(request); } string Client::recv () { char response[128]; size_t num = socket_->receive(boost::asio::buffer(response)); if (num > 0) { return string (response, num); } return ""; } void Client::close () { socket_->close(); } int main(int argc, char* argv[]) { Client client ("localhost", 2009); std::cout << client.recv() << endl; string request; do { std::cout << "Request: "; std::cin >> request; if (request == "q") break; client.send (request); std::cout << "Response: " << client.recv() << endl; } while (true); client.close(); return 0; }