fix doc example typo
[boost.git] / boost / asio / basic_datagram_socket.hpp
blob8fa870b5e6ab8aac41f9149f1dc3f6e5e0d7d461
1 //
2 // basic_datagram_socket.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
11 #ifndef BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <cstddef>
22 #include <boost/config.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 #include <boost/asio/basic_socket.hpp>
26 #include <boost/asio/datagram_socket_service.hpp>
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/detail/throw_error.hpp>
30 namespace boost {
31 namespace asio {
33 /// Provides datagram-oriented socket functionality.
34 /**
35 * The basic_datagram_socket class template provides asynchronous and blocking
36 * datagram-oriented socket functionality.
38 * @par Thread Safety
39 * @e Distinct @e objects: Safe.@n
40 * @e Shared @e objects: Unsafe.
42 template <typename Protocol,
43 typename DatagramSocketService = datagram_socket_service<Protocol> >
44 class basic_datagram_socket
45 : public basic_socket<Protocol, DatagramSocketService>
47 public:
48 /// The native representation of a socket.
49 typedef typename DatagramSocketService::native_type native_type;
51 /// The protocol type.
52 typedef Protocol protocol_type;
54 /// The endpoint type.
55 typedef typename Protocol::endpoint endpoint_type;
57 /// Construct a basic_datagram_socket without opening it.
58 /**
59 * This constructor creates a datagram socket without opening it. The open()
60 * function must be called before data can be sent or received on the socket.
62 * @param io_service The io_service object that the datagram socket will use
63 * to dispatch handlers for any asynchronous operations performed on the
64 * socket.
66 explicit basic_datagram_socket(boost::asio::io_service& io_service)
67 : basic_socket<Protocol, DatagramSocketService>(io_service)
71 /// Construct and open a basic_datagram_socket.
72 /**
73 * This constructor creates and opens a datagram socket.
75 * @param io_service The io_service object that the datagram socket will use
76 * to dispatch handlers for any asynchronous operations performed on the
77 * socket.
79 * @param protocol An object specifying protocol parameters to be used.
81 * @throws boost::system::system_error Thrown on failure.
83 basic_datagram_socket(boost::asio::io_service& io_service,
84 const protocol_type& protocol)
85 : basic_socket<Protocol, DatagramSocketService>(io_service, protocol)
89 /// Construct a basic_datagram_socket, opening it and binding it to the given
90 /// local endpoint.
91 /**
92 * This constructor creates a datagram socket and automatically opens it bound
93 * to the specified endpoint on the local machine. The protocol used is the
94 * protocol associated with the given endpoint.
96 * @param io_service The io_service object that the datagram socket will use
97 * to dispatch handlers for any asynchronous operations performed on the
98 * socket.
100 * @param endpoint An endpoint on the local machine to which the datagram
101 * socket will be bound.
103 * @throws boost::system::system_error Thrown on failure.
105 basic_datagram_socket(boost::asio::io_service& io_service,
106 const endpoint_type& endpoint)
107 : basic_socket<Protocol, DatagramSocketService>(io_service, endpoint)
111 /// Construct a basic_datagram_socket on an existing native socket.
113 * This constructor creates a datagram socket object to hold an existing
114 * native socket.
116 * @param io_service The io_service object that the datagram socket will use
117 * to dispatch handlers for any asynchronous operations performed on the
118 * socket.
120 * @param protocol An object specifying protocol parameters to be used.
122 * @param native_socket The new underlying socket implementation.
124 * @throws boost::system::system_error Thrown on failure.
126 basic_datagram_socket(boost::asio::io_service& io_service,
127 const protocol_type& protocol, const native_type& native_socket)
128 : basic_socket<Protocol, DatagramSocketService>(
129 io_service, protocol, native_socket)
133 /// Send some data on a connected socket.
135 * This function is used to send data on the datagram socket. The function
136 * call will block until the data has been sent successfully or an error
137 * occurs.
139 * @param buffers One ore more data buffers to be sent on the socket.
141 * @returns The number of bytes sent.
143 * @throws boost::system::system_error Thrown on failure.
145 * @note The send operation can only be used with a connected socket. Use
146 * the send_to function to send data on an unconnected datagram socket.
148 * @par Example
149 * To send a single data buffer use the @ref buffer function as follows:
150 * @code socket.send(boost::asio::buffer(data, size)); @endcode
151 * See the @ref buffer documentation for information on sending multiple
152 * buffers in one go, and how to use it with arrays, boost::array or
153 * std::vector.
155 template <typename ConstBufferSequence>
156 std::size_t send(const ConstBufferSequence& buffers)
158 boost::system::error_code ec;
159 std::size_t s = this->service.send(this->implementation, buffers, 0, ec);
160 boost::asio::detail::throw_error(ec);
161 return s;
164 /// Send some data on a connected socket.
166 * This function is used to send data on the datagram socket. The function
167 * call will block until the data has been sent successfully or an error
168 * occurs.
170 * @param buffers One ore more data buffers to be sent on the socket.
172 * @param flags Flags specifying how the send call is to be made.
174 * @returns The number of bytes sent.
176 * @throws boost::system::system_error Thrown on failure.
178 * @note The send operation can only be used with a connected socket. Use
179 * the send_to function to send data on an unconnected datagram socket.
181 template <typename ConstBufferSequence>
182 std::size_t send(const ConstBufferSequence& buffers,
183 socket_base::message_flags flags)
185 boost::system::error_code ec;
186 std::size_t s = this->service.send(
187 this->implementation, buffers, flags, ec);
188 boost::asio::detail::throw_error(ec);
189 return s;
192 /// Send some data on a connected socket.
194 * This function is used to send data on the datagram socket. The function
195 * call will block until the data has been sent successfully or an error
196 * occurs.
198 * @param buffers One or more data buffers to be sent on the socket.
200 * @param flags Flags specifying how the send call is to be made.
202 * @param ec Set to indicate what error occurred, if any.
204 * @returns The number of bytes sent.
206 * @note The send operation can only be used with a connected socket. Use
207 * the send_to function to send data on an unconnected datagram socket.
209 template <typename ConstBufferSequence>
210 std::size_t send(const ConstBufferSequence& buffers,
211 socket_base::message_flags flags, boost::system::error_code& ec)
213 return this->service.send(this->implementation, buffers, flags, ec);
216 /// Start an asynchronous send on a connected socket.
218 * This function is used to send data on the datagram socket. The function
219 * call will block until the data has been sent successfully or an error
220 * occurs.
222 * @param buffers One or more data buffers to be sent on the socket. Although
223 * the buffers object may be copied as necessary, ownership of the underlying
224 * memory blocks is retained by the caller, which must guarantee that they
225 * remain valid until the handler is called.
227 * @param handler The handler to be called when the send operation completes.
228 * Copies will be made of the handler as required. The function signature of
229 * the handler must be:
230 * @code void handler(
231 * const boost::system::error_code& error, // Result of operation.
232 * std::size_t bytes_transferred // Number of bytes sent.
233 * ); @endcode
234 * Regardless of whether the asynchronous operation completes immediately or
235 * not, the handler will not be invoked from within this function. Invocation
236 * of the handler will be performed in a manner equivalent to using
237 * boost::asio::io_service::post().
239 * @note The async_send operation can only be used with a connected socket.
240 * Use the async_send_to function to send data on an unconnected datagram
241 * socket.
243 * @par Example
244 * To send a single data buffer use the @ref buffer function as follows:
245 * @code
246 * socket.async_send(boost::asio::buffer(data, size), handler);
247 * @endcode
248 * See the @ref buffer documentation for information on sending multiple
249 * buffers in one go, and how to use it with arrays, boost::array or
250 * std::vector.
252 template <typename ConstBufferSequence, typename WriteHandler>
253 void async_send(const ConstBufferSequence& buffers, WriteHandler handler)
255 this->service.async_send(this->implementation, buffers, 0, handler);
258 /// Start an asynchronous send on a connected socket.
260 * This function is used to send data on the datagram socket. The function
261 * call will block until the data has been sent successfully or an error
262 * occurs.
264 * @param buffers One or more data buffers to be sent on the socket. Although
265 * the buffers object may be copied as necessary, ownership of the underlying
266 * memory blocks is retained by the caller, which must guarantee that they
267 * remain valid until the handler is called.
269 * @param flags Flags specifying how the send call is to be made.
271 * @param handler The handler to be called when the send operation completes.
272 * Copies will be made of the handler as required. The function signature of
273 * the handler must be:
274 * @code void handler(
275 * const boost::system::error_code& error, // Result of operation.
276 * std::size_t bytes_transferred // Number of bytes sent.
277 * ); @endcode
278 * Regardless of whether the asynchronous operation completes immediately or
279 * not, the handler will not be invoked from within this function. Invocation
280 * of the handler will be performed in a manner equivalent to using
281 * boost::asio::io_service::post().
283 * @note The async_send operation can only be used with a connected socket.
284 * Use the async_send_to function to send data on an unconnected datagram
285 * socket.
287 template <typename ConstBufferSequence, typename WriteHandler>
288 void async_send(const ConstBufferSequence& buffers,
289 socket_base::message_flags flags, WriteHandler handler)
291 this->service.async_send(this->implementation, buffers, flags, handler);
294 /// Send a datagram to the specified endpoint.
296 * This function is used to send a datagram to the specified remote endpoint.
297 * The function call will block until the data has been sent successfully or
298 * an error occurs.
300 * @param buffers One or more data buffers to be sent to the remote endpoint.
302 * @param destination The remote endpoint to which the data will be sent.
304 * @returns The number of bytes sent.
306 * @throws boost::system::system_error Thrown on failure.
308 * @par Example
309 * To send a single data buffer use the @ref buffer function as follows:
310 * @code
311 * boost::asio::ip::udp::endpoint destination(
312 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
313 * socket.send_to(boost::asio::buffer(data, size), destination);
314 * @endcode
315 * See the @ref buffer documentation for information on sending multiple
316 * buffers in one go, and how to use it with arrays, boost::array or
317 * std::vector.
319 template <typename ConstBufferSequence>
320 std::size_t send_to(const ConstBufferSequence& buffers,
321 const endpoint_type& destination)
323 boost::system::error_code ec;
324 std::size_t s = this->service.send_to(
325 this->implementation, buffers, destination, 0, ec);
326 boost::asio::detail::throw_error(ec);
327 return s;
330 /// Send a datagram to the specified endpoint.
332 * This function is used to send a datagram to the specified remote endpoint.
333 * The function call will block until the data has been sent successfully or
334 * an error occurs.
336 * @param buffers One or more data buffers to be sent to the remote endpoint.
338 * @param destination The remote endpoint to which the data will be sent.
340 * @param flags Flags specifying how the send call is to be made.
342 * @returns The number of bytes sent.
344 * @throws boost::system::system_error Thrown on failure.
346 template <typename ConstBufferSequence>
347 std::size_t send_to(const ConstBufferSequence& buffers,
348 const endpoint_type& destination, socket_base::message_flags flags)
350 boost::system::error_code ec;
351 std::size_t s = this->service.send_to(
352 this->implementation, buffers, destination, flags, ec);
353 boost::asio::detail::throw_error(ec);
354 return s;
357 /// Send a datagram to the specified endpoint.
359 * This function is used to send a datagram to the specified remote endpoint.
360 * The function call will block until the data has been sent successfully or
361 * an error occurs.
363 * @param buffers One or more data buffers to be sent to the remote endpoint.
365 * @param destination The remote endpoint to which the data will be sent.
367 * @param flags Flags specifying how the send call is to be made.
369 * @param ec Set to indicate what error occurred, if any.
371 * @returns The number of bytes sent.
373 template <typename ConstBufferSequence>
374 std::size_t send_to(const ConstBufferSequence& buffers,
375 const endpoint_type& destination, socket_base::message_flags flags,
376 boost::system::error_code& ec)
378 return this->service.send_to(this->implementation,
379 buffers, destination, flags, ec);
382 /// Start an asynchronous send.
384 * This function is used to asynchronously send a datagram to the specified
385 * remote endpoint. The function call always returns immediately.
387 * @param buffers One or more data buffers to be sent to the remote endpoint.
388 * Although the buffers object may be copied as necessary, ownership of the
389 * underlying memory blocks is retained by the caller, which must guarantee
390 * that they remain valid until the handler is called.
392 * @param destination The remote endpoint to which the data will be sent.
393 * Copies will be made of the endpoint as required.
395 * @param handler The handler to be called when the send operation completes.
396 * Copies will be made of the handler as required. The function signature of
397 * the handler must be:
398 * @code void handler(
399 * const boost::system::error_code& error, // Result of operation.
400 * std::size_t bytes_transferred // Number of bytes sent.
401 * ); @endcode
402 * Regardless of whether the asynchronous operation completes immediately or
403 * not, the handler will not be invoked from within this function. Invocation
404 * of the handler will be performed in a manner equivalent to using
405 * boost::asio::io_service::post().
407 * @par Example
408 * To send a single data buffer use the @ref buffer function as follows:
409 * @code
410 * boost::asio::ip::udp::endpoint destination(
411 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
412 * socket.async_send_to(
413 * boost::asio::buffer(data, size), destination, handler);
414 * @endcode
415 * See the @ref buffer documentation for information on sending multiple
416 * buffers in one go, and how to use it with arrays, boost::array or
417 * std::vector.
419 template <typename ConstBufferSequence, typename WriteHandler>
420 void async_send_to(const ConstBufferSequence& buffers,
421 const endpoint_type& destination, WriteHandler handler)
423 this->service.async_send_to(this->implementation, buffers, destination, 0,
424 handler);
427 /// Start an asynchronous send.
429 * This function is used to asynchronously send a datagram to the specified
430 * remote endpoint. The function call always returns immediately.
432 * @param buffers One or more data buffers to be sent to the remote endpoint.
433 * Although the buffers object may be copied as necessary, ownership of the
434 * underlying memory blocks is retained by the caller, which must guarantee
435 * that they remain valid until the handler is called.
437 * @param flags Flags specifying how the send call is to be made.
439 * @param destination The remote endpoint to which the data will be sent.
440 * Copies will be made of the endpoint as required.
442 * @param handler The handler to be called when the send operation completes.
443 * Copies will be made of the handler as required. The function signature of
444 * the handler must be:
445 * @code void handler(
446 * const boost::system::error_code& error, // Result of operation.
447 * std::size_t bytes_transferred // Number of bytes sent.
448 * ); @endcode
449 * Regardless of whether the asynchronous operation completes immediately or
450 * not, the handler will not be invoked from within this function. Invocation
451 * of the handler will be performed in a manner equivalent to using
452 * boost::asio::io_service::post().
454 template <typename ConstBufferSequence, typename WriteHandler>
455 void async_send_to(const ConstBufferSequence& buffers,
456 const endpoint_type& destination, socket_base::message_flags flags,
457 WriteHandler handler)
459 this->service.async_send_to(this->implementation, buffers, destination,
460 flags, handler);
463 /// Receive some data on a connected socket.
465 * This function is used to receive data on the datagram socket. The function
466 * call will block until data has been received successfully or an error
467 * occurs.
469 * @param buffers One or more buffers into which the data will be received.
471 * @returns The number of bytes received.
473 * @throws boost::system::system_error Thrown on failure.
475 * @note The receive operation can only be used with a connected socket. Use
476 * the receive_from function to receive data on an unconnected datagram
477 * socket.
479 * @par Example
480 * To receive into a single data buffer use the @ref buffer function as
481 * follows:
482 * @code socket.receive(boost::asio::buffer(data, size)); @endcode
483 * See the @ref buffer documentation for information on receiving into
484 * multiple buffers in one go, and how to use it with arrays, boost::array or
485 * std::vector.
487 template <typename MutableBufferSequence>
488 std::size_t receive(const MutableBufferSequence& buffers)
490 boost::system::error_code ec;
491 std::size_t s = this->service.receive(
492 this->implementation, buffers, 0, ec);
493 boost::asio::detail::throw_error(ec);
494 return s;
497 /// Receive some data on a connected socket.
499 * This function is used to receive data on the datagram socket. The function
500 * call will block until data has been received successfully or an error
501 * occurs.
503 * @param buffers One or more buffers into which the data will be received.
505 * @param flags Flags specifying how the receive call is to be made.
507 * @returns The number of bytes received.
509 * @throws boost::system::system_error Thrown on failure.
511 * @note The receive operation can only be used with a connected socket. Use
512 * the receive_from function to receive data on an unconnected datagram
513 * socket.
515 template <typename MutableBufferSequence>
516 std::size_t receive(const MutableBufferSequence& buffers,
517 socket_base::message_flags flags)
519 boost::system::error_code ec;
520 std::size_t s = this->service.receive(
521 this->implementation, buffers, flags, ec);
522 boost::asio::detail::throw_error(ec);
523 return s;
526 /// Receive some data on a connected socket.
528 * This function is used to receive data on the datagram socket. The function
529 * call will block until data has been received successfully or an error
530 * occurs.
532 * @param buffers One or more buffers into which the data will be received.
534 * @param flags Flags specifying how the receive call is to be made.
536 * @param ec Set to indicate what error occurred, if any.
538 * @returns The number of bytes received.
540 * @note The receive operation can only be used with a connected socket. Use
541 * the receive_from function to receive data on an unconnected datagram
542 * socket.
544 template <typename MutableBufferSequence>
545 std::size_t receive(const MutableBufferSequence& buffers,
546 socket_base::message_flags flags, boost::system::error_code& ec)
548 return this->service.receive(this->implementation, buffers, flags, ec);
551 /// Start an asynchronous receive on a connected socket.
553 * This function is used to asynchronously receive data from the datagram
554 * socket. The function call always returns immediately.
556 * @param buffers One or more buffers into which the data will be received.
557 * Although the buffers object may be copied as necessary, ownership of the
558 * underlying memory blocks is retained by the caller, which must guarantee
559 * that they remain valid until the handler is called.
561 * @param handler The handler to be called when the receive operation
562 * completes. Copies will be made of the handler as required. The function
563 * signature of the handler must be:
564 * @code void handler(
565 * const boost::system::error_code& error, // Result of operation.
566 * std::size_t bytes_transferred // Number of bytes received.
567 * ); @endcode
568 * Regardless of whether the asynchronous operation completes immediately or
569 * not, the handler will not be invoked from within this function. Invocation
570 * of the handler will be performed in a manner equivalent to using
571 * boost::asio::io_service::post().
573 * @note The async_receive operation can only be used with a connected socket.
574 * Use the async_receive_from function to receive data on an unconnected
575 * datagram socket.
577 * @par Example
578 * To receive into a single data buffer use the @ref buffer function as
579 * follows:
580 * @code
581 * socket.async_receive(boost::asio::buffer(data, size), handler);
582 * @endcode
583 * See the @ref buffer documentation for information on receiving into
584 * multiple buffers in one go, and how to use it with arrays, boost::array or
585 * std::vector.
587 template <typename MutableBufferSequence, typename ReadHandler>
588 void async_receive(const MutableBufferSequence& buffers, ReadHandler handler)
590 this->service.async_receive(this->implementation, buffers, 0, handler);
593 /// Start an asynchronous receive on a connected socket.
595 * This function is used to asynchronously receive data from the datagram
596 * socket. The function call always returns immediately.
598 * @param buffers One or more buffers into which the data will be received.
599 * Although the buffers object may be copied as necessary, ownership of the
600 * underlying memory blocks is retained by the caller, which must guarantee
601 * that they remain valid until the handler is called.
603 * @param flags Flags specifying how the receive call is to be made.
605 * @param handler The handler to be called when the receive operation
606 * completes. Copies will be made of the handler as required. The function
607 * signature of the handler must be:
608 * @code void handler(
609 * const boost::system::error_code& error, // Result of operation.
610 * std::size_t bytes_transferred // Number of bytes received.
611 * ); @endcode
612 * Regardless of whether the asynchronous operation completes immediately or
613 * not, the handler will not be invoked from within this function. Invocation
614 * of the handler will be performed in a manner equivalent to using
615 * boost::asio::io_service::post().
617 * @note The async_receive operation can only be used with a connected socket.
618 * Use the async_receive_from function to receive data on an unconnected
619 * datagram socket.
621 template <typename MutableBufferSequence, typename ReadHandler>
622 void async_receive(const MutableBufferSequence& buffers,
623 socket_base::message_flags flags, ReadHandler handler)
625 this->service.async_receive(this->implementation, buffers, flags, handler);
628 /// Receive a datagram with the endpoint of the sender.
630 * This function is used to receive a datagram. The function call will block
631 * until data has been received successfully or an error occurs.
633 * @param buffers One or more buffers into which the data will be received.
635 * @param sender_endpoint An endpoint object that receives the endpoint of
636 * the remote sender of the datagram.
638 * @returns The number of bytes received.
640 * @throws boost::system::system_error Thrown on failure.
642 * @par Example
643 * To receive into a single data buffer use the @ref buffer function as
644 * follows:
645 * @code
646 * boost::asio::ip::udp::endpoint sender_endpoint;
647 * socket.receive_from(
648 * boost::asio::buffer(data, size), sender_endpoint);
649 * @endcode
650 * See the @ref buffer documentation for information on receiving into
651 * multiple buffers in one go, and how to use it with arrays, boost::array or
652 * std::vector.
654 template <typename MutableBufferSequence>
655 std::size_t receive_from(const MutableBufferSequence& buffers,
656 endpoint_type& sender_endpoint)
658 boost::system::error_code ec;
659 std::size_t s = this->service.receive_from(
660 this->implementation, buffers, sender_endpoint, 0, ec);
661 boost::asio::detail::throw_error(ec);
662 return s;
665 /// Receive a datagram with the endpoint of the sender.
667 * This function is used to receive a datagram. The function call will block
668 * until data has been received successfully or an error occurs.
670 * @param buffers One or more buffers into which the data will be received.
672 * @param sender_endpoint An endpoint object that receives the endpoint of
673 * the remote sender of the datagram.
675 * @param flags Flags specifying how the receive call is to be made.
677 * @returns The number of bytes received.
679 * @throws boost::system::system_error Thrown on failure.
681 template <typename MutableBufferSequence>
682 std::size_t receive_from(const MutableBufferSequence& buffers,
683 endpoint_type& sender_endpoint, socket_base::message_flags flags)
685 boost::system::error_code ec;
686 std::size_t s = this->service.receive_from(
687 this->implementation, buffers, sender_endpoint, flags, ec);
688 boost::asio::detail::throw_error(ec);
689 return s;
692 /// Receive a datagram with the endpoint of the sender.
694 * This function is used to receive a datagram. The function call will block
695 * until data has been received successfully or an error occurs.
697 * @param buffers One or more buffers into which the data will be received.
699 * @param sender_endpoint An endpoint object that receives the endpoint of
700 * the remote sender of the datagram.
702 * @param flags Flags specifying how the receive call is to be made.
704 * @param ec Set to indicate what error occurred, if any.
706 * @returns The number of bytes received.
708 template <typename MutableBufferSequence>
709 std::size_t receive_from(const MutableBufferSequence& buffers,
710 endpoint_type& sender_endpoint, socket_base::message_flags flags,
711 boost::system::error_code& ec)
713 return this->service.receive_from(this->implementation, buffers,
714 sender_endpoint, flags, ec);
717 /// Start an asynchronous receive.
719 * This function is used to asynchronously receive a datagram. The function
720 * call always returns immediately.
722 * @param buffers One or more buffers into which the data will be received.
723 * Although the buffers object may be copied as necessary, ownership of the
724 * underlying memory blocks is retained by the caller, which must guarantee
725 * that they remain valid until the handler is called.
727 * @param sender_endpoint An endpoint object that receives the endpoint of
728 * the remote sender of the datagram. Ownership of the sender_endpoint object
729 * is retained by the caller, which must guarantee that it is valid until the
730 * handler is called.
732 * @param handler The handler to be called when the receive operation
733 * completes. Copies will be made of the handler as required. The function
734 * signature of the handler must be:
735 * @code void handler(
736 * const boost::system::error_code& error, // Result of operation.
737 * std::size_t bytes_transferred // Number of bytes received.
738 * ); @endcode
739 * Regardless of whether the asynchronous operation completes immediately or
740 * not, the handler will not be invoked from within this function. Invocation
741 * of the handler will be performed in a manner equivalent to using
742 * boost::asio::io_service::post().
744 * @par Example
745 * To receive into a single data buffer use the @ref buffer function as
746 * follows:
747 * @code socket.async_receive_from(
748 * boost::asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
749 * See the @ref buffer documentation for information on receiving into
750 * multiple buffers in one go, and how to use it with arrays, boost::array or
751 * std::vector.
753 template <typename MutableBufferSequence, typename ReadHandler>
754 void async_receive_from(const MutableBufferSequence& buffers,
755 endpoint_type& sender_endpoint, ReadHandler handler)
757 this->service.async_receive_from(this->implementation, buffers,
758 sender_endpoint, 0, handler);
761 /// Start an asynchronous receive.
763 * This function is used to asynchronously receive a datagram. The function
764 * call always returns immediately.
766 * @param buffers One or more buffers into which the data will be received.
767 * Although the buffers object may be copied as necessary, ownership of the
768 * underlying memory blocks is retained by the caller, which must guarantee
769 * that they remain valid until the handler is called.
771 * @param sender_endpoint An endpoint object that receives the endpoint of
772 * the remote sender of the datagram. Ownership of the sender_endpoint object
773 * is retained by the caller, which must guarantee that it is valid until the
774 * handler is called.
776 * @param flags Flags specifying how the receive call is to be made.
778 * @param handler The handler to be called when the receive operation
779 * completes. Copies will be made of the handler as required. The function
780 * signature of the handler must be:
781 * @code void handler(
782 * const boost::system::error_code& error, // Result of operation.
783 * std::size_t bytes_transferred // Number of bytes received.
784 * ); @endcode
785 * Regardless of whether the asynchronous operation completes immediately or
786 * not, the handler will not be invoked from within this function. Invocation
787 * of the handler will be performed in a manner equivalent to using
788 * boost::asio::io_service::post().
790 template <typename MutableBufferSequence, typename ReadHandler>
791 void async_receive_from(const MutableBufferSequence& buffers,
792 endpoint_type& sender_endpoint, socket_base::message_flags flags,
793 ReadHandler handler)
795 this->service.async_receive_from(this->implementation, buffers,
796 sender_endpoint, flags, handler);
800 } // namespace asio
801 } // namespace boost
803 #include <boost/asio/detail/pop_options.hpp>
805 #endif // BOOST_ASIO_BASIC_DATAGRAM_SOCKET_HPP