2 // basic_datagram_socket.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2011 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/datagram_socket_service.hpp>
22 #include <boost/asio/detail/handler_type_requirements.hpp>
23 #include <boost/asio/detail/throw_error.hpp>
24 #include <boost/asio/error.hpp>
26 #include <boost/asio/detail/push_options.hpp>
31 /// Provides datagram-oriented socket functionality.
33 * The basic_datagram_socket class template provides asynchronous and blocking
34 * datagram-oriented socket functionality.
37 * @e Distinct @e objects: Safe.@n
38 * @e Shared @e objects: Unsafe.
40 template <typename Protocol,
41 typename DatagramSocketService = datagram_socket_service<Protocol> >
42 class basic_datagram_socket
43 : public basic_socket<Protocol, DatagramSocketService>
46 /// (Deprecated: Use native_handle_type.) The native representation of a
48 typedef typename DatagramSocketService::native_handle_type native_type;
50 /// The native representation of a socket.
51 typedef typename DatagramSocketService::native_handle_type native_handle_type;
53 /// The protocol type.
54 typedef Protocol protocol_type;
56 /// The endpoint type.
57 typedef typename Protocol::endpoint endpoint_type;
59 /// Construct a basic_datagram_socket without opening it.
61 * This constructor creates a datagram socket without opening it. The open()
62 * function must be called before data can be sent or received on the socket.
64 * @param io_service The io_service object that the datagram socket will use
65 * to dispatch handlers for any asynchronous operations performed on the
68 explicit basic_datagram_socket(boost::asio::io_service& io_service)
69 : basic_socket<Protocol, DatagramSocketService>(io_service)
73 /// Construct and open a basic_datagram_socket.
75 * This constructor creates and opens a datagram socket.
77 * @param io_service The io_service object that the datagram socket will use
78 * to dispatch handlers for any asynchronous operations performed on the
81 * @param protocol An object specifying protocol parameters to be used.
83 * @throws boost::system::system_error Thrown on failure.
85 basic_datagram_socket(boost::asio::io_service& io_service,
86 const protocol_type& protocol)
87 : basic_socket<Protocol, DatagramSocketService>(io_service, protocol)
91 /// Construct a basic_datagram_socket, opening it and binding it to the given
94 * This constructor creates a datagram socket and automatically opens it bound
95 * to the specified endpoint on the local machine. The protocol used is the
96 * protocol associated with the given endpoint.
98 * @param io_service The io_service object that the datagram socket will use
99 * to dispatch handlers for any asynchronous operations performed on the
102 * @param endpoint An endpoint on the local machine to which the datagram
103 * socket will be bound.
105 * @throws boost::system::system_error Thrown on failure.
107 basic_datagram_socket(boost::asio::io_service& io_service,
108 const endpoint_type& endpoint)
109 : basic_socket<Protocol, DatagramSocketService>(io_service, endpoint)
113 /// Construct a basic_datagram_socket on an existing native socket.
115 * This constructor creates a datagram socket object to hold an existing
118 * @param io_service The io_service object that the datagram socket will use
119 * to dispatch handlers for any asynchronous operations performed on the
122 * @param protocol An object specifying protocol parameters to be used.
124 * @param native_socket The new underlying socket implementation.
126 * @throws boost::system::system_error Thrown on failure.
128 basic_datagram_socket(boost::asio::io_service& io_service,
129 const protocol_type& protocol, const native_handle_type& native_socket)
130 : basic_socket<Protocol, DatagramSocketService>(
131 io_service, protocol, native_socket)
135 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
136 /// Move-construct a basic_datagram_socket from another.
138 * This constructor moves a datagram socket from one object to another.
140 * @param other The other basic_datagram_socket object from which the move
143 * @note Following the move, the moved-from object is in the same state as if
144 * constructed using the @c basic_datagram_socket(io_service&) constructor.
146 basic_datagram_socket(basic_datagram_socket&& other)
147 : basic_socket<Protocol, DatagramSocketService>(
148 BOOST_ASIO_MOVE_CAST(basic_datagram_socket)(other))
152 /// Move-assign a basic_datagram_socket from another.
154 * This assignment operator moves a datagram socket from one object to
157 * @param other The other basic_datagram_socket object from which the move
160 * @note Following the move, the moved-from object is in the same state as if
161 * constructed using the @c basic_datagram_socket(io_service&) constructor.
163 basic_datagram_socket& operator=(basic_datagram_socket&& other)
165 basic_socket<Protocol, DatagramSocketService>::operator=(
166 BOOST_ASIO_MOVE_CAST(basic_datagram_socket)(other));
169 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
171 /// Send some data on a connected socket.
173 * This function is used to send data on the datagram socket. The function
174 * call will block until the data has been sent successfully or an error
177 * @param buffers One ore more data buffers to be sent on the socket.
179 * @returns The number of bytes sent.
181 * @throws boost::system::system_error Thrown on failure.
183 * @note The send operation can only be used with a connected socket. Use
184 * the send_to function to send data on an unconnected datagram socket.
187 * To send a single data buffer use the @ref buffer function as follows:
188 * @code socket.send(boost::asio::buffer(data, size)); @endcode
189 * See the @ref buffer documentation for information on sending multiple
190 * buffers in one go, and how to use it with arrays, boost::array or
193 template <typename ConstBufferSequence>
194 std::size_t send(const ConstBufferSequence& buffers)
196 boost::system::error_code ec;
197 std::size_t s = this->get_service().send(
198 this->get_implementation(), buffers, 0, ec);
199 boost::asio::detail::throw_error(ec, "send");
203 /// Send some data on a connected socket.
205 * This function is used to send data on the datagram socket. The function
206 * call will block until the data has been sent successfully or an error
209 * @param buffers One ore more data buffers to be sent on the socket.
211 * @param flags Flags specifying how the send call is to be made.
213 * @returns The number of bytes sent.
215 * @throws boost::system::system_error Thrown on failure.
217 * @note The send operation can only be used with a connected socket. Use
218 * the send_to function to send data on an unconnected datagram socket.
220 template <typename ConstBufferSequence>
221 std::size_t send(const ConstBufferSequence& buffers,
222 socket_base::message_flags flags)
224 boost::system::error_code ec;
225 std::size_t s = this->get_service().send(
226 this->get_implementation(), buffers, flags, ec);
227 boost::asio::detail::throw_error(ec, "send");
231 /// Send some data on a connected socket.
233 * This function is used to send data on the datagram socket. The function
234 * call will block until the data has been sent successfully or an error
237 * @param buffers One or more data buffers to be sent on the socket.
239 * @param flags Flags specifying how the send call is to be made.
241 * @param ec Set to indicate what error occurred, if any.
243 * @returns The number of bytes sent.
245 * @note The send operation can only be used with a connected socket. Use
246 * the send_to function to send data on an unconnected datagram socket.
248 template <typename ConstBufferSequence>
249 std::size_t send(const ConstBufferSequence& buffers,
250 socket_base::message_flags flags, boost::system::error_code& ec)
252 return this->get_service().send(
253 this->get_implementation(), buffers, flags, ec);
256 /// Start an asynchronous send on a connected socket.
258 * This function is used to send data on the datagram socket. The function
259 * call will block until the data has been sent successfully or an error
262 * @param buffers One or more data buffers to be sent on the socket. Although
263 * the buffers object may be copied as necessary, ownership of the underlying
264 * memory blocks is retained by the caller, which must guarantee that they
265 * remain valid until the handler is called.
267 * @param handler The handler to be called when the send operation completes.
268 * Copies will be made of the handler as required. The function signature of
269 * the handler must be:
270 * @code void handler(
271 * const boost::system::error_code& error, // Result of operation.
272 * std::size_t bytes_transferred // Number of bytes sent.
274 * Regardless of whether the asynchronous operation completes immediately or
275 * not, the handler will not be invoked from within this function. Invocation
276 * of the handler will be performed in a manner equivalent to using
277 * boost::asio::io_service::post().
279 * @note The async_send operation can only be used with a connected socket.
280 * Use the async_send_to function to send data on an unconnected datagram
284 * To send a single data buffer use the @ref buffer function as follows:
286 * socket.async_send(boost::asio::buffer(data, size), handler);
288 * See the @ref buffer documentation for information on sending multiple
289 * buffers in one go, and how to use it with arrays, boost::array or
292 template <typename ConstBufferSequence, typename WriteHandler>
293 void async_send(const ConstBufferSequence& buffers,
294 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
296 // If you get an error on the following line it means that your handler does
297 // not meet the documented type requirements for a WriteHandler.
298 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
300 this->get_service().async_send(this->get_implementation(),
301 buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
304 /// Start an asynchronous send on a connected socket.
306 * This function is used to send data on the datagram socket. The function
307 * call will block until the data has been sent successfully or an error
310 * @param buffers One or more data buffers to be sent on the socket. Although
311 * the buffers object may be copied as necessary, ownership of the underlying
312 * memory blocks is retained by the caller, which must guarantee that they
313 * remain valid until the handler is called.
315 * @param flags Flags specifying how the send call is to be made.
317 * @param handler The handler to be called when the send operation completes.
318 * Copies will be made of the handler as required. The function signature of
319 * the handler must be:
320 * @code void handler(
321 * const boost::system::error_code& error, // Result of operation.
322 * std::size_t bytes_transferred // Number of bytes sent.
324 * Regardless of whether the asynchronous operation completes immediately or
325 * not, the handler will not be invoked from within this function. Invocation
326 * of the handler will be performed in a manner equivalent to using
327 * boost::asio::io_service::post().
329 * @note The async_send operation can only be used with a connected socket.
330 * Use the async_send_to function to send data on an unconnected datagram
333 template <typename ConstBufferSequence, typename WriteHandler>
334 void async_send(const ConstBufferSequence& buffers,
335 socket_base::message_flags flags,
336 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
338 // If you get an error on the following line it means that your handler does
339 // not meet the documented type requirements for a WriteHandler.
340 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
342 this->get_service().async_send(this->get_implementation(),
343 buffers, flags, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
346 /// Send a datagram to the specified endpoint.
348 * This function is used to send a datagram to the specified remote endpoint.
349 * The function call will block until the data has been sent successfully or
352 * @param buffers One or more data buffers to be sent to the remote endpoint.
354 * @param destination The remote endpoint to which the data will be sent.
356 * @returns The number of bytes sent.
358 * @throws boost::system::system_error Thrown on failure.
361 * To send a single data buffer use the @ref buffer function as follows:
363 * boost::asio::ip::udp::endpoint destination(
364 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
365 * socket.send_to(boost::asio::buffer(data, size), destination);
367 * See the @ref buffer documentation for information on sending multiple
368 * buffers in one go, and how to use it with arrays, boost::array or
371 template <typename ConstBufferSequence>
372 std::size_t send_to(const ConstBufferSequence& buffers,
373 const endpoint_type& destination)
375 boost::system::error_code ec;
376 std::size_t s = this->get_service().send_to(
377 this->get_implementation(), buffers, destination, 0, ec);
378 boost::asio::detail::throw_error(ec, "send_to");
382 /// Send a datagram to the specified endpoint.
384 * This function is used to send a datagram to the specified remote endpoint.
385 * The function call will block until the data has been sent successfully or
388 * @param buffers One or more data buffers to be sent to the remote endpoint.
390 * @param destination The remote endpoint to which the data will be sent.
392 * @param flags Flags specifying how the send call is to be made.
394 * @returns The number of bytes sent.
396 * @throws boost::system::system_error Thrown on failure.
398 template <typename ConstBufferSequence>
399 std::size_t send_to(const ConstBufferSequence& buffers,
400 const endpoint_type& destination, socket_base::message_flags flags)
402 boost::system::error_code ec;
403 std::size_t s = this->get_service().send_to(
404 this->get_implementation(), buffers, destination, flags, ec);
405 boost::asio::detail::throw_error(ec, "send_to");
409 /// Send a datagram to the specified endpoint.
411 * This function is used to send a datagram to the specified remote endpoint.
412 * The function call will block until the data has been sent successfully or
415 * @param buffers One or more data buffers to be sent to the remote endpoint.
417 * @param destination The remote endpoint to which the data will be sent.
419 * @param flags Flags specifying how the send call is to be made.
421 * @param ec Set to indicate what error occurred, if any.
423 * @returns The number of bytes sent.
425 template <typename ConstBufferSequence>
426 std::size_t send_to(const ConstBufferSequence& buffers,
427 const endpoint_type& destination, socket_base::message_flags flags,
428 boost::system::error_code& ec)
430 return this->get_service().send_to(this->get_implementation(),
431 buffers, destination, flags, ec);
434 /// Start an asynchronous send.
436 * This function is used to asynchronously send a datagram to the specified
437 * remote endpoint. The function call always returns immediately.
439 * @param buffers One or more data buffers to be sent to the remote endpoint.
440 * Although the buffers object may be copied as necessary, ownership of the
441 * underlying memory blocks is retained by the caller, which must guarantee
442 * that they remain valid until the handler is called.
444 * @param destination The remote endpoint to which the data will be sent.
445 * Copies will be made of the endpoint as required.
447 * @param handler The handler to be called when the send operation completes.
448 * Copies will be made of the handler as required. The function signature of
449 * the handler must be:
450 * @code void handler(
451 * const boost::system::error_code& error, // Result of operation.
452 * std::size_t bytes_transferred // Number of bytes sent.
454 * Regardless of whether the asynchronous operation completes immediately or
455 * not, the handler will not be invoked from within this function. Invocation
456 * of the handler will be performed in a manner equivalent to using
457 * boost::asio::io_service::post().
460 * To send a single data buffer use the @ref buffer function as follows:
462 * boost::asio::ip::udp::endpoint destination(
463 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
464 * socket.async_send_to(
465 * boost::asio::buffer(data, size), destination, handler);
467 * See the @ref buffer documentation for information on sending multiple
468 * buffers in one go, and how to use it with arrays, boost::array or
471 template <typename ConstBufferSequence, typename WriteHandler>
472 void async_send_to(const ConstBufferSequence& buffers,
473 const endpoint_type& destination,
474 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
476 // If you get an error on the following line it means that your handler does
477 // not meet the documented type requirements for a WriteHandler.
478 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
480 this->get_service().async_send_to(this->get_implementation(), buffers,
481 destination, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
484 /// Start an asynchronous send.
486 * This function is used to asynchronously send a datagram to the specified
487 * remote endpoint. The function call always returns immediately.
489 * @param buffers One or more data buffers to be sent to the remote endpoint.
490 * Although the buffers object may be copied as necessary, ownership of the
491 * underlying memory blocks is retained by the caller, which must guarantee
492 * that they remain valid until the handler is called.
494 * @param flags Flags specifying how the send call is to be made.
496 * @param destination The remote endpoint to which the data will be sent.
497 * Copies will be made of the endpoint as required.
499 * @param handler The handler to be called when the send operation completes.
500 * Copies will be made of the handler as required. The function signature of
501 * the handler must be:
502 * @code void handler(
503 * const boost::system::error_code& error, // Result of operation.
504 * std::size_t bytes_transferred // Number of bytes sent.
506 * Regardless of whether the asynchronous operation completes immediately or
507 * not, the handler will not be invoked from within this function. Invocation
508 * of the handler will be performed in a manner equivalent to using
509 * boost::asio::io_service::post().
511 template <typename ConstBufferSequence, typename WriteHandler>
512 void async_send_to(const ConstBufferSequence& buffers,
513 const endpoint_type& destination, socket_base::message_flags flags,
514 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
516 // If you get an error on the following line it means that your handler does
517 // not meet the documented type requirements for a WriteHandler.
518 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
520 this->get_service().async_send_to(this->get_implementation(), buffers,
521 destination, flags, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
524 /// Receive some data on a connected socket.
526 * This function is used to receive data on the datagram socket. The function
527 * call will block until data has been received successfully or an error
530 * @param buffers One or more buffers into which the data will be received.
532 * @returns The number of bytes received.
534 * @throws boost::system::system_error Thrown on failure.
536 * @note The receive operation can only be used with a connected socket. Use
537 * the receive_from function to receive data on an unconnected datagram
541 * To receive into a single data buffer use the @ref buffer function as
543 * @code socket.receive(boost::asio::buffer(data, size)); @endcode
544 * See the @ref buffer documentation for information on receiving into
545 * multiple buffers in one go, and how to use it with arrays, boost::array or
548 template <typename MutableBufferSequence>
549 std::size_t receive(const MutableBufferSequence& buffers)
551 boost::system::error_code ec;
552 std::size_t s = this->get_service().receive(
553 this->get_implementation(), buffers, 0, ec);
554 boost::asio::detail::throw_error(ec, "receive");
558 /// Receive some data on a connected socket.
560 * This function is used to receive data on the datagram socket. The function
561 * call will block until data has been received successfully or an error
564 * @param buffers One or more buffers into which the data will be received.
566 * @param flags Flags specifying how the receive call is to be made.
568 * @returns The number of bytes received.
570 * @throws boost::system::system_error Thrown on failure.
572 * @note The receive operation can only be used with a connected socket. Use
573 * the receive_from function to receive data on an unconnected datagram
576 template <typename MutableBufferSequence>
577 std::size_t receive(const MutableBufferSequence& buffers,
578 socket_base::message_flags flags)
580 boost::system::error_code ec;
581 std::size_t s = this->get_service().receive(
582 this->get_implementation(), buffers, flags, ec);
583 boost::asio::detail::throw_error(ec, "receive");
587 /// Receive some data on a connected socket.
589 * This function is used to receive data on the datagram socket. The function
590 * call will block until data has been received successfully or an error
593 * @param buffers One or more buffers into which the data will be received.
595 * @param flags Flags specifying how the receive call is to be made.
597 * @param ec Set to indicate what error occurred, if any.
599 * @returns The number of bytes received.
601 * @note The receive operation can only be used with a connected socket. Use
602 * the receive_from function to receive data on an unconnected datagram
605 template <typename MutableBufferSequence>
606 std::size_t receive(const MutableBufferSequence& buffers,
607 socket_base::message_flags flags, boost::system::error_code& ec)
609 return this->get_service().receive(
610 this->get_implementation(), buffers, flags, ec);
613 /// Start an asynchronous receive on a connected socket.
615 * This function is used to asynchronously receive data from the datagram
616 * socket. The function call always returns immediately.
618 * @param buffers One or more buffers into which the data will be received.
619 * Although the buffers object may be copied as necessary, ownership of the
620 * underlying memory blocks is retained by the caller, which must guarantee
621 * that they remain valid until the handler is called.
623 * @param handler The handler to be called when the receive operation
624 * completes. Copies will be made of the handler as required. The function
625 * signature of the handler must be:
626 * @code void handler(
627 * const boost::system::error_code& error, // Result of operation.
628 * std::size_t bytes_transferred // Number of bytes received.
630 * Regardless of whether the asynchronous operation completes immediately or
631 * not, the handler will not be invoked from within this function. Invocation
632 * of the handler will be performed in a manner equivalent to using
633 * boost::asio::io_service::post().
635 * @note The async_receive operation can only be used with a connected socket.
636 * Use the async_receive_from function to receive data on an unconnected
640 * To receive into a single data buffer use the @ref buffer function as
643 * socket.async_receive(boost::asio::buffer(data, size), handler);
645 * See the @ref buffer documentation for information on receiving into
646 * multiple buffers in one go, and how to use it with arrays, boost::array or
649 template <typename MutableBufferSequence, typename ReadHandler>
650 void async_receive(const MutableBufferSequence& buffers,
651 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
653 // If you get an error on the following line it means that your handler does
654 // not meet the documented type requirements for a ReadHandler.
655 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
657 this->get_service().async_receive(this->get_implementation(),
658 buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
661 /// Start an asynchronous receive on a connected socket.
663 * This function is used to asynchronously receive data from the datagram
664 * socket. The function call always returns immediately.
666 * @param buffers One or more buffers into which the data will be received.
667 * Although the buffers object may be copied as necessary, ownership of the
668 * underlying memory blocks is retained by the caller, which must guarantee
669 * that they remain valid until the handler is called.
671 * @param flags Flags specifying how the receive call is to be made.
673 * @param handler The handler to be called when the receive operation
674 * completes. Copies will be made of the handler as required. The function
675 * signature of the handler must be:
676 * @code void handler(
677 * const boost::system::error_code& error, // Result of operation.
678 * std::size_t bytes_transferred // Number of bytes received.
680 * Regardless of whether the asynchronous operation completes immediately or
681 * not, the handler will not be invoked from within this function. Invocation
682 * of the handler will be performed in a manner equivalent to using
683 * boost::asio::io_service::post().
685 * @note The async_receive operation can only be used with a connected socket.
686 * Use the async_receive_from function to receive data on an unconnected
689 template <typename MutableBufferSequence, typename ReadHandler>
690 void async_receive(const MutableBufferSequence& buffers,
691 socket_base::message_flags flags,
692 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
694 // If you get an error on the following line it means that your handler does
695 // not meet the documented type requirements for a ReadHandler.
696 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
698 this->get_service().async_receive(this->get_implementation(),
699 buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
702 /// Receive a datagram with the endpoint of the sender.
704 * This function is used to receive a datagram. The function call will block
705 * until data has been received successfully or an error occurs.
707 * @param buffers One or more buffers into which the data will be received.
709 * @param sender_endpoint An endpoint object that receives the endpoint of
710 * the remote sender of the datagram.
712 * @returns The number of bytes received.
714 * @throws boost::system::system_error Thrown on failure.
717 * To receive into a single data buffer use the @ref buffer function as
720 * boost::asio::ip::udp::endpoint sender_endpoint;
721 * socket.receive_from(
722 * boost::asio::buffer(data, size), sender_endpoint);
724 * See the @ref buffer documentation for information on receiving into
725 * multiple buffers in one go, and how to use it with arrays, boost::array or
728 template <typename MutableBufferSequence>
729 std::size_t receive_from(const MutableBufferSequence& buffers,
730 endpoint_type& sender_endpoint)
732 boost::system::error_code ec;
733 std::size_t s = this->get_service().receive_from(
734 this->get_implementation(), buffers, sender_endpoint, 0, ec);
735 boost::asio::detail::throw_error(ec, "receive_from");
739 /// Receive a datagram with the endpoint of the sender.
741 * This function is used to receive a datagram. The function call will block
742 * until data has been received successfully or an error occurs.
744 * @param buffers One or more buffers into which the data will be received.
746 * @param sender_endpoint An endpoint object that receives the endpoint of
747 * the remote sender of the datagram.
749 * @param flags Flags specifying how the receive call is to be made.
751 * @returns The number of bytes received.
753 * @throws boost::system::system_error Thrown on failure.
755 template <typename MutableBufferSequence>
756 std::size_t receive_from(const MutableBufferSequence& buffers,
757 endpoint_type& sender_endpoint, socket_base::message_flags flags)
759 boost::system::error_code ec;
760 std::size_t s = this->get_service().receive_from(
761 this->get_implementation(), buffers, sender_endpoint, flags, ec);
762 boost::asio::detail::throw_error(ec, "receive_from");
766 /// Receive a datagram with the endpoint of the sender.
768 * This function is used to receive a datagram. The function call will block
769 * until data has been received successfully or an error occurs.
771 * @param buffers One or more buffers into which the data will be received.
773 * @param sender_endpoint An endpoint object that receives the endpoint of
774 * the remote sender of the datagram.
776 * @param flags Flags specifying how the receive call is to be made.
778 * @param ec Set to indicate what error occurred, if any.
780 * @returns The number of bytes received.
782 template <typename MutableBufferSequence>
783 std::size_t receive_from(const MutableBufferSequence& buffers,
784 endpoint_type& sender_endpoint, socket_base::message_flags flags,
785 boost::system::error_code& ec)
787 return this->get_service().receive_from(this->get_implementation(),
788 buffers, sender_endpoint, flags, ec);
791 /// Start an asynchronous receive.
793 * This function is used to asynchronously receive a datagram. The function
794 * call always returns immediately.
796 * @param buffers One or more buffers into which the data will be received.
797 * Although the buffers object may be copied as necessary, ownership of the
798 * underlying memory blocks is retained by the caller, which must guarantee
799 * that they remain valid until the handler is called.
801 * @param sender_endpoint An endpoint object that receives the endpoint of
802 * the remote sender of the datagram. Ownership of the sender_endpoint object
803 * is retained by the caller, which must guarantee that it is valid until the
806 * @param handler The handler to be called when the receive operation
807 * completes. Copies will be made of the handler as required. The function
808 * signature of the handler must be:
809 * @code void handler(
810 * const boost::system::error_code& error, // Result of operation.
811 * std::size_t bytes_transferred // Number of bytes received.
813 * Regardless of whether the asynchronous operation completes immediately or
814 * not, the handler will not be invoked from within this function. Invocation
815 * of the handler will be performed in a manner equivalent to using
816 * boost::asio::io_service::post().
819 * To receive into a single data buffer use the @ref buffer function as
821 * @code socket.async_receive_from(
822 * boost::asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
823 * See the @ref buffer documentation for information on receiving into
824 * multiple buffers in one go, and how to use it with arrays, boost::array or
827 template <typename MutableBufferSequence, typename ReadHandler>
828 void async_receive_from(const MutableBufferSequence& buffers,
829 endpoint_type& sender_endpoint,
830 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
832 // If you get an error on the following line it means that your handler does
833 // not meet the documented type requirements for a ReadHandler.
834 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
836 this->get_service().async_receive_from(this->get_implementation(), buffers,
837 sender_endpoint, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
840 /// Start an asynchronous receive.
842 * This function is used to asynchronously receive a datagram. The function
843 * call always returns immediately.
845 * @param buffers One or more buffers into which the data will be received.
846 * Although the buffers object may be copied as necessary, ownership of the
847 * underlying memory blocks is retained by the caller, which must guarantee
848 * that they remain valid until the handler is called.
850 * @param sender_endpoint An endpoint object that receives the endpoint of
851 * the remote sender of the datagram. Ownership of the sender_endpoint object
852 * is retained by the caller, which must guarantee that it is valid until the
855 * @param flags Flags specifying how the receive call is to be made.
857 * @param handler The handler to be called when the receive operation
858 * completes. Copies will be made of the handler as required. The function
859 * signature of the handler must be:
860 * @code void handler(
861 * const boost::system::error_code& error, // Result of operation.
862 * std::size_t bytes_transferred // Number of bytes received.
864 * Regardless of whether the asynchronous operation completes immediately or
865 * not, the handler will not be invoked from within this function. Invocation
866 * of the handler will be performed in a manner equivalent to using
867 * boost::asio::io_service::post().
869 template <typename MutableBufferSequence, typename ReadHandler>
870 void async_receive_from(const MutableBufferSequence& buffers,
871 endpoint_type& sender_endpoint, socket_base::message_flags flags,
872 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
874 // If you get an error on the following line it means that your handler does
875 // not meet the documented type requirements for a ReadHandler.
876 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
878 this->get_service().async_receive_from(this->get_implementation(), buffers,
879 sender_endpoint, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
886 #include <boost/asio/detail/pop_options.hpp>
888 #endif // BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP