http_listener behaviour misunderstanding

Jul 20, 2014 at 6:28 PM
Hi all.

Consider this code:
int main(int argc, char **argv) {
    try {
        web::http::experimental::listener::http_listener listener(U("http://localhost:4444/"));
        listener.open().wait();
    } catch (std::exception &e) {
        std::cout << "caught error: " << e.what() << std::endl;
    }
    return 0;
}
I supposed the listener should block the program execution and listen until requests but the program is always emmediately exits. How can i achieve the server like behaviour (forever listen) ? Or may be i missing something?
Jul 21, 2014 at 9:35 AM
Yes, you are missing something. Specify a lamda that does something with each request. Insert before listener.open().
    listener.support(([&](web::http::http_request request){ do something with this request }));
Jul 21, 2014 at 5:23 PM
The reason the listener isn't waiting for any requests is because your program, immediately after opening the listener, is exiting the process. The task returned from open is signaled once the listener has started to accept requests. You need to do something in your program to decide when you actually want to exit. For example you could read something in from the standard input stream using std::cin. Then later on you could call listener.close().wait().

Steve
Jul 21, 2014 at 8:10 PM
Thank you for reply, now i understand.