Last active
December 30, 2015 16:28
-
-
Save fjnl/7854464 to your computer and use it in GitHub Desktop.
C++ Advent Calendar 2013 9日目の記事のサンプルコードです。 http://d.hatena.ne.jp/fjnl/ C++ Advent Calendar: http://partake.in/events/91328710-3c7b-436e-bd4e-4d98d88333f9
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 <thread> | |
#include <future> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <boost/asio.hpp> | |
#include <boost/exception/all.hpp> | |
namespace asio = boost::asio; | |
using boost::asio::ip::tcp; | |
void client_main(asio::io_service& io_service) { | |
tcp::resolver resolver(io_service); | |
tcp::resolver::query q("localhost", "1234"); | |
tcp::socket socket(io_service); | |
asio::connect(socket, resolver.resolve(q)); | |
asio::streambuf buf; | |
std::string input; | |
while (std::cin) { | |
buf.consume(buf.size()); | |
std::cout << ">> " << std::flush; | |
std::getline(std::cin, input); | |
input += "\n"; | |
asio::write(socket, asio::buffer(input)); | |
asio::read_until(socket, buf, "\n"); | |
std::istream stream(&buf); | |
stream >> input; | |
std::cout << "--> " << input << std::endl; | |
} | |
} | |
int main() { | |
try { | |
asio::io_service io_service; | |
auto thread = std::async([&] { io_service.run(); }); | |
client_main(io_service); | |
thread.get(); | |
} catch (...) { | |
std::cerr << "error: " << boost::current_exception_diagnostic_information() << std::endl; | |
return 1; | |
} | |
return 0; | |
} |
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 <thread> | |
#include <future> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <boost/asio.hpp> | |
#include <boost/asio/spawn.hpp> | |
#include <boost/exception/all.hpp> | |
#include <boost/lexical_cast.hpp> | |
namespace asio = boost::asio; | |
using boost::asio::ip::tcp; | |
#ifndef NDEBUG | |
# define DEBUG(expr) do { std::cout << expr << std::endl; } while (0) | |
#else | |
# define DEBUG(expr) do {} while (0) | |
#endif | |
void connection_rountine(asio::yield_context yield, tcp::socket s) { | |
int acc = 0; | |
boost::system::error_code ec; | |
asio::streambuf buf; | |
std::string str; | |
DEBUG("enter connection_rountine"); | |
for (;;) { | |
buf.consume(buf.size()); | |
auto const n = asio::async_read_until(s, buf, "\n", yield[ec]); | |
if (ec) break; | |
std::istream stream(&buf); | |
int value; | |
stream >> value; | |
DEBUG("read " << n << " byte: " << value); | |
acc += value; | |
str = std::to_string(acc) + "\n"; | |
asio::async_write(s, asio::buffer(str), yield[ec]); | |
if (ec) break; | |
} | |
DEBUG("leave connection_rountine"); | |
} | |
void server_main(asio::io_service& io_service) { | |
asio::spawn(io_service, [&] (asio::yield_context yield) { | |
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1234)); | |
boost::system::error_code ec; | |
for (;;) { | |
DEBUG("accept"); | |
tcp::socket socket(io_service); | |
acceptor.async_accept(socket, yield); | |
asio::spawn(io_service, | |
[&] (asio::yield_context yc) { connection_rountine(yc, std::move(socket)); }); | |
} | |
}); | |
} | |
int main() { | |
try { | |
asio::io_service io_service; | |
server_main(io_service); | |
io_service.run(); | |
} catch (...) { | |
std::cerr << "error: " << boost::current_exception_diagnostic_information() << std::endl; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment