daytime_dox.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. //
  2. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. /**
  8. \page tutdaytime1 Daytime.1 - A synchronous TCP daytime client
  9. This tutorial program shows how to use asio to implement a client application
  10. with TCP.
  11. \dontinclude daytime1/client.cpp
  12. \skip #include
  13. We start by including the necessary header files.
  14. \until asio.hpp
  15. The purpose of this application is to access a daytime service,
  16. so we need the user to specify the server.
  17. \until }
  18. All programs that use asio need to have at least one boost::asio::io_context
  19. object.
  20. \until boost::asio::io_context
  21. We need to turn the server name that was specified as a parameter to the
  22. application, into a TCP endpoint. To do this we use an
  23. boost::asio::ip::tcp::resolver object.
  24. \until tcp::resolver
  25. A resolver takes a query object and turns it into a list of endpoints. We
  26. construct a query using the name of the server, specified in <tt>argv[1]</tt>,
  27. and the name of the service, in this case <tt>"daytime"</tt>.
  28. \until tcp::resolver::query
  29. The list of endpoints is returned using an iterator of type
  30. boost::asio::ip::tcp::resolver::iterator. (Note that a default constructed
  31. boost::asio::ip::tcp::resolver::iterator object can be used as an end iterator.)
  32. \until tcp::resolver::iterator
  33. Now we create and connect the socket. The list of endpoints obtained above may
  34. contain both IPv4 and IPv6 endpoints, so we need to try each of them until we
  35. find one that works. This keeps the client program independent of a specific IP
  36. version. The boost::asio::connect() function does this for us automatically.
  37. \until boost::asio::connect
  38. The connection is open. All we need to do now is read the response from the
  39. daytime service.
  40. We use a <tt>boost::array</tt> to hold the received data. The boost::asio::buffer()
  41. function automatically determines the size of the array to help prevent buffer
  42. overruns. Instead of a <tt>boost::array</tt>, we could have used a <tt>char
  43. []</tt> or <tt>std::vector</tt>.
  44. \until read_some
  45. When the server closes the connection, the boost::asio::ip::tcp::socket::read_some()
  46. function will exit with the boost::asio::error::eof error, which is how we know to
  47. exit the loop.
  48. \until }
  49. Finally, handle any exceptions that may have been thrown.
  50. \until }
  51. \until }
  52. See the \ref tutdaytime1src "full source listing" \n
  53. Return to the \ref index "tutorial index" \n
  54. Next: \ref tutdaytime2
  55. */
  56. /**
  57. \page tutdaytime1src Source listing for Daytime.1
  58. \include daytime1/client.cpp
  59. Return to \ref tutdaytime1
  60. */
  61. /**
  62. \page tutdaytime2 Daytime.2 - A synchronous TCP daytime server
  63. This tutorial program shows how to use asio to implement a server application
  64. with TCP.
  65. \dontinclude daytime2/server.cpp
  66. \skip #include
  67. \until using
  68. We define the function <tt>make_daytime_string()</tt> to create the string to
  69. be sent back to the client. This function will be reused in all of our daytime
  70. server applications.
  71. \until boost::asio::io_context
  72. A boost::asio::ip::tcp::acceptor object needs to be created to listen
  73. for new connections. It is initialised to listen on TCP port 13, for IP version 4.
  74. \until tcp::acceptor
  75. This is an iterative server, which means that it will handle one
  76. connection at a time. Create a socket that will represent the connection to the
  77. client, and then wait for a connection.
  78. \until acceptor.accept
  79. A client is accessing our service. Determine the current time
  80. and transfer this information to the client.
  81. \until }
  82. \until }
  83. Finally, handle any exceptions.
  84. \until }
  85. \until }
  86. See the \ref tutdaytime2src "full source listing" \n
  87. Return to the \ref index "tutorial index" \n
  88. Previous: \ref tutdaytime1 \n
  89. Next: \ref tutdaytime3
  90. */
  91. /**
  92. \page tutdaytime2src Source listing for Daytime.2
  93. \include daytime2/server.cpp
  94. Return to \ref tutdaytime2
  95. */
  96. /**
  97. \page tutdaytime3 Daytime.3 - An asynchronous TCP daytime server
  98. \section tutdaytime3funcmain The main() function
  99. \dontinclude daytime3/server.cpp
  100. \skip int main()
  101. \until try
  102. \until {
  103. We need to create a server object to accept incoming client connections. The
  104. boost::asio::io_context object provides I/O services, such as sockets, that the
  105. server object will use.
  106. \until tcp_server
  107. Run the boost::asio::io_context object so that it will perform asynchronous operations
  108. on your behalf.
  109. \until return 0;
  110. \until }
  111. \section tutdaytime3classtcp_server The tcp_server class
  112. \dontinclude daytime3/server.cpp
  113. \skip class tcp_server
  114. \until public:
  115. The constructor initialises an acceptor to listen on TCP port 13.
  116. \until private:
  117. The function <tt>start_accept()</tt> creates a socket and initiates an
  118. asynchronous accept operation to wait for a new connection.
  119. \until }
  120. The function <tt>handle_accept()</tt> is called when the asynchronous accept
  121. operation initiated by <tt>start_accept()</tt> finishes. It services the client
  122. request, and then calls <tt>start_accept()</tt> to initiate the next accept
  123. operation.
  124. \until }
  125. \until }
  126. \section tutdaytime3classtcp_connection The tcp_connection class
  127. We will use <tt>shared_ptr</tt> and <tt>enable_shared_from_this</tt> because we
  128. want to keep the <tt>tcp_connection</tt> object alive as long as there is an
  129. operation that refers to it.
  130. \dontinclude daytime3/server.cpp
  131. \skip class tcp_connection
  132. \until shared_ptr
  133. \until }
  134. \until }
  135. In the function <tt>start()</tt>, we call boost::asio::async_write() to serve the data
  136. to the client. Note that we are using boost::asio::async_write(), rather than
  137. boost::asio::ip::tcp::socket::async_write_some(), to ensure that the entire block of
  138. data is sent.
  139. \until {
  140. The data to be sent is stored in the class member <tt>message_</tt> as we need
  141. to keep the data valid until the asynchronous operation is complete.
  142. \until message_
  143. When initiating the asynchronous operation, and if using boost::bind(), you
  144. must specify only the arguments that match the handler's parameter list. In
  145. this program, both of the argument placeholders (boost::asio::placeholders::error and
  146. boost::asio::placeholders::bytes_transferred) could potentially have been removed,
  147. since they are not being used in <tt>handle_write()</tt>.
  148. \until placeholders::bytes_transferred
  149. Any further actions for this client connection are now the responsibility of
  150. <tt>handle_write()</tt>.
  151. \until };
  152. \section tutdaytime3remunused Removing unused handler parameters
  153. You may have noticed that the <tt>error</tt>, and <tt>bytes_transferred</tt>
  154. parameters are not used in the body of the <tt>handle_write()</tt> function. If
  155. parameters are not needed, it is possible to remove them from the function so
  156. that it looks like:
  157. \code
  158. void handle_write()
  159. {
  160. }
  161. \endcode
  162. The boost::asio::async_write() call used to initiate the call can then be changed to
  163. just:
  164. \code
  165. boost::asio::async_write(socket_, boost::asio::buffer(message_),
  166. boost::bind(&tcp_connection::handle_write, shared_from_this()));
  167. \endcode
  168. See the \ref tutdaytime3src "full source listing" \n
  169. Return to the \ref index "tutorial index" \n
  170. Previous: \ref tutdaytime2 \n
  171. Next: \ref tutdaytime4
  172. */
  173. /**
  174. \page tutdaytime3src Source listing for Daytime.3
  175. \include daytime3/server.cpp
  176. Return to \ref tutdaytime3
  177. */
  178. /**
  179. \page tutdaytime4 Daytime.4 - A synchronous UDP daytime client
  180. This tutorial program shows how to use asio to implement a client application
  181. with UDP.
  182. \dontinclude daytime4/client.cpp
  183. \skip #include
  184. \until using boost::asio::ip::udp;
  185. The start of the application is essentially the same as for the TCP daytime
  186. client.
  187. \until boost::asio::io_context
  188. We use an boost::asio::ip::udp::resolver object to find the correct remote endpoint to
  189. use based on the host and service names. The query is restricted to return only
  190. IPv4 endpoints by the boost::asio::ip::udp::v4() argument.
  191. \until udp::v4
  192. The boost::asio::ip::udp::resolver::resolve() function is guaranteed to return at
  193. least one endpoint in the list if it does not fail. This means it is safe to
  194. dereference the return value directly.
  195. \until udp::endpoint
  196. Since UDP is datagram-oriented, we will not be using a stream socket. Create an
  197. boost::asio::ip::udp::socket and initiate contact with the remote endpoint.
  198. \until receiver_endpoint
  199. Now we need to be ready to accept whatever the server sends back to us. The
  200. endpoint on our side that receives the server's response will be initialised by
  201. boost::asio::ip::udp::socket::receive_from().
  202. \until }
  203. Finally, handle any exceptions that may have been thrown.
  204. \until }
  205. \until }
  206. See the \ref tutdaytime4src "full source listing" \n
  207. Return to the \ref index "tutorial index" \n
  208. Previous: \ref tutdaytime3 \n
  209. Next: \ref tutdaytime5
  210. */
  211. /**
  212. \page tutdaytime4src Source listing for Daytime.4
  213. \include daytime4/client.cpp
  214. Return to \ref tutdaytime4
  215. */
  216. /**
  217. \page tutdaytime5 Daytime.5 - A synchronous UDP daytime server
  218. This tutorial program shows how to use asio to implement a server application
  219. with UDP.
  220. \dontinclude daytime5/server.cpp
  221. \skip int main()
  222. \until boost::asio::io_context
  223. Create an boost::asio::ip::udp::socket object to receive requests on UDP port 13.
  224. \until udp::socket
  225. Wait for a client to initiate contact with us. The remote_endpoint object will
  226. be populated by boost::asio::ip::udp::socket::receive_from().
  227. \until receive_from
  228. Determine what we are going to send back to the client.
  229. \until std::string message
  230. Send the response to the remote_endpoint.
  231. \until }
  232. \until }
  233. Finally, handle any exceptions.
  234. \until }
  235. \until }
  236. See the \ref tutdaytime5src "full source listing" \n
  237. Return to the \ref index "tutorial index" \n
  238. Previous: \ref tutdaytime4 \n
  239. Next: \ref tutdaytime6
  240. */
  241. /**
  242. \page tutdaytime5src Source listing for Daytime.5
  243. \include daytime5/server.cpp
  244. Return to \ref tutdaytime5
  245. */
  246. /**
  247. \page tutdaytime6 Daytime.6 - An asynchronous UDP daytime server
  248. \section tutdaytime6funcmain The main() function
  249. \dontinclude daytime6/server.cpp
  250. \skip int main()
  251. \until try
  252. \until {
  253. Create a server object to accept incoming client requests, and run
  254. the boost::asio::io_context object.
  255. \until return 0;
  256. \until }
  257. \section tutdaytime6classudp_server The udp_server class
  258. \dontinclude daytime6/server.cpp
  259. \skip class udp_server
  260. \until public:
  261. The constructor initialises a socket to listen on UDP port 13.
  262. \until private:
  263. \until {
  264. The function boost::asio::ip::udp::socket::async_receive_from() will cause the
  265. application to listen in the background for a new request. When such a request
  266. is received, the boost::asio::io_context object will invoke the
  267. <tt>handle_receive()</tt> function with two arguments: a value of type
  268. boost::system::error_code indicating whether the operation succeeded or failed, and a
  269. <tt>size_t</tt> value <tt>bytes_transferred</tt> specifying the number of bytes
  270. received.
  271. \until }
  272. The function <tt>handle_receive()</tt> will service the client request.
  273. \until {
  274. The <tt>error</tt> parameter contains the result of the asynchronous operation.
  275. Since we only provide the 1-byte <tt>recv_buffer_</tt> to contain the client's
  276. request, the boost::asio::io_context object would return an error if the client sent
  277. anything larger. We can ignore such an error if it comes up.
  278. \until {
  279. Determine what we are going to send.
  280. \until make_daytime_string()
  281. We now call boost::asio::ip::udp::socket::async_send_to() to serve the data to the
  282. client.
  283. \until boost::asio::placeholders::bytes_transferred
  284. When initiating the asynchronous operation, and if using boost::bind(), you
  285. must specify only the arguments that match the handler's parameter list. In
  286. this program, both of the argument placeholders (boost::asio::placeholders::error and
  287. boost::asio::placeholders::bytes_transferred) could potentially have been removed.
  288. Start listening for the next client request.
  289. \until start_receive
  290. Any further actions for this client request are now the responsibility of
  291. <tt>handle_send()</tt>.
  292. \until }
  293. \until }
  294. The function <tt>handle_send()</tt> is invoked after the service request has
  295. been completed.
  296. \until }
  297. \until }
  298. See the \ref tutdaytime6src "full source listing" \n
  299. Return to the \ref index "tutorial index" \n
  300. Previous: \ref tutdaytime5 \n
  301. Next: \ref tutdaytime7
  302. */
  303. /**
  304. \page tutdaytime6src Source listing for Daytime.6
  305. \include daytime6/server.cpp
  306. Return to \ref tutdaytime6
  307. */
  308. /**
  309. \page tutdaytime7 Daytime.7 - A combined TCP/UDP asynchronous server
  310. This tutorial program shows how to combine the two asynchronous servers that we
  311. have just written, into a single server application.
  312. \section tutdaytime7funcmain The main() function
  313. \dontinclude daytime7/server.cpp
  314. \skip int main()
  315. \until boost::asio::io_context
  316. We will begin by creating a server object to accept a TCP client connection.
  317. \until tcp_server
  318. We also need a server object to accept a UDP client request.
  319. \until udp_server
  320. We have created two lots of work for the boost::asio::io_context object to do.
  321. \until return 0;
  322. \until }
  323. \section tutdaytime7classtcp The tcp_connection and tcp_server classes
  324. The following two classes are taken from \ref tutdaytime3 "Daytime.3".
  325. \dontinclude daytime7/server.cpp
  326. \skip class tcp_connection
  327. \until };
  328. \until };
  329. \section tutdaytime7classudp The udp_server class
  330. Similarly, this next class is taken from the
  331. \ref tutdaytime6 "previous tutorial step".
  332. \dontinclude daytime7/server.cpp
  333. \skip class udp_server
  334. \until };
  335. See the \ref tutdaytime7src "full source listing" \n
  336. Return to the \ref index "tutorial index" \n
  337. Previous: \ref tutdaytime6
  338. */
  339. /**
  340. \page tutdaytime7src Source listing for Daytime.7
  341. \include daytime7/server.cpp
  342. Return to \ref tutdaytime7
  343. */