Skip to content

Instantly share code, notes, and snippets.

@fjnl fjnl/client.cpp
Last active Dec 30, 2015

Embed
What would you like to do?
C++ Advent Calendar 2013 9日目の記事のサンプルコードです。 http://d.hatena.ne.jp/fjnl/ C++ Advent Calendar: http://partake.in/events/91328710-3c7b-436e-bd4e-4d98d88333f9
#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;
}
#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
You can’t perform that action at this time.