firstaccount firstaccount - 4 months ago 35
C++ Question

Cannot listen on specific port num with localhost address with http_listener(C++ REST SDK)

I use C++ REST SDK to build up a HTTP server which is used for receiving requests from postman, but if I code like this:

http_listener listener(L"http://localhost/io");
listener.open().wait();

listener.support(methods::POST, [](http_request req) {
});


Postman can connect to it with
http://localhost/io
in POST method,
But if I code like :

http_listener listener(L"http://localhost:6000/io");


Postman cannot connect to it with
http://localhost:6000/io
and POST method.
But if I code like
http_listener listener(L"http://localhost/io:6000");

Postman can connect with it in
http://localhost/io:6000
with POST method. How could I make
http://localhost:6000/io
works for my listener ? Another program I work with always send http request to
http://localhost:6000/io
, so I need to let my server listen on this address.

http_listener listener(L"http://localhost:6000"); //doesn't work, too.


But when I change 6000 to any other port num,like 7000 or 8000, like
http_listener listener(L"http://localhost:7000")
or
http_listener listener(L"http://localhost:7000/io")
it works for me. I use
netstat -a -b
to check whether 6000 has been occupied by another program, but 6000 is free.

Answer

I think I have the answer. I went through this same problem and I used a différent unused port and a different use of "support" function :

http_listener listener(L"http://localhost:11369/");
listener->open().wait();
listener->configuration() ;

listener->support(methods::GET,
        std::tr1::bind(&CMFCApplication1Dlg::handle_get,
            this,
            std::tr1::placeholders::_1));
listener->support(methods::PUT,
        std::tr1::bind(&CMFCApplication1Dlg::handle_put,
            this,
            std::tr1::placeholders::_1));
listener->support(methods::POST,
        std::tr1::bind(&CMFCApplication1Dlg::handle_post,
            this,
            std::tr1::placeholders::_1));
listener->support(methods::DEL,
        std::tr1::bind(&CMFCApplication1Dlg::handle_delete,
            this,
            std::tr1::placeholders::_1));

I use http_listener in a C++ MFC app. So it will be surely different for you. If you need any help don't hesitate! Sincerely, Ahmed.