2 // basic_datagram_socket.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/basic_socket.hpp>
21 #include <boost/asio/detail/handler_type_requirements.hpp>
22 #include <boost/asio/detail/throw_error.hpp>
23 #include <boost/asio/detail/type_traits.hpp>
24 #include <boost/asio/error.hpp>
26 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
27 # include <boost/asio/datagram_socket_service.hpp>
28 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
30 #include <boost/asio/detail/push_options.hpp>
35 /// Provides datagram-oriented socket functionality.
37 * The basic_datagram_socket class template provides asynchronous and blocking
38 * datagram-oriented socket functionality.
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
44 template <typename Protocol
45 BOOST_ASIO_SVC_TPARAM_DEF1(= datagram_socket_service<Protocol>)>
46 class basic_datagram_socket
47 : public basic_socket<Protocol BOOST_ASIO_SVC_TARG>
50 /// The native representation of a socket.
51 #if defined(GENERATING_DOCUMENTATION)
52 typedef implementation_defined native_handle_type;
54 typedef typename basic_socket<
55 Protocol BOOST_ASIO_SVC_TARG>::native_handle_type native_handle_type;
58 /// The protocol type.
59 typedef Protocol protocol_type;
61 /// The endpoint type.
62 typedef typename Protocol::endpoint endpoint_type;
64 /// Construct a basic_datagram_socket without opening it.
66 * This constructor creates a datagram socket without opening it. The open()
67 * function must be called before data can be sent or received on the socket.
69 * @param io_context The io_context object that the datagram socket will use
70 * to dispatch handlers for any asynchronous operations performed on the
73 explicit basic_datagram_socket(boost::asio::io_context& io_context)
74 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context)
78 /// Construct and open a basic_datagram_socket.
80 * This constructor creates and opens a datagram socket.
82 * @param io_context The io_context object that the datagram socket will use
83 * to dispatch handlers for any asynchronous operations performed on the
86 * @param protocol An object specifying protocol parameters to be used.
88 * @throws boost::system::system_error Thrown on failure.
90 basic_datagram_socket(boost::asio::io_context& io_context,
91 const protocol_type& protocol)
92 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, protocol)
96 /// Construct a basic_datagram_socket, opening it and binding it to the given
99 * This constructor creates a datagram socket and automatically opens it bound
100 * to the specified endpoint on the local machine. The protocol used is the
101 * protocol associated with the given endpoint.
103 * @param io_context The io_context object that the datagram socket will use
104 * to dispatch handlers for any asynchronous operations performed on the
107 * @param endpoint An endpoint on the local machine to which the datagram
108 * socket will be bound.
110 * @throws boost::system::system_error Thrown on failure.
112 basic_datagram_socket(boost::asio::io_context& io_context,
113 const endpoint_type& endpoint)
114 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, endpoint)
118 /// Construct a basic_datagram_socket on an existing native socket.
120 * This constructor creates a datagram socket object to hold an existing
123 * @param io_context The io_context object that the datagram socket will use
124 * to dispatch handlers for any asynchronous operations performed on the
127 * @param protocol An object specifying protocol parameters to be used.
129 * @param native_socket The new underlying socket implementation.
131 * @throws boost::system::system_error Thrown on failure.
133 basic_datagram_socket(boost::asio::io_context& io_context,
134 const protocol_type& protocol, const native_handle_type& native_socket)
135 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(
136 io_context, protocol, native_socket)
140 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
141 /// Move-construct a basic_datagram_socket from another.
143 * This constructor moves a datagram socket from one object to another.
145 * @param other The other basic_datagram_socket object from which the move
148 * @note Following the move, the moved-from object is in the same state as if
149 * constructed using the @c basic_datagram_socket(io_context&) constructor.
151 basic_datagram_socket(basic_datagram_socket&& other)
152 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other))
156 /// Move-assign a basic_datagram_socket from another.
158 * This assignment operator moves a datagram socket from one object to
161 * @param other The other basic_datagram_socket object from which the move
164 * @note Following the move, the moved-from object is in the same state as if
165 * constructed using the @c basic_datagram_socket(io_context&) constructor.
167 basic_datagram_socket& operator=(basic_datagram_socket&& other)
169 basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other));
173 /// Move-construct a basic_datagram_socket from a socket of another protocol
176 * This constructor moves a datagram socket from one object to another.
178 * @param other The other basic_datagram_socket object from which the move
181 * @note Following the move, the moved-from object is in the same state as if
182 * constructed using the @c basic_datagram_socket(io_context&) constructor.
184 template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1>
185 basic_datagram_socket(
186 basic_datagram_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other,
187 typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
188 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other))
192 /// Move-assign a basic_datagram_socket from a socket of another protocol
195 * This assignment operator moves a datagram socket from one object to
198 * @param other The other basic_datagram_socket object from which the move
201 * @note Following the move, the moved-from object is in the same state as if
202 * constructed using the @c basic_datagram_socket(io_context&) constructor.
204 template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1>
205 typename enable_if<is_convertible<Protocol1, Protocol>::value,
206 basic_datagram_socket>::type& operator=(
207 basic_datagram_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other)
209 basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other));
212 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
214 /// Destroys the socket.
216 * This function destroys the socket, cancelling any outstanding asynchronous
217 * operations associated with the socket as if by calling @c cancel.
219 ~basic_datagram_socket()
223 /// Send some data on a connected socket.
225 * This function is used to send data on the datagram socket. The function
226 * call will block until the data has been sent successfully or an error
229 * @param buffers One ore more data buffers to be sent on the socket.
231 * @returns The number of bytes sent.
233 * @throws boost::system::system_error Thrown on failure.
235 * @note The send operation can only be used with a connected socket. Use
236 * the send_to function to send data on an unconnected datagram socket.
239 * To send a single data buffer use the @ref buffer function as follows:
240 * @code socket.send(boost::asio::buffer(data, size)); @endcode
241 * See the @ref buffer documentation for information on sending multiple
242 * buffers in one go, and how to use it with arrays, boost::array or
245 template <typename ConstBufferSequence>
246 std::size_t send(const ConstBufferSequence& buffers)
248 boost::system::error_code ec;
249 std::size_t s = this->get_service().send(
250 this->get_implementation(), buffers, 0, ec);
251 boost::asio::detail::throw_error(ec, "send");
255 /// Send some data on a connected socket.
257 * This function is used to send data on the datagram socket. The function
258 * call will block until the data has been sent successfully or an error
261 * @param buffers One ore more data buffers to be sent on the socket.
263 * @param flags Flags specifying how the send call is to be made.
265 * @returns The number of bytes sent.
267 * @throws boost::system::system_error Thrown on failure.
269 * @note The send operation can only be used with a connected socket. Use
270 * the send_to function to send data on an unconnected datagram socket.
272 template <typename ConstBufferSequence>
273 std::size_t send(const ConstBufferSequence& buffers,
274 socket_base::message_flags flags)
276 boost::system::error_code ec;
277 std::size_t s = this->get_service().send(
278 this->get_implementation(), buffers, flags, ec);
279 boost::asio::detail::throw_error(ec, "send");
283 /// Send some data on a connected socket.
285 * This function is used to send data on the datagram socket. The function
286 * call will block until the data has been sent successfully or an error
289 * @param buffers One or more data buffers to be sent on the socket.
291 * @param flags Flags specifying how the send call is to be made.
293 * @param ec Set to indicate what error occurred, if any.
295 * @returns The number of bytes sent.
297 * @note The send operation can only be used with a connected socket. Use
298 * the send_to function to send data on an unconnected datagram socket.
300 template <typename ConstBufferSequence>
301 std::size_t send(const ConstBufferSequence& buffers,
302 socket_base::message_flags flags, boost::system::error_code& ec)
304 return this->get_service().send(
305 this->get_implementation(), buffers, flags, ec);
308 /// Start an asynchronous send on a connected socket.
310 * This function is used to asynchronously send data on the datagram socket.
311 * The function call always returns immediately.
313 * @param buffers One or more data buffers to be sent on the socket. Although
314 * the buffers object may be copied as necessary, ownership of the underlying
315 * memory blocks is retained by the caller, which must guarantee that they
316 * remain valid until the handler is called.
318 * @param handler The handler to be called when the send operation completes.
319 * Copies will be made of the handler as required. The function signature of
320 * the handler must be:
321 * @code void handler(
322 * const boost::system::error_code& error, // Result of operation.
323 * std::size_t bytes_transferred // Number of bytes sent.
325 * Regardless of whether the asynchronous operation completes immediately or
326 * not, the handler will not be invoked from within this function. Invocation
327 * of the handler will be performed in a manner equivalent to using
328 * boost::asio::io_context::post().
330 * @note The async_send operation can only be used with a connected socket.
331 * Use the async_send_to function to send data on an unconnected datagram
335 * To send a single data buffer use the @ref buffer function as follows:
337 * socket.async_send(boost::asio::buffer(data, size), handler);
339 * See the @ref buffer documentation for information on sending multiple
340 * buffers in one go, and how to use it with arrays, boost::array or
343 template <typename ConstBufferSequence, typename WriteHandler>
344 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
345 void (boost::system::error_code, std::size_t))
346 async_send(const ConstBufferSequence& buffers,
347 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
349 // If you get an error on the following line it means that your handler does
350 // not meet the documented type requirements for a WriteHandler.
351 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
353 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
354 return this->get_service().async_send(this->get_implementation(),
355 buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
356 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
357 async_completion<WriteHandler,
358 void (boost::system::error_code, std::size_t)> init(handler);
360 this->get_service().async_send(this->get_implementation(),
361 buffers, 0, init.completion_handler);
363 return init.result.get();
364 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
367 /// Start an asynchronous send on a connected socket.
369 * This function is used to asynchronously send data on the datagram socket.
370 * The function call always returns immediately.
372 * @param buffers One or more data buffers to be sent on the socket. Although
373 * the buffers object may be copied as necessary, ownership of the underlying
374 * memory blocks is retained by the caller, which must guarantee that they
375 * remain valid until the handler is called.
377 * @param flags Flags specifying how the send call is to be made.
379 * @param handler The handler to be called when the send operation completes.
380 * Copies will be made of the handler as required. The function signature of
381 * the handler must be:
382 * @code void handler(
383 * const boost::system::error_code& error, // Result of operation.
384 * std::size_t bytes_transferred // Number of bytes sent.
386 * Regardless of whether the asynchronous operation completes immediately or
387 * not, the handler will not be invoked from within this function. Invocation
388 * of the handler will be performed in a manner equivalent to using
389 * boost::asio::io_context::post().
391 * @note The async_send operation can only be used with a connected socket.
392 * Use the async_send_to function to send data on an unconnected datagram
395 template <typename ConstBufferSequence, typename WriteHandler>
396 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
397 void (boost::system::error_code, std::size_t))
398 async_send(const ConstBufferSequence& buffers,
399 socket_base::message_flags flags,
400 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
402 // If you get an error on the following line it means that your handler does
403 // not meet the documented type requirements for a WriteHandler.
404 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
406 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
407 return this->get_service().async_send(this->get_implementation(),
408 buffers, flags, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
409 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
410 async_completion<WriteHandler,
411 void (boost::system::error_code, std::size_t)> init(handler);
413 this->get_service().async_send(this->get_implementation(),
414 buffers, flags, init.completion_handler);
416 return init.result.get();
417 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
420 /// Send a datagram to the specified endpoint.
422 * This function is used to send a datagram to the specified remote endpoint.
423 * The function call will block until the data has been sent successfully or
426 * @param buffers One or more data buffers to be sent to the remote endpoint.
428 * @param destination The remote endpoint to which the data will be sent.
430 * @returns The number of bytes sent.
432 * @throws boost::system::system_error Thrown on failure.
435 * To send a single data buffer use the @ref buffer function as follows:
437 * boost::asio::ip::udp::endpoint destination(
438 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
439 * socket.send_to(boost::asio::buffer(data, size), destination);
441 * See the @ref buffer documentation for information on sending multiple
442 * buffers in one go, and how to use it with arrays, boost::array or
445 template <typename ConstBufferSequence>
446 std::size_t send_to(const ConstBufferSequence& buffers,
447 const endpoint_type& destination)
449 boost::system::error_code ec;
450 std::size_t s = this->get_service().send_to(
451 this->get_implementation(), buffers, destination, 0, ec);
452 boost::asio::detail::throw_error(ec, "send_to");
456 /// Send a datagram to the specified endpoint.
458 * This function is used to send a datagram to the specified remote endpoint.
459 * The function call will block until the data has been sent successfully or
462 * @param buffers One or more data buffers to be sent to the remote endpoint.
464 * @param destination The remote endpoint to which the data will be sent.
466 * @param flags Flags specifying how the send call is to be made.
468 * @returns The number of bytes sent.
470 * @throws boost::system::system_error Thrown on failure.
472 template <typename ConstBufferSequence>
473 std::size_t send_to(const ConstBufferSequence& buffers,
474 const endpoint_type& destination, socket_base::message_flags flags)
476 boost::system::error_code ec;
477 std::size_t s = this->get_service().send_to(
478 this->get_implementation(), buffers, destination, flags, ec);
479 boost::asio::detail::throw_error(ec, "send_to");
483 /// Send a datagram to the specified endpoint.
485 * This function is used to send a datagram to the specified remote endpoint.
486 * The function call will block until the data has been sent successfully or
489 * @param buffers One or more data buffers to be sent to the remote endpoint.
491 * @param destination The remote endpoint to which the data will be sent.
493 * @param flags Flags specifying how the send call is to be made.
495 * @param ec Set to indicate what error occurred, if any.
497 * @returns The number of bytes sent.
499 template <typename ConstBufferSequence>
500 std::size_t send_to(const ConstBufferSequence& buffers,
501 const endpoint_type& destination, socket_base::message_flags flags,
502 boost::system::error_code& ec)
504 return this->get_service().send_to(this->get_implementation(),
505 buffers, destination, flags, ec);
508 /// Start an asynchronous send.
510 * This function is used to asynchronously send a datagram to the specified
511 * remote endpoint. The function call always returns immediately.
513 * @param buffers One or more data buffers to be sent to the remote endpoint.
514 * Although the buffers object may be copied as necessary, ownership of the
515 * underlying memory blocks is retained by the caller, which must guarantee
516 * that they remain valid until the handler is called.
518 * @param destination The remote endpoint to which the data will be sent.
519 * Copies will be made of the endpoint as required.
521 * @param handler The handler to be called when the send operation completes.
522 * Copies will be made of the handler as required. The function signature of
523 * the handler must be:
524 * @code void handler(
525 * const boost::system::error_code& error, // Result of operation.
526 * std::size_t bytes_transferred // Number of bytes sent.
528 * Regardless of whether the asynchronous operation completes immediately or
529 * not, the handler will not be invoked from within this function. Invocation
530 * of the handler will be performed in a manner equivalent to using
531 * boost::asio::io_context::post().
534 * To send a single data buffer use the @ref buffer function as follows:
536 * boost::asio::ip::udp::endpoint destination(
537 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
538 * socket.async_send_to(
539 * boost::asio::buffer(data, size), destination, handler);
541 * See the @ref buffer documentation for information on sending multiple
542 * buffers in one go, and how to use it with arrays, boost::array or
545 template <typename ConstBufferSequence, typename WriteHandler>
546 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
547 void (boost::system::error_code, std::size_t))
548 async_send_to(const ConstBufferSequence& buffers,
549 const endpoint_type& destination,
550 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
552 // If you get an error on the following line it means that your handler does
553 // not meet the documented type requirements for a WriteHandler.
554 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
556 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
557 return this->get_service().async_send_to(
558 this->get_implementation(), buffers, destination, 0,
559 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
560 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
561 async_completion<WriteHandler,
562 void (boost::system::error_code, std::size_t)> init(handler);
564 this->get_service().async_send_to(
565 this->get_implementation(), buffers, destination, 0,
566 init.completion_handler);
568 return init.result.get();
569 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
572 /// Start an asynchronous send.
574 * This function is used to asynchronously send a datagram to the specified
575 * remote endpoint. The function call always returns immediately.
577 * @param buffers One or more data buffers to be sent to the remote endpoint.
578 * Although the buffers object may be copied as necessary, ownership of the
579 * underlying memory blocks is retained by the caller, which must guarantee
580 * that they remain valid until the handler is called.
582 * @param flags Flags specifying how the send call is to be made.
584 * @param destination The remote endpoint to which the data will be sent.
585 * Copies will be made of the endpoint as required.
587 * @param handler The handler to be called when the send operation completes.
588 * Copies will be made of the handler as required. The function signature of
589 * the handler must be:
590 * @code void handler(
591 * const boost::system::error_code& error, // Result of operation.
592 * std::size_t bytes_transferred // Number of bytes sent.
594 * Regardless of whether the asynchronous operation completes immediately or
595 * not, the handler will not be invoked from within this function. Invocation
596 * of the handler will be performed in a manner equivalent to using
597 * boost::asio::io_context::post().
599 template <typename ConstBufferSequence, typename WriteHandler>
600 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
601 void (boost::system::error_code, std::size_t))
602 async_send_to(const ConstBufferSequence& buffers,
603 const endpoint_type& destination, socket_base::message_flags flags,
604 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
606 // If you get an error on the following line it means that your handler does
607 // not meet the documented type requirements for a WriteHandler.
608 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
610 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
611 return this->get_service().async_send_to(
612 this->get_implementation(), buffers, destination, flags,
613 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
614 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
615 async_completion<WriteHandler,
616 void (boost::system::error_code, std::size_t)> init(handler);
618 this->get_service().async_send_to(
619 this->get_implementation(), buffers, destination, flags,
620 init.completion_handler);
622 return init.result.get();
623 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
626 /// Receive some data on a connected socket.
628 * This function is used to receive data on the datagram socket. The function
629 * call will block until data has been received successfully or an error
632 * @param buffers One or more buffers into which the data will be received.
634 * @returns The number of bytes received.
636 * @throws boost::system::system_error Thrown on failure.
638 * @note The receive operation can only be used with a connected socket. Use
639 * the receive_from function to receive data on an unconnected datagram
643 * To receive into a single data buffer use the @ref buffer function as
645 * @code socket.receive(boost::asio::buffer(data, size)); @endcode
646 * See the @ref buffer documentation for information on receiving into
647 * multiple buffers in one go, and how to use it with arrays, boost::array or
650 template <typename MutableBufferSequence>
651 std::size_t receive(const MutableBufferSequence& buffers)
653 boost::system::error_code ec;
654 std::size_t s = this->get_service().receive(
655 this->get_implementation(), buffers, 0, ec);
656 boost::asio::detail::throw_error(ec, "receive");
660 /// Receive some data on a connected socket.
662 * This function is used to receive data on the datagram socket. The function
663 * call will block until data has been received successfully or an error
666 * @param buffers One or more buffers into which the data will be received.
668 * @param flags Flags specifying how the receive call is to be made.
670 * @returns The number of bytes received.
672 * @throws boost::system::system_error Thrown on failure.
674 * @note The receive operation can only be used with a connected socket. Use
675 * the receive_from function to receive data on an unconnected datagram
678 template <typename MutableBufferSequence>
679 std::size_t receive(const MutableBufferSequence& buffers,
680 socket_base::message_flags flags)
682 boost::system::error_code ec;
683 std::size_t s = this->get_service().receive(
684 this->get_implementation(), buffers, flags, ec);
685 boost::asio::detail::throw_error(ec, "receive");
689 /// Receive some data on a connected socket.
691 * This function is used to receive data on the datagram socket. The function
692 * call will block until data has been received successfully or an error
695 * @param buffers One or more buffers into which the data will be received.
697 * @param flags Flags specifying how the receive call is to be made.
699 * @param ec Set to indicate what error occurred, if any.
701 * @returns The number of bytes received.
703 * @note The receive operation can only be used with a connected socket. Use
704 * the receive_from function to receive data on an unconnected datagram
707 template <typename MutableBufferSequence>
708 std::size_t receive(const MutableBufferSequence& buffers,
709 socket_base::message_flags flags, boost::system::error_code& ec)
711 return this->get_service().receive(
712 this->get_implementation(), buffers, flags, ec);
715 /// Start an asynchronous receive on a connected socket.
717 * This function is used to asynchronously receive data from the datagram
718 * socket. The function call always returns immediately.
720 * @param buffers One or more buffers into which the data will be received.
721 * Although the buffers object may be copied as necessary, ownership of the
722 * underlying memory blocks is retained by the caller, which must guarantee
723 * that they remain valid until the handler is called.
725 * @param handler The handler to be called when the receive operation
726 * completes. Copies will be made of the handler as required. The function
727 * signature of the handler must be:
728 * @code void handler(
729 * const boost::system::error_code& error, // Result of operation.
730 * std::size_t bytes_transferred // Number of bytes received.
732 * Regardless of whether the asynchronous operation completes immediately or
733 * not, the handler will not be invoked from within this function. Invocation
734 * of the handler will be performed in a manner equivalent to using
735 * boost::asio::io_context::post().
737 * @note The async_receive operation can only be used with a connected socket.
738 * Use the async_receive_from function to receive data on an unconnected
742 * To receive into a single data buffer use the @ref buffer function as
745 * socket.async_receive(boost::asio::buffer(data, size), handler);
747 * See the @ref buffer documentation for information on receiving into
748 * multiple buffers in one go, and how to use it with arrays, boost::array or
751 template <typename MutableBufferSequence, typename ReadHandler>
752 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
753 void (boost::system::error_code, std::size_t))
754 async_receive(const MutableBufferSequence& buffers,
755 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
757 // If you get an error on the following line it means that your handler does
758 // not meet the documented type requirements for a ReadHandler.
759 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
761 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
762 return this->get_service().async_receive(this->get_implementation(),
763 buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
764 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
765 async_completion<ReadHandler,
766 void (boost::system::error_code, std::size_t)> init(handler);
768 this->get_service().async_receive(this->get_implementation(),
769 buffers, 0, init.completion_handler);
771 return init.result.get();
772 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
775 /// Start an asynchronous receive on a connected socket.
777 * This function is used to asynchronously receive data from the datagram
778 * socket. The function call always returns immediately.
780 * @param buffers One or more buffers into which the data will be received.
781 * Although the buffers object may be copied as necessary, ownership of the
782 * underlying memory blocks is retained by the caller, which must guarantee
783 * that they remain valid until the handler is called.
785 * @param flags Flags specifying how the receive call is to be made.
787 * @param handler The handler to be called when the receive operation
788 * completes. Copies will be made of the handler as required. The function
789 * signature of the handler must be:
790 * @code void handler(
791 * const boost::system::error_code& error, // Result of operation.
792 * std::size_t bytes_transferred // Number of bytes received.
794 * Regardless of whether the asynchronous operation completes immediately or
795 * not, the handler will not be invoked from within this function. Invocation
796 * of the handler will be performed in a manner equivalent to using
797 * boost::asio::io_context::post().
799 * @note The async_receive operation can only be used with a connected socket.
800 * Use the async_receive_from function to receive data on an unconnected
803 template <typename MutableBufferSequence, typename ReadHandler>
804 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
805 void (boost::system::error_code, std::size_t))
806 async_receive(const MutableBufferSequence& buffers,
807 socket_base::message_flags flags,
808 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
810 // If you get an error on the following line it means that your handler does
811 // not meet the documented type requirements for a ReadHandler.
812 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
814 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
815 return this->get_service().async_receive(this->get_implementation(),
816 buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
817 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
818 async_completion<ReadHandler,
819 void (boost::system::error_code, std::size_t)> init(handler);
821 this->get_service().async_receive(this->get_implementation(),
822 buffers, flags, init.completion_handler);
824 return init.result.get();
825 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
828 /// Receive a datagram with the endpoint of the sender.
830 * This function is used to receive a datagram. The function call will block
831 * until data has been received successfully or an error occurs.
833 * @param buffers One or more buffers into which the data will be received.
835 * @param sender_endpoint An endpoint object that receives the endpoint of
836 * the remote sender of the datagram.
838 * @returns The number of bytes received.
840 * @throws boost::system::system_error Thrown on failure.
843 * To receive into a single data buffer use the @ref buffer function as
846 * boost::asio::ip::udp::endpoint sender_endpoint;
847 * socket.receive_from(
848 * boost::asio::buffer(data, size), sender_endpoint);
850 * See the @ref buffer documentation for information on receiving into
851 * multiple buffers in one go, and how to use it with arrays, boost::array or
854 template <typename MutableBufferSequence>
855 std::size_t receive_from(const MutableBufferSequence& buffers,
856 endpoint_type& sender_endpoint)
858 boost::system::error_code ec;
859 std::size_t s = this->get_service().receive_from(
860 this->get_implementation(), buffers, sender_endpoint, 0, ec);
861 boost::asio::detail::throw_error(ec, "receive_from");
865 /// Receive a datagram with the endpoint of the sender.
867 * This function is used to receive a datagram. The function call will block
868 * until data has been received successfully or an error occurs.
870 * @param buffers One or more buffers into which the data will be received.
872 * @param sender_endpoint An endpoint object that receives the endpoint of
873 * the remote sender of the datagram.
875 * @param flags Flags specifying how the receive call is to be made.
877 * @returns The number of bytes received.
879 * @throws boost::system::system_error Thrown on failure.
881 template <typename MutableBufferSequence>
882 std::size_t receive_from(const MutableBufferSequence& buffers,
883 endpoint_type& sender_endpoint, socket_base::message_flags flags)
885 boost::system::error_code ec;
886 std::size_t s = this->get_service().receive_from(
887 this->get_implementation(), buffers, sender_endpoint, flags, ec);
888 boost::asio::detail::throw_error(ec, "receive_from");
892 /// Receive a datagram with the endpoint of the sender.
894 * This function is used to receive a datagram. The function call will block
895 * until data has been received successfully or an error occurs.
897 * @param buffers One or more buffers into which the data will be received.
899 * @param sender_endpoint An endpoint object that receives the endpoint of
900 * the remote sender of the datagram.
902 * @param flags Flags specifying how the receive call is to be made.
904 * @param ec Set to indicate what error occurred, if any.
906 * @returns The number of bytes received.
908 template <typename MutableBufferSequence>
909 std::size_t receive_from(const MutableBufferSequence& buffers,
910 endpoint_type& sender_endpoint, socket_base::message_flags flags,
911 boost::system::error_code& ec)
913 return this->get_service().receive_from(this->get_implementation(),
914 buffers, sender_endpoint, flags, ec);
917 /// Start an asynchronous receive.
919 * This function is used to asynchronously receive a datagram. The function
920 * call always returns immediately.
922 * @param buffers One or more buffers into which the data will be received.
923 * Although the buffers object may be copied as necessary, ownership of the
924 * underlying memory blocks is retained by the caller, which must guarantee
925 * that they remain valid until the handler is called.
927 * @param sender_endpoint An endpoint object that receives the endpoint of
928 * the remote sender of the datagram. Ownership of the sender_endpoint object
929 * is retained by the caller, which must guarantee that it is valid until the
932 * @param handler The handler to be called when the receive operation
933 * completes. Copies will be made of the handler as required. The function
934 * signature of the handler must be:
935 * @code void handler(
936 * const boost::system::error_code& error, // Result of operation.
937 * std::size_t bytes_transferred // Number of bytes received.
939 * Regardless of whether the asynchronous operation completes immediately or
940 * not, the handler will not be invoked from within this function. Invocation
941 * of the handler will be performed in a manner equivalent to using
942 * boost::asio::io_context::post().
945 * To receive into a single data buffer use the @ref buffer function as
947 * @code socket.async_receive_from(
948 * boost::asio::buffer(data, size), sender_endpoint, handler); @endcode
949 * See the @ref buffer documentation for information on receiving into
950 * multiple buffers in one go, and how to use it with arrays, boost::array or
953 template <typename MutableBufferSequence, typename ReadHandler>
954 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
955 void (boost::system::error_code, std::size_t))
956 async_receive_from(const MutableBufferSequence& buffers,
957 endpoint_type& sender_endpoint,
958 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
960 // If you get an error on the following line it means that your handler does
961 // not meet the documented type requirements for a ReadHandler.
962 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
964 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
965 return this->get_service().async_receive_from(
966 this->get_implementation(), buffers, sender_endpoint, 0,
967 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
968 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
969 async_completion<ReadHandler,
970 void (boost::system::error_code, std::size_t)> init(handler);
972 this->get_service().async_receive_from(
973 this->get_implementation(), buffers, sender_endpoint, 0,
974 init.completion_handler);
976 return init.result.get();
977 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
980 /// Start an asynchronous receive.
982 * This function is used to asynchronously receive a datagram. The function
983 * call always returns immediately.
985 * @param buffers One or more buffers into which the data will be received.
986 * Although the buffers object may be copied as necessary, ownership of the
987 * underlying memory blocks is retained by the caller, which must guarantee
988 * that they remain valid until the handler is called.
990 * @param sender_endpoint An endpoint object that receives the endpoint of
991 * the remote sender of the datagram. Ownership of the sender_endpoint object
992 * is retained by the caller, which must guarantee that it is valid until the
995 * @param flags Flags specifying how the receive call is to be made.
997 * @param handler The handler to be called when the receive operation
998 * completes. Copies will be made of the handler as required. The function
999 * signature of the handler must be:
1000 * @code void handler(
1001 * const boost::system::error_code& error, // Result of operation.
1002 * std::size_t bytes_transferred // Number of bytes received.
1004 * Regardless of whether the asynchronous operation completes immediately or
1005 * not, the handler will not be invoked from within this function. Invocation
1006 * of the handler will be performed in a manner equivalent to using
1007 * boost::asio::io_context::post().
1009 template <typename MutableBufferSequence, typename ReadHandler>
1010 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
1011 void (boost::system::error_code, std::size_t))
1012 async_receive_from(const MutableBufferSequence& buffers,
1013 endpoint_type& sender_endpoint, socket_base::message_flags flags,
1014 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
1016 // If you get an error on the following line it means that your handler does
1017 // not meet the documented type requirements for a ReadHandler.
1018 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1020 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
1021 return this->get_service().async_receive_from(
1022 this->get_implementation(), buffers, sender_endpoint, flags,
1023 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
1024 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
1025 async_completion<ReadHandler,
1026 void (boost::system::error_code, std::size_t)> init(handler);
1028 this->get_service().async_receive_from(
1029 this->get_implementation(), buffers, sender_endpoint, flags,
1030 init.completion_handler);
1032 return init.result.get();
1033 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
1038 } // namespace boost
1040 #include <boost/asio/detail/pop_options.hpp>
1042 #endif // BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP