| 257 | //------------------------------------------------------------------------------ |
| 258 | |
| 259 | int main(int argc, char* argv[]) |
| 260 | { |
| 261 | try |
| 262 | { |
| 263 | // Check command line arguments. |
| 264 | if (argc != 4) |
| 265 | { |
| 266 | std::cerr << |
| 267 | "Usage: http-server-sync <address> <port> <doc_root>\n" << |
| 268 | "Example:\n" << |
| 269 | " http-server-sync 0.0.0.0 8080 .\n"; |
| 270 | return EXIT_FAILURE; |
| 271 | } |
| 272 | auto const address = net::ip::make_address(argv[1]); |
| 273 | auto const port = static_cast<unsigned short>(std::atoi(argv[2])); |
| 274 | auto const doc_root = std::make_shared<std::string>(argv[3]); |
| 275 | |
| 276 | // The io_context is required for all I/O |
| 277 | net::io_context ioc{1}; |
| 278 | |
| 279 | // The acceptor receives incoming connections |
| 280 | tcp::acceptor acceptor{ioc, {address, port}}; |
| 281 | for(;;) |
| 282 | { |
| 283 | // This will receive the new connection |
| 284 | tcp::socket socket{ioc}; |
| 285 | |
| 286 | // Block until we get a connection |
| 287 | acceptor.accept(socket); |
| 288 | |
| 289 | // Launch the session, transferring ownership of the socket |
| 290 | std::thread{std::bind( |
| 291 | &do_session, |
| 292 | std::move(socket), |
| 293 | doc_root)}.detach(); |
| 294 | } |
| 295 | } |
| 296 | catch (const std::exception& e) |
| 297 | { |
| 298 | std::cerr << "Error: " << e.what() << std::endl; |
| 299 | return EXIT_FAILURE; |
| 300 | } |
| 301 | } |