asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design
Examples

http/server/win_main.cpp

Go to the documentation of this file.
00001 //
00002 // win_main.cpp
00003 // ~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #include <iostream>
00012 #include <string>
00013 #include <asio.hpp>
00014 #include <boost/bind.hpp>
00015 #include <boost/function.hpp>
00016 #include "server.hpp"
00017 
00018 #if defined(_WIN32)
00019 
00020 boost::function0<void> console_ctrl_function;
00021 
00022 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
00023 {
00024   switch (ctrl_type)
00025   {
00026   case CTRL_C_EVENT:
00027   case CTRL_BREAK_EVENT:
00028   case CTRL_CLOSE_EVENT:
00029   case CTRL_SHUTDOWN_EVENT:
00030     console_ctrl_function();
00031     return TRUE;
00032   default:
00033     return FALSE;
00034   }
00035 }
00036 
00037 int main(int argc, char* argv[])
00038 {
00039   try
00040   {
00041     // Check command line arguments.
00042     if (argc != 4)
00043     {
00044       std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
00045       std::cerr << "  For IPv4, try:\n";
00046       std::cerr << "    http_server 0.0.0.0 80 .\n";
00047       std::cerr << "  For IPv6, try:\n";
00048       std::cerr << "    http_server 0::0 80 .\n";
00049       return 1;
00050     }
00051 
00052     // Initialise server.
00053     http::server::server s(argv[1], argv[2], argv[3]);
00054 
00055     // Set console control handler to allow server to be stopped.
00056     console_ctrl_function = boost::bind(&http::server::server::stop, &s);
00057     SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
00058 
00059     // Run the server until stopped.
00060     s.run();
00061   }
00062   catch (std::exception& e)
00063   {
00064     std::cerr << "exception: " << e.what() << "\n";
00065   }
00066 
00067   return 0;
00068 }
00069 
00070 #endif // defined(_WIN32)
asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design