| 1 | #include <boost/asio/ip/tcp.hpp>
|
|---|
| 2 | #include <boost/asio/write.hpp>
|
|---|
| 3 | #include <boost/asio/read.hpp>
|
|---|
| 4 | #include <boost/exception/all.hpp>
|
|---|
| 5 | #include <boost/lexical_cast.hpp>
|
|---|
| 6 | #include <boost/thread.hpp>
|
|---|
| 7 | #include <iostream>
|
|---|
| 8 |
|
|---|
| 9 | using namespace boost;
|
|---|
| 10 | using namespace boost::asio;
|
|---|
| 11 | using namespace boost::posix_time;
|
|---|
| 12 | using namespace boost::system;
|
|---|
| 13 | using namespace std;
|
|---|
| 14 |
|
|---|
| 15 | namespace {
|
|---|
| 16 |
|
|---|
| 17 | const char * const CONNECTION_UNAVAILABLE("error.connection-unavailable");
|
|---|
| 18 | const char * const RESOLVE_ERROR("error.resolve");
|
|---|
| 19 | struct host_name;
|
|---|
| 20 | struct port_value;
|
|---|
| 21 |
|
|---|
| 22 | ip::tcp::endpoint createEndpoint(const string &hostName, unsigned int port) {
|
|---|
| 23 | io_service ioService;
|
|---|
| 24 | ip::tcp::resolver resolver(ioService);
|
|---|
| 25 | ip::tcp::resolver::query query(hostName, lexical_cast<string>(port), ip::tcp::resolver::query::numeric_service);
|
|---|
| 26 | try {
|
|---|
| 27 | ip::tcp::resolver::iterator resolve(resolver.resolve(query));
|
|---|
| 28 | if (resolve != ip::tcp::resolver::iterator()) {
|
|---|
| 29 | return *resolve;
|
|---|
| 30 | }
|
|---|
| 31 | } catch (...) {
|
|---|
| 32 | error_info<host_name, string> nameInfo(hostName);
|
|---|
| 33 | error_info<port_value, unsigned int> portInfo(port);
|
|---|
| 34 | exception_ptr nested(current_exception());
|
|---|
| 35 | runtime_error error(RESOLVE_ERROR);
|
|---|
| 36 | BOOST_THROW_EXCEPTION(enable_error_info(error) << nameInfo << portInfo << errinfo_nested_exception(nested));
|
|---|
| 37 | }
|
|---|
| 38 | error_info<host_name, string> nameInfo(hostName);
|
|---|
| 39 | error_info<port_value, unsigned int> portInfo(port);
|
|---|
| 40 | BOOST_THROW_EXCEPTION(enable_error_info(runtime_error(CONNECTION_UNAVAILABLE)) << nameInfo << portInfo);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void onSuccess(const error_code &error, size_t bytes_transferred) {
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | class ServerReceive {
|
|---|
| 47 | private:
|
|---|
| 48 | volatile bool ready;
|
|---|
| 49 | string result;
|
|---|
| 50 | public:
|
|---|
| 51 | ServerReceive() :
|
|---|
| 52 | ready(false) {
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | ~ServerReceive() {
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void operator()() {
|
|---|
| 59 | io_service io;
|
|---|
| 60 | try {
|
|---|
| 61 | ip::tcp::socket socket(io);
|
|---|
| 62 | ip::tcp::acceptor acceptor(io, ip::tcp::endpoint(ip::tcp::v4(), 32768));
|
|---|
| 63 | acceptor.listen();
|
|---|
| 64 | ready = true;
|
|---|
| 65 | acceptor.accept(socket);
|
|---|
| 66 | char data[] = { '\0', '\0', '\0', '\0', '\0' };
|
|---|
| 67 | async_read(socket, buffer(data, 4), &onSuccess);
|
|---|
| 68 | // Illegal instruction
|
|---|
| 69 | io.run();
|
|---|
| 70 | result = data;
|
|---|
| 71 | } catch (const std::exception &ex) {
|
|---|
| 72 | result = ex.what();
|
|---|
| 73 | ready = true;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | const string &getResult() const {
|
|---|
| 78 | return result;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | volatile bool isReady() const {
|
|---|
| 82 | return ready;
|
|---|
| 83 | }
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | int main(int argc, char *argv[]) {
|
|---|
| 89 | ServerReceive serverReceive;
|
|---|
| 90 | thread server(ref(serverReceive));
|
|---|
| 91 | while (!serverReceive.isReady()) {
|
|---|
| 92 | this_thread::sleep(milliseconds(100));
|
|---|
| 93 | }
|
|---|
| 94 | io_service io;
|
|---|
| 95 | ip::tcp::socket socket(io);
|
|---|
| 96 | socket.connect(createEndpoint("127.0.0.1", 32768));
|
|---|
| 97 | asio::write(socket, buffer("asdf", 4));
|
|---|
| 98 | server.join();
|
|---|
| 99 | cout << "Result: " << serverReceive.getResult() << endl;
|
|---|
| 100 | }
|
|---|