@@ -19,7 +19,6 @@ simple response to any HTTP request.
1919.. code-block:: c++
2020
2121 #include <boost/network/protocol/http/server.hpp>
22- #include <string>
2322 #include <iostream>
2423
2524 namespace http = boost::network::http;
@@ -28,16 +27,19 @@ simple response to any HTTP request.
2827 typedef http::server<hello_world> server;
2928
3029 struct hello_world {
31- void operator() (server::request const &request,
32- server::response &response) {
33- std::string ip = source(request);
34- response = server::response::stock_reply(
35- server::response::ok, std::string("Hello, ") + ip + "!");
30+ void operator()(server::request const &request, server::response &response) {
31+ server::string_type ip = source(request);
32+ unsigned int port = request.source_port;
33+ std::ostringstream data;
34+ data << "Hello, " << ip << ':' << port << '!';
35+ response = server::response::stock_reply(server::response::ok, data.str());
36+ }
37+ void log(const server::string_type& message) {
38+ std::cerr << "ERROR: " << message << std::endl;
3639 }
3740 };
3841
39- int
40- main(int argc, char * argv[]) {
42+ int main(int argc, char *argv[]) {
4143
4244 if (argc != 3) {
4345 std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
@@ -46,7 +48,8 @@ simple response to any HTTP request.
4648
4749 try {
4850 hello_world handler;
49- server server_(argv[1], argv[2], handler);
51+ server::options options(handler);
52+ server server_(options.address(argv[1]).port(argv[2]));
5053 server_.run();
5154 }
5255 catch (std::exception &e) {
@@ -100,34 +103,39 @@ This header contains all the code needed to develop an HTTP server with
100103 typedef http::server<hello_world> server;
101104
102105 struct hello_world {
103- void operator () (server::request const &request,
104- server::response &response) {
105- std::string ip = source(request);
106- response = server::response::stock_reply(
107- server::response::ok, std::string("Hello, ") + ip + "!");
106+ void operator()(server::request const &request, server::response &response) {
107+ server::string_type ip = source(request);
108+ unsigned int port = request.source_port;
109+ std::ostringstream data;
110+ data << "Hello, " << ip << ':' << port << '!';
111+ response = server::response::stock_reply(server::response::ok, data.str());
112+ }
113+ void log(const server::string_type& message) {
114+ std::cerr << "ERROR: " << message << std::endl;
108115 }
109116 };
110117
111- ``hello_world`` is a functor class which handles HTTP requests. All
112- the operator does here is return an HTTP response with HTTP code 200
113- and the body ``"Hello, <ip>!"``. The ``<ip>`` in this case would be
114- the IP address of the client that made the request.
118+ ``hello_world`` is a functor class which handles HTTP requests.
119+ All the operator does here is return an HTTP response with HTTP code 200
120+ and the body ``"Hello, <ip>:<port> !"``. The ``<ip>`` in this case would be
121+ the IP address of the client that made the request and ``<port>`` the clients port .
115122
116123There are a number of pre-defined stock replies differentiated by
117124status code with configurable bodies.
118-
119125All the supported enumeration values for the response status codes can be found
120126in ``boost/network/protocol/http/impl/response.ipp``.
121127
122128.. code-block:: c++
123129
124130 hello_world handler;
125- server server_(argv[1], argv[2], handler);
131+ server::options options(handler);
132+ server server_(options.address(argv[1]).port(argv[2]));
126133 server_.run();
127134
128- The first two arguments to the ``server`` constructor are the host and
129- the port on which the server will listen. The third argument is the
130- the handler object defined previously.
135+ The ``server`` constructor requires an object of the ``options`` class,
136+ this object stores all needed options, especially the host and
137+ the port on which the server will listen.
138+ The ``options`` constructor's single argument is the handler object defined previously.
131139
132140.. note:: In this example, the server is specifically made to be single-threaded.
133141 In a multi-threaded server, you would invoke the ``hello_world::run`` member