protocols.qbk 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. [section:protocols TCP, UDP and ICMP]
  8. Boost.Asio provides off-the-shelf support for the internet protocols TCP, UDP and
  9. ICMP.
  10. [heading TCP Clients]
  11. Hostname resolution is performed using a resolver, where host and service names
  12. are looked up and converted into one or more endpoints:
  13. ip::tcp::resolver resolver(my_io_context);
  14. ip::tcp::resolver::query query("www.boost.org", "http");
  15. ip::tcp::resolver::iterator iter = resolver.resolve(query);
  16. ip::tcp::resolver::iterator end; // End marker.
  17. while (iter != end)
  18. {
  19. ip::tcp::endpoint endpoint = *iter++;
  20. std::cout << endpoint << std::endl;
  21. }
  22. The list of endpoints obtained above could contain both IPv4 and IPv6 endpoints,
  23. so a program should try each of them until it finds one that works. This keeps the
  24. client program independent of a specific IP version.
  25. To simplify the development of protocol-independent programs, TCP clients may
  26. establish connections using the free functions [link boost_asio.reference.connect
  27. connect()] and [link boost_asio.reference.async_connect async_connect()]. These
  28. operations try each endpoint in a list until the socket is successfully
  29. connected. For example, a single call:
  30. ip::tcp::socket socket(my_io_context);
  31. boost::asio::connect(socket, resolver.resolve(query));
  32. will synchronously try all endpoints until one is successfully connected.
  33. Similarly, an asynchronous connect may be performed by writing:
  34. boost::asio::async_connect(socket_, iter,
  35. boost::bind(&client::handle_connect, this,
  36. boost::asio::placeholders::error));
  37. // ...
  38. void handle_connect(const error_code& error)
  39. {
  40. if (!error)
  41. {
  42. // Start read or write operations.
  43. }
  44. else
  45. {
  46. // Handle error.
  47. }
  48. }
  49. When a specific endpoint is available, a socket can be created and connected:
  50. ip::tcp::socket socket(my_io_context);
  51. socket.connect(endpoint);
  52. Data may be read from or written to a connected TCP socket using the [link
  53. boost_asio.reference.basic_stream_socket.receive receive()], [link
  54. boost_asio.reference.basic_stream_socket.async_receive async_receive()], [link
  55. boost_asio.reference.basic_stream_socket.send send()] or [link
  56. boost_asio.reference.basic_stream_socket.async_send async_send()] member functions.
  57. However, as these could result in [link boost_asio.overview.core.streams short writes
  58. or reads], an application will typically use the following operations instead:
  59. [link boost_asio.reference.read read()], [link boost_asio.reference.async_read
  60. async_read()], [link boost_asio.reference.write write()] and [link
  61. boost_asio.reference.async_write async_write()].
  62. [heading TCP Servers]
  63. A program uses an acceptor to accept incoming TCP connections:
  64. ip::tcp::acceptor acceptor(my_io_context, my_endpoint);
  65. ...
  66. ip::tcp::socket socket(my_io_context);
  67. acceptor.accept(socket);
  68. After a socket has been successfully accepted, it may be read from or written
  69. to as illustrated for TCP clients above.
  70. [heading UDP]
  71. UDP hostname resolution is also performed using a resolver:
  72. ip::udp::resolver resolver(my_io_context);
  73. ip::udp::resolver::query query("localhost", "daytime");
  74. ip::udp::resolver::iterator iter = resolver.resolve(query);
  75. ...
  76. A UDP socket is typically bound to a local endpoint. The following code will
  77. create an IP version 4 UDP socket and bind it to the "any" address on port
  78. `12345`:
  79. ip::udp::endpoint endpoint(ip::udp::v4(), 12345);
  80. ip::udp::socket socket(my_io_context, endpoint);
  81. Data may be read from or written to an unconnected UDP socket using the [link
  82. boost_asio.reference.basic_datagram_socket.receive_from receive_from()], [link
  83. boost_asio.reference.basic_datagram_socket.async_receive_from async_receive_from()],
  84. [link boost_asio.reference.basic_datagram_socket.send_to send_to()] or [link
  85. boost_asio.reference.basic_datagram_socket.async_send_to async_send_to()] member
  86. functions. For a connected UDP socket, use the [link
  87. boost_asio.reference.basic_datagram_socket.receive receive()], [link
  88. boost_asio.reference.basic_datagram_socket.async_receive async_receive()], [link
  89. boost_asio.reference.basic_datagram_socket.send send()] or [link
  90. boost_asio.reference.basic_datagram_socket.async_send async_send()] member functions.
  91. [heading ICMP]
  92. As with TCP and UDP, ICMP hostname resolution is performed using a resolver:
  93. ip::icmp::resolver resolver(my_io_context);
  94. ip::icmp::resolver::query query("localhost", "");
  95. ip::icmp::resolver::iterator iter = resolver.resolve(query);
  96. ...
  97. An ICMP socket may be bound to a local endpoint. The following code will create
  98. an IP version 6 ICMP socket and bind it to the "any" address:
  99. ip::icmp::endpoint endpoint(ip::icmp::v6(), 0);
  100. ip::icmp::socket socket(my_io_context, endpoint);
  101. The port number is not used for ICMP.
  102. Data may be read from or written to an unconnected ICMP socket using the [link
  103. boost_asio.reference.basic_raw_socket.receive_from receive_from()], [link
  104. boost_asio.reference.basic_raw_socket.async_receive_from async_receive_from()],
  105. [link boost_asio.reference.basic_raw_socket.send_to send_to()] or [link
  106. boost_asio.reference.basic_raw_socket.async_send_to async_send_to()] member
  107. functions.
  108. [heading See Also]
  109. [link boost_asio.reference.ip__tcp ip::tcp],
  110. [link boost_asio.reference.ip__udp ip::udp],
  111. [link boost_asio.reference.ip__icmp ip::icmp],
  112. [link boost_asio.tutorial.tutdaytime1 daytime protocol tutorials],
  113. [link boost_asio.examples.cpp03_examples.icmp ICMP ping example].
  114. [endsect]