| src/examples/cpp03/echo/blocking_tcp_echo_server.cpp | src/examples/cpp11/echo/blocking_tcp_echo_server.cpp | 
|---|
| ⋮ | ⋮ | 
| 1 | // | 1 | // | 
| 2 | //·blocking_tcp_echo_server.cpp | 2 | //·blocking_tcp_echo_server.cpp | 
| 3 | //·~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 3 | //·~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
| 4 | // | 4 | // | 
| 5 | //·Copyright·(c)·2003-2015·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) | 5 | //·Copyright·(c)·2003-2015·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) | 
| 6 | // | 6 | // | 
| 7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying | 7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying | 
| 8 | //·file·LICENSE_1_0.txt·or·copy·at·http://www.boost.org/LICENSE_1_0.txt) | 8 | //·file·LICENSE_1_0.txt·or·copy·at·http://www.boost.org/LICENSE_1_0.txt) | 
| 9 | // | 9 | // | 
| 10 |  | 10 |  | 
| 11 | #include·<cstdlib> | 11 | #include·<cstdlib> | 
| 12 | #include·<iostream> | 12 | #include·<iostream> | 
| 13 | #include·<boost/bind.hpp> | 13 | #include·<thread> | 
| 14 | #include·<boost/smart_ptr.hpp> | 14 | #include·<utility> | 
| 15 | #include·"asio.hpp" | 15 | #include·"asio.hpp" | 
| 16 |  | 16 |  | 
| 17 | using·asio::ip::tcp; | 17 | using·asio::ip::tcp; | 
| 18 |  | 18 |  | 
| 19 | const·int·max_length·=·1024; | 19 | const·int·max_length·=·1024; | 
| 20 |  | 20 |  | 
| 21 | typedef·boost::shared_ptr<tcp::socket>·socket_ptr; | 21 | void·session(tcp::socket·sock) | 
| 22 |  |  | 
| 23 | void·session(socket_ptr·sock) |  | 
| 24 | { | 22 | { | 
| 25 | ··try | 23 | ··try | 
| 26 | ··{ | 24 | ··{ | 
| 27 | ····for·(;;) | 25 | ····for·(;;) | 
| 28 | ····{ | 26 | ····{ | 
| 29 | ······char·data[max_length]; | 27 | ······char·data[max_length]; | 
| 30 |  | 28 |  | 
| 31 | ······asio::error_code·error; | 29 | ······asio::error_code·error; | 
| 32 | ······size_t·length·=·sock->read_some(asio::buffer(data),·error); | 30 | ······size_t·length·=·sock.read_some(asio::buffer(data),·error); | 
| 33 | ······if·(error·==·asio::error::eof) | 31 | ······if·(error·==·asio::error::eof) | 
| 34 | ········break;·//·Connection·closed·cleanly·by·peer. | 32 | ········break;·//·Connection·closed·cleanly·by·peer. | 
| 35 | ······else·if·(error) | 33 | ······else·if·(error) | 
| 36 | ········throw·asio::system_error(error);·//·Some·other·error. | 34 | ········throw·asio::system_error(error);·//·Some·other·error. | 
| 37 |  | 35 |  | 
| 38 | ······asio::write(*sock,·asio::buffer(data,·length)); | 36 | ······asio::write(sock,·asio::buffer(data,·length)); | 
| 39 | ····} | 37 | ····} | 
| 40 | ··} | 38 | ··} | 
| 41 | ··catch·(std::exception&·e) | 39 | ··catch·(std::exception&·e) | 
| 42 | ··{ | 40 | ··{ | 
| 43 | ····std::cerr·<<·"Exception·in·thread:·"·<<·e.what()·<<·"\n"; | 41 | ····std::cerr·<<·"Exception·in·thread:·"·<<·e.what()·<<·"\n"; | 
| 44 | ··} | 42 | ··} | 
| 45 | } | 43 | } | 
| 46 |  | 44 |  | 
| 47 | void·server(asio::io_service&·io_service,·unsigned·short·port) | 45 | void·server(asio::io_service&·io_service,·unsigned·short·port) | 
| 48 | { | 46 | { | 
| 49 | ··tcp::acceptor·a(io_service,·tcp::endpoint(tcp::v4(),·port)); | 47 | ··tcp::acceptor·a(io_service,·tcp::endpoint(tcp::v4(),·port)); | 
| 50 | ··for·(;;) | 48 | ··for·(;;) | 
| 51 | ··{ | 49 | ··{ | 
| 52 | ····socket_ptr·sock(new·tcp::socket(io_service)); | 50 | ····tcp::socket·sock(io_service); | 
| 53 | ····a.accept(*sock); | 51 | ····a.accept(sock); | 
| 54 | ····asio::thread·t(boost::bind(session,·sock)); | 52 | ····std::thread(session,·std::move(sock)).detach(); | 
| 55 | ··} | 53 | ··} | 
| 56 | } | 54 | } | 
| 57 |  | 55 |  | 
| 58 | int·main(int·argc,·char*·argv[]) | 56 | int·main(int·argc,·char*·argv[]) | 
| 59 | { | 57 | { | 
| 60 | ··try | 58 | ··try | 
| 61 | ··{ | 59 | ··{ | 
| 62 | ····if·(argc·!=·2) | 60 | ····if·(argc·!=·2) | 
| 63 | ····{ | 61 | ····{ | 
| 64 | ······std::cerr·<<·"Usage:·blocking_tcp_echo_server·<port>\n"; | 62 | ······std::cerr·<<·"Usage:·blocking_tcp_echo_server·<port>\n"; | 
| 65 | ······return·1; | 63 | ······return·1; | 
| 66 | ····} | 64 | ····} | 
| 67 |  | 65 |  | 
| 68 | ····asio::io_service·io_service; | 66 | ····asio::io_service·io_service; | 
| 69 |  | 67 |  | 
| 70 | ····using·namespace·std;·//·For·atoi. | 68 | ····server(io_service,·std::atoi(argv[1])); | 
| 71 | ····server(io_service,·atoi(argv[1])); |  | 
| 72 | ··} | 69 | ··} | 
| 73 | ··catch·(std::exception&·e) | 70 | ··catch·(std::exception&·e) | 
| 74 | ··{ | 71 | ··{ | 
| 75 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; | 72 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; | 
| 76 | ··} | 73 | ··} | 
| 77 |  | 74 |  | 
| 78 | ··return·0; | 75 | ··return·0; | 
| 79 | } | 76 | } |