5 // Copyright (c) 2003-2022 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_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_SOCKET_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/any_io_executor.hpp>
19 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/detail/handler_type_requirements.hpp>
22 #include <boost/asio/detail/io_object_impl.hpp>
23 #include <boost/asio/detail/non_const_lvalue.hpp>
24 #include <boost/asio/detail/throw_error.hpp>
25 #include <boost/asio/detail/type_traits.hpp>
26 #include <boost/asio/error.hpp>
27 #include <boost/asio/execution_context.hpp>
28 #include <boost/asio/post.hpp>
29 #include <boost/asio/socket_base.hpp>
31 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
32 # include <boost/asio/detail/null_socket_service.hpp>
33 #elif defined(BOOST_ASIO_HAS_IOCP)
34 # include <boost/asio/detail/win_iocp_socket_service.hpp>
35 #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
36 # include <boost/asio/detail/io_uring_socket_service.hpp>
38 # include <boost/asio/detail/reactive_socket_service.hpp>
41 #if defined(BOOST_ASIO_HAS_MOVE)
43 #endif // defined(BOOST_ASIO_HAS_MOVE)
45 #include <boost/asio/detail/push_options.hpp>
50 #if !defined(BOOST_ASIO_BASIC_SOCKET_FWD_DECL)
51 #define BOOST_ASIO_BASIC_SOCKET_FWD_DECL
53 // Forward declaration with defaulted arguments.
54 template <typename Protocol, typename Executor = any_io_executor>
57 #endif // !defined(BOOST_ASIO_BASIC_SOCKET_FWD_DECL)
59 /// Provides socket functionality.
61 * The basic_socket class template provides functionality that is common to both
62 * stream-oriented and datagram-oriented sockets.
65 * @e Distinct @e objects: Safe.@n
66 * @e Shared @e objects: Unsafe.
68 template <typename Protocol, typename Executor>
73 /// The type of the executor associated with the object.
74 typedef Executor executor_type;
76 /// Rebinds the socket type to another executor.
77 template <typename Executor1>
78 struct rebind_executor
80 /// The socket type when rebound to the specified executor.
81 typedef basic_socket<Protocol, Executor1> other;
84 /// The native representation of a socket.
85 #if defined(GENERATING_DOCUMENTATION)
86 typedef implementation_defined native_handle_type;
87 #elif defined(BOOST_ASIO_WINDOWS_RUNTIME)
88 typedef typename detail::null_socket_service<
89 Protocol>::native_handle_type native_handle_type;
90 #elif defined(BOOST_ASIO_HAS_IOCP)
91 typedef typename detail::win_iocp_socket_service<
92 Protocol>::native_handle_type native_handle_type;
93 #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
94 typedef typename detail::io_uring_socket_service<
95 Protocol>::native_handle_type native_handle_type;
97 typedef typename detail::reactive_socket_service<
98 Protocol>::native_handle_type native_handle_type;
101 /// The protocol type.
102 typedef Protocol protocol_type;
104 /// The endpoint type.
105 typedef typename Protocol::endpoint endpoint_type;
107 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
108 /// A basic_socket is always the lowest layer.
109 typedef basic_socket<Protocol, Executor> lowest_layer_type;
110 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
112 /// Construct a basic_socket without opening it.
114 * This constructor creates a socket without opening it.
116 * @param ex The I/O executor that the socket will use, by default, to
117 * dispatch handlers for any asynchronous operations performed on the socket.
119 explicit basic_socket(const executor_type& ex)
124 /// Construct a basic_socket without opening it.
126 * This constructor creates a socket without opening it.
128 * @param context An execution context which provides the I/O executor that
129 * the socket will use, by default, to dispatch handlers for any asynchronous
130 * operations performed on the socket.
132 template <typename ExecutionContext>
133 explicit basic_socket(ExecutionContext& context,
135 is_convertible<ExecutionContext&, execution_context&>::value
137 : impl_(0, 0, context)
141 /// Construct and open a basic_socket.
143 * This constructor creates and opens a socket.
145 * @param ex The I/O executor that the socket will use, by default, to
146 * dispatch handlers for any asynchronous operations performed on the socket.
148 * @param protocol An object specifying protocol parameters to be used.
150 * @throws boost::system::system_error Thrown on failure.
152 basic_socket(const executor_type& ex, const protocol_type& protocol)
155 boost::system::error_code ec;
156 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
157 boost::asio::detail::throw_error(ec, "open");
160 /// Construct and open a basic_socket.
162 * This constructor creates and opens a socket.
164 * @param context An execution context which provides the I/O executor that
165 * the socket will use, by default, to dispatch handlers for any asynchronous
166 * operations performed on the socket.
168 * @param protocol An object specifying protocol parameters to be used.
170 * @throws boost::system::system_error Thrown on failure.
172 template <typename ExecutionContext>
173 basic_socket(ExecutionContext& context, const protocol_type& protocol,
175 is_convertible<ExecutionContext&, execution_context&>::value,
177 >::type = defaulted_constraint())
178 : impl_(0, 0, context)
180 boost::system::error_code ec;
181 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
182 boost::asio::detail::throw_error(ec, "open");
185 /// Construct a basic_socket, opening it and binding it to the given local
188 * This constructor creates a socket and automatically opens it bound to the
189 * specified endpoint on the local machine. The protocol used is the protocol
190 * associated with the given endpoint.
192 * @param ex The I/O executor that the socket will use, by default, to
193 * dispatch handlers for any asynchronous operations performed on the socket.
195 * @param endpoint An endpoint on the local machine to which the socket will
198 * @throws boost::system::system_error Thrown on failure.
200 basic_socket(const executor_type& ex, const endpoint_type& endpoint)
203 boost::system::error_code ec;
204 const protocol_type protocol = endpoint.protocol();
205 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
206 boost::asio::detail::throw_error(ec, "open");
207 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
208 boost::asio::detail::throw_error(ec, "bind");
211 /// Construct a basic_socket, opening it and binding it to the given local
214 * This constructor creates a socket and automatically opens it bound to the
215 * specified endpoint on the local machine. The protocol used is the protocol
216 * associated with the given endpoint.
218 * @param context An execution context which provides the I/O executor that
219 * the socket will use, by default, to dispatch handlers for any asynchronous
220 * operations performed on the socket.
222 * @param endpoint An endpoint on the local machine to which the socket will
225 * @throws boost::system::system_error Thrown on failure.
227 template <typename ExecutionContext>
228 basic_socket(ExecutionContext& context, const endpoint_type& endpoint,
230 is_convertible<ExecutionContext&, execution_context&>::value
232 : impl_(0, 0, context)
234 boost::system::error_code ec;
235 const protocol_type protocol = endpoint.protocol();
236 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
237 boost::asio::detail::throw_error(ec, "open");
238 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
239 boost::asio::detail::throw_error(ec, "bind");
242 /// Construct a basic_socket on an existing native socket.
244 * This constructor creates a socket object to hold an existing native socket.
246 * @param ex The I/O executor that the socket will use, by default, to
247 * dispatch handlers for any asynchronous operations performed on the socket.
249 * @param protocol An object specifying protocol parameters to be used.
251 * @param native_socket A native socket.
253 * @throws boost::system::system_error Thrown on failure.
255 basic_socket(const executor_type& ex, const protocol_type& protocol,
256 const native_handle_type& native_socket)
259 boost::system::error_code ec;
260 impl_.get_service().assign(impl_.get_implementation(),
261 protocol, native_socket, ec);
262 boost::asio::detail::throw_error(ec, "assign");
265 /// Construct a basic_socket on an existing native socket.
267 * This constructor creates a socket object to hold an existing native socket.
269 * @param context An execution context which provides the I/O executor that
270 * the socket will use, by default, to dispatch handlers for any asynchronous
271 * operations performed on the socket.
273 * @param protocol An object specifying protocol parameters to be used.
275 * @param native_socket A native socket.
277 * @throws boost::system::system_error Thrown on failure.
279 template <typename ExecutionContext>
280 basic_socket(ExecutionContext& context, const protocol_type& protocol,
281 const native_handle_type& native_socket,
283 is_convertible<ExecutionContext&, execution_context&>::value
285 : impl_(0, 0, context)
287 boost::system::error_code ec;
288 impl_.get_service().assign(impl_.get_implementation(),
289 protocol, native_socket, ec);
290 boost::asio::detail::throw_error(ec, "assign");
293 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
294 /// Move-construct a basic_socket from another.
296 * This constructor moves a socket from one object to another.
298 * @param other The other basic_socket object from which the move will
301 * @note Following the move, the moved-from object is in the same state as if
302 * constructed using the @c basic_socket(const executor_type&) constructor.
304 basic_socket(basic_socket&& other) BOOST_ASIO_NOEXCEPT
305 : impl_(std::move(other.impl_))
309 /// Move-assign a basic_socket from another.
311 * This assignment operator moves a socket from one object to another.
313 * @param other The other basic_socket object from which the move will
316 * @note Following the move, the moved-from object is in the same state as if
317 * constructed using the @c basic_socket(const executor_type&) constructor.
319 basic_socket& operator=(basic_socket&& other)
321 impl_ = std::move(other.impl_);
325 // All sockets have access to each other's implementations.
326 template <typename Protocol1, typename Executor1>
327 friend class basic_socket;
329 /// Move-construct a basic_socket from a socket of another protocol type.
331 * This constructor moves a socket from one object to another.
333 * @param other The other basic_socket object from which the move will
336 * @note Following the move, the moved-from object is in the same state as if
337 * constructed using the @c basic_socket(const executor_type&) constructor.
339 template <typename Protocol1, typename Executor1>
340 basic_socket(basic_socket<Protocol1, Executor1>&& other,
342 is_convertible<Protocol1, Protocol>::value
343 && is_convertible<Executor1, Executor>::value
345 : impl_(std::move(other.impl_))
349 /// Move-assign a basic_socket from a socket of another protocol type.
351 * This assignment operator moves a socket from one object to another.
353 * @param other The other basic_socket object from which the move will
356 * @note Following the move, the moved-from object is in the same state as if
357 * constructed using the @c basic_socket(const executor_type&) constructor.
359 template <typename Protocol1, typename Executor1>
361 is_convertible<Protocol1, Protocol>::value
362 && is_convertible<Executor1, Executor>::value,
364 >::type operator=(basic_socket<Protocol1, Executor1> && other)
366 basic_socket tmp(std::move(other));
367 impl_ = std::move(tmp.impl_);
370 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
372 /// Get the executor associated with the object.
373 executor_type get_executor() BOOST_ASIO_NOEXCEPT
375 return impl_.get_executor();
378 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
379 /// Get a reference to the lowest layer.
381 * This function returns a reference to the lowest layer in a stack of
382 * layers. Since a basic_socket cannot contain any further layers, it simply
383 * returns a reference to itself.
385 * @return A reference to the lowest layer in the stack of layers. Ownership
386 * is not transferred to the caller.
388 lowest_layer_type& lowest_layer()
393 /// Get a const reference to the lowest layer.
395 * This function returns a const reference to the lowest layer in a stack of
396 * layers. Since a basic_socket cannot contain any further layers, it simply
397 * returns a reference to itself.
399 * @return A const reference to the lowest layer in the stack of layers.
400 * Ownership is not transferred to the caller.
402 const lowest_layer_type& lowest_layer() const
406 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
408 /// Open the socket using the specified protocol.
410 * This function opens the socket so that it will use the specified protocol.
412 * @param protocol An object specifying protocol parameters to be used.
414 * @throws boost::system::system_error Thrown on failure.
418 * boost::asio::ip::tcp::socket socket(my_context);
419 * socket.open(boost::asio::ip::tcp::v4());
422 void open(const protocol_type& protocol = protocol_type())
424 boost::system::error_code ec;
425 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
426 boost::asio::detail::throw_error(ec, "open");
429 /// Open the socket using the specified protocol.
431 * This function opens the socket so that it will use the specified protocol.
433 * @param protocol An object specifying which protocol is to be used.
435 * @param ec Set to indicate what error occurred, if any.
439 * boost::asio::ip::tcp::socket socket(my_context);
440 * boost::system::error_code ec;
441 * socket.open(boost::asio::ip::tcp::v4(), ec);
444 * // An error occurred.
448 BOOST_ASIO_SYNC_OP_VOID open(const protocol_type& protocol,
449 boost::system::error_code& ec)
451 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
452 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
455 /// Assign an existing native socket to the socket.
457 * This function opens the socket to hold an existing native socket.
459 * @param protocol An object specifying which protocol is to be used.
461 * @param native_socket A native socket.
463 * @throws boost::system::system_error Thrown on failure.
465 void assign(const protocol_type& protocol,
466 const native_handle_type& native_socket)
468 boost::system::error_code ec;
469 impl_.get_service().assign(impl_.get_implementation(),
470 protocol, native_socket, ec);
471 boost::asio::detail::throw_error(ec, "assign");
474 /// Assign an existing native socket to the socket.
476 * This function opens the socket to hold an existing native socket.
478 * @param protocol An object specifying which protocol is to be used.
480 * @param native_socket A native socket.
482 * @param ec Set to indicate what error occurred, if any.
484 BOOST_ASIO_SYNC_OP_VOID assign(const protocol_type& protocol,
485 const native_handle_type& native_socket, boost::system::error_code& ec)
487 impl_.get_service().assign(impl_.get_implementation(),
488 protocol, native_socket, ec);
489 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
492 /// Determine whether the socket is open.
495 return impl_.get_service().is_open(impl_.get_implementation());
498 /// Close the socket.
500 * This function is used to close the socket. Any asynchronous send, receive
501 * or connect operations will be cancelled immediately, and will complete
502 * with the boost::asio::error::operation_aborted error.
504 * @throws boost::system::system_error Thrown on failure. Note that, even if
505 * the function indicates an error, the underlying descriptor is closed.
507 * @note For portable behaviour with respect to graceful closure of a
508 * connected socket, call shutdown() before closing the socket.
512 boost::system::error_code ec;
513 impl_.get_service().close(impl_.get_implementation(), ec);
514 boost::asio::detail::throw_error(ec, "close");
517 /// Close the socket.
519 * This function is used to close the socket. Any asynchronous send, receive
520 * or connect operations will be cancelled immediately, and will complete
521 * with the boost::asio::error::operation_aborted error.
523 * @param ec Set to indicate what error occurred, if any. Note that, even if
524 * the function indicates an error, the underlying descriptor is closed.
528 * boost::asio::ip::tcp::socket socket(my_context);
530 * boost::system::error_code ec;
534 * // An error occurred.
538 * @note For portable behaviour with respect to graceful closure of a
539 * connected socket, call shutdown() before closing the socket.
541 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
543 impl_.get_service().close(impl_.get_implementation(), ec);
544 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
547 /// Release ownership of the underlying native socket.
549 * This function causes all outstanding asynchronous connect, send and receive
550 * operations to finish immediately, and the handlers for cancelled operations
551 * will be passed the boost::asio::error::operation_aborted error. Ownership
552 * of the native socket is then transferred to the caller.
554 * @throws boost::system::system_error Thrown on failure.
556 * @note This function is unsupported on Windows versions prior to Windows
557 * 8.1, and will fail with boost::asio::error::operation_not_supported on
560 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
561 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
562 __declspec(deprecated("This function always fails with "
563 "operation_not_supported when used on Windows versions "
564 "prior to Windows 8.1."))
566 native_handle_type release()
568 boost::system::error_code ec;
569 native_handle_type s = impl_.get_service().release(
570 impl_.get_implementation(), ec);
571 boost::asio::detail::throw_error(ec, "release");
575 /// Release ownership of the underlying native socket.
577 * This function causes all outstanding asynchronous connect, send and receive
578 * operations to finish immediately, and the handlers for cancelled operations
579 * will be passed the boost::asio::error::operation_aborted error. Ownership
580 * of the native socket is then transferred to the caller.
582 * @param ec Set to indicate what error occurred, if any.
584 * @note This function is unsupported on Windows versions prior to Windows
585 * 8.1, and will fail with boost::asio::error::operation_not_supported on
588 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
589 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
590 __declspec(deprecated("This function always fails with "
591 "operation_not_supported when used on Windows versions "
592 "prior to Windows 8.1."))
594 native_handle_type release(boost::system::error_code& ec)
596 return impl_.get_service().release(impl_.get_implementation(), ec);
599 /// Get the native socket representation.
601 * This function may be used to obtain the underlying representation of the
602 * socket. This is intended to allow access to native socket functionality
603 * that is not otherwise provided.
605 native_handle_type native_handle()
607 return impl_.get_service().native_handle(impl_.get_implementation());
610 /// Cancel all asynchronous operations associated with the socket.
612 * This function causes all outstanding asynchronous connect, send and receive
613 * operations to finish immediately, and the handlers for cancelled operations
614 * will be passed the boost::asio::error::operation_aborted error.
616 * @throws boost::system::system_error Thrown on failure.
618 * @note Calls to cancel() will always fail with
619 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
620 * Server 2003, and earlier versions of Windows, unless
621 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
622 * two issues that should be considered before enabling its use:
624 * @li It will only cancel asynchronous operations that were initiated in the
627 * @li It can appear to complete without error, but the request to cancel the
628 * unfinished operations may be silently ignored by the operating system.
629 * Whether it works or not seems to depend on the drivers that are installed.
631 * For portable cancellation, consider using one of the following
634 * @li Disable asio's I/O completion port backend by defining
635 * BOOST_ASIO_DISABLE_IOCP.
637 * @li Use the close() function to simultaneously cancel the outstanding
638 * operations and close the socket.
640 * When running on Windows Vista, Windows Server 2008, and later, the
641 * CancelIoEx function is always used. This function does not have the
642 * problems described above.
644 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
645 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
646 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
647 __declspec(deprecated("By default, this function always fails with "
648 "operation_not_supported when used on Windows XP, Windows Server 2003, "
649 "or earlier. Consult documentation for details."))
653 boost::system::error_code ec;
654 impl_.get_service().cancel(impl_.get_implementation(), ec);
655 boost::asio::detail::throw_error(ec, "cancel");
658 /// Cancel all asynchronous operations associated with the socket.
660 * This function causes all outstanding asynchronous connect, send and receive
661 * operations to finish immediately, and the handlers for cancelled operations
662 * will be passed the boost::asio::error::operation_aborted error.
664 * @param ec Set to indicate what error occurred, if any.
666 * @note Calls to cancel() will always fail with
667 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
668 * Server 2003, and earlier versions of Windows, unless
669 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
670 * two issues that should be considered before enabling its use:
672 * @li It will only cancel asynchronous operations that were initiated in the
675 * @li It can appear to complete without error, but the request to cancel the
676 * unfinished operations may be silently ignored by the operating system.
677 * Whether it works or not seems to depend on the drivers that are installed.
679 * For portable cancellation, consider using one of the following
682 * @li Disable asio's I/O completion port backend by defining
683 * BOOST_ASIO_DISABLE_IOCP.
685 * @li Use the close() function to simultaneously cancel the outstanding
686 * operations and close the socket.
688 * When running on Windows Vista, Windows Server 2008, and later, the
689 * CancelIoEx function is always used. This function does not have the
690 * problems described above.
692 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
693 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
694 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
695 __declspec(deprecated("By default, this function always fails with "
696 "operation_not_supported when used on Windows XP, Windows Server 2003, "
697 "or earlier. Consult documentation for details."))
699 BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
701 impl_.get_service().cancel(impl_.get_implementation(), ec);
702 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
705 /// Determine whether the socket is at the out-of-band data mark.
707 * This function is used to check whether the socket input is currently
708 * positioned at the out-of-band data mark.
710 * @return A bool indicating whether the socket is at the out-of-band data
713 * @throws boost::system::system_error Thrown on failure.
717 boost::system::error_code ec;
718 bool b = impl_.get_service().at_mark(impl_.get_implementation(), ec);
719 boost::asio::detail::throw_error(ec, "at_mark");
723 /// Determine whether the socket is at the out-of-band data mark.
725 * This function is used to check whether the socket input is currently
726 * positioned at the out-of-band data mark.
728 * @param ec Set to indicate what error occurred, if any.
730 * @return A bool indicating whether the socket is at the out-of-band data
733 bool at_mark(boost::system::error_code& ec) const
735 return impl_.get_service().at_mark(impl_.get_implementation(), ec);
738 /// Determine the number of bytes available for reading.
740 * This function is used to determine the number of bytes that may be read
743 * @return The number of bytes that may be read without blocking, or 0 if an
746 * @throws boost::system::system_error Thrown on failure.
748 std::size_t available() const
750 boost::system::error_code ec;
751 std::size_t s = impl_.get_service().available(
752 impl_.get_implementation(), ec);
753 boost::asio::detail::throw_error(ec, "available");
757 /// Determine the number of bytes available for reading.
759 * This function is used to determine the number of bytes that may be read
762 * @param ec Set to indicate what error occurred, if any.
764 * @return The number of bytes that may be read without blocking, or 0 if an
767 std::size_t available(boost::system::error_code& ec) const
769 return impl_.get_service().available(impl_.get_implementation(), ec);
772 /// Bind the socket to the given local endpoint.
774 * This function binds the socket to the specified endpoint on the local
777 * @param endpoint An endpoint on the local machine to which the socket will
780 * @throws boost::system::system_error Thrown on failure.
784 * boost::asio::ip::tcp::socket socket(my_context);
785 * socket.open(boost::asio::ip::tcp::v4());
786 * socket.bind(boost::asio::ip::tcp::endpoint(
787 * boost::asio::ip::tcp::v4(), 12345));
790 void bind(const endpoint_type& endpoint)
792 boost::system::error_code ec;
793 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
794 boost::asio::detail::throw_error(ec, "bind");
797 /// Bind the socket to the given local endpoint.
799 * This function binds the socket to the specified endpoint on the local
802 * @param endpoint An endpoint on the local machine to which the socket will
805 * @param ec Set to indicate what error occurred, if any.
809 * boost::asio::ip::tcp::socket socket(my_context);
810 * socket.open(boost::asio::ip::tcp::v4());
811 * boost::system::error_code ec;
812 * socket.bind(boost::asio::ip::tcp::endpoint(
813 * boost::asio::ip::tcp::v4(), 12345), ec);
816 * // An error occurred.
820 BOOST_ASIO_SYNC_OP_VOID bind(const endpoint_type& endpoint,
821 boost::system::error_code& ec)
823 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
824 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
827 /// Connect the socket to the specified endpoint.
829 * This function is used to connect a socket to the specified remote endpoint.
830 * The function call will block until the connection is successfully made or
833 * The socket is automatically opened if it is not already open. If the
834 * connect fails, and the socket was automatically opened, the socket is
835 * not returned to the closed state.
837 * @param peer_endpoint The remote endpoint to which the socket will be
840 * @throws boost::system::system_error Thrown on failure.
844 * boost::asio::ip::tcp::socket socket(my_context);
845 * boost::asio::ip::tcp::endpoint endpoint(
846 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
847 * socket.connect(endpoint);
850 void connect(const endpoint_type& peer_endpoint)
852 boost::system::error_code ec;
855 impl_.get_service().open(impl_.get_implementation(),
856 peer_endpoint.protocol(), ec);
857 boost::asio::detail::throw_error(ec, "connect");
859 impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
860 boost::asio::detail::throw_error(ec, "connect");
863 /// Connect the socket to the specified endpoint.
865 * This function is used to connect a socket to the specified remote endpoint.
866 * The function call will block until the connection is successfully made or
869 * The socket is automatically opened if it is not already open. If the
870 * connect fails, and the socket was automatically opened, the socket is
871 * not returned to the closed state.
873 * @param peer_endpoint The remote endpoint to which the socket will be
876 * @param ec Set to indicate what error occurred, if any.
880 * boost::asio::ip::tcp::socket socket(my_context);
881 * boost::asio::ip::tcp::endpoint endpoint(
882 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
883 * boost::system::error_code ec;
884 * socket.connect(endpoint, ec);
887 * // An error occurred.
891 BOOST_ASIO_SYNC_OP_VOID connect(const endpoint_type& peer_endpoint,
892 boost::system::error_code& ec)
896 impl_.get_service().open(impl_.get_implementation(),
897 peer_endpoint.protocol(), ec);
900 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
904 impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
905 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
908 /// Start an asynchronous connect.
910 * This function is used to asynchronously connect a socket to the specified
911 * remote endpoint. It is an initiating function for an @ref
912 * asynchronous_operation, and always returns immediately.
914 * The socket is automatically opened if it is not already open. If the
915 * connect fails, and the socket was automatically opened, the socket is
916 * not returned to the closed state.
918 * @param peer_endpoint The remote endpoint to which the socket will be
919 * connected. Copies will be made of the endpoint object as required.
921 * @param token The @ref completion_token that will be used to produce a
922 * completion handler, which will be called when the connect completes.
923 * Potential completion tokens include @ref use_future, @ref use_awaitable,
924 * @ref yield_context, or a function object with the correct completion
925 * signature. The function signature of the completion handler must be:
926 * @code void handler(
927 * const boost::system::error_code& error // Result of operation.
929 * Regardless of whether the asynchronous operation completes immediately or
930 * not, the completion handler will not be invoked from within this function.
931 * On immediate completion, invocation of the handler will be performed in a
932 * manner equivalent to using boost::asio::post().
934 * @par Completion Signature
935 * @code void(boost::system::error_code) @endcode
939 * void connect_handler(const boost::system::error_code& error)
943 * // Connect succeeded.
949 * boost::asio::ip::tcp::socket socket(my_context);
950 * boost::asio::ip::tcp::endpoint endpoint(
951 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
952 * socket.async_connect(endpoint, connect_handler);
955 * @par Per-Operation Cancellation
956 * On POSIX or Windows operating systems, this asynchronous operation supports
957 * cancellation for the following boost::asio::cancellation_type values:
959 * @li @c cancellation_type::terminal
961 * @li @c cancellation_type::partial
963 * @li @c cancellation_type::total
966 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
967 ConnectToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
968 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectToken,
969 void (boost::system::error_code))
970 async_connect(const endpoint_type& peer_endpoint,
971 BOOST_ASIO_MOVE_ARG(ConnectToken) token
972 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
974 boost::system::error_code open_ec;
977 const protocol_type protocol = peer_endpoint.protocol();
978 impl_.get_service().open(impl_.get_implementation(), protocol, open_ec);
981 return async_initiate<ConnectToken, void (boost::system::error_code)>(
982 initiate_async_connect(this), token, peer_endpoint, open_ec);
985 /// Set an option on the socket.
987 * This function is used to set an option on the socket.
989 * @param option The new option value to be set on the socket.
991 * @throws boost::system::system_error Thrown on failure.
993 * @sa SettableSocketOption @n
994 * boost::asio::socket_base::broadcast @n
995 * boost::asio::socket_base::do_not_route @n
996 * boost::asio::socket_base::keep_alive @n
997 * boost::asio::socket_base::linger @n
998 * boost::asio::socket_base::receive_buffer_size @n
999 * boost::asio::socket_base::receive_low_watermark @n
1000 * boost::asio::socket_base::reuse_address @n
1001 * boost::asio::socket_base::send_buffer_size @n
1002 * boost::asio::socket_base::send_low_watermark @n
1003 * boost::asio::ip::multicast::join_group @n
1004 * boost::asio::ip::multicast::leave_group @n
1005 * boost::asio::ip::multicast::enable_loopback @n
1006 * boost::asio::ip::multicast::outbound_interface @n
1007 * boost::asio::ip::multicast::hops @n
1008 * boost::asio::ip::tcp::no_delay
1011 * Setting the IPPROTO_TCP/TCP_NODELAY option:
1013 * boost::asio::ip::tcp::socket socket(my_context);
1015 * boost::asio::ip::tcp::no_delay option(true);
1016 * socket.set_option(option);
1019 template <typename SettableSocketOption>
1020 void set_option(const SettableSocketOption& option)
1022 boost::system::error_code ec;
1023 impl_.get_service().set_option(impl_.get_implementation(), option, ec);
1024 boost::asio::detail::throw_error(ec, "set_option");
1027 /// Set an option on the socket.
1029 * This function is used to set an option on the socket.
1031 * @param option The new option value to be set on the socket.
1033 * @param ec Set to indicate what error occurred, if any.
1035 * @sa SettableSocketOption @n
1036 * boost::asio::socket_base::broadcast @n
1037 * boost::asio::socket_base::do_not_route @n
1038 * boost::asio::socket_base::keep_alive @n
1039 * boost::asio::socket_base::linger @n
1040 * boost::asio::socket_base::receive_buffer_size @n
1041 * boost::asio::socket_base::receive_low_watermark @n
1042 * boost::asio::socket_base::reuse_address @n
1043 * boost::asio::socket_base::send_buffer_size @n
1044 * boost::asio::socket_base::send_low_watermark @n
1045 * boost::asio::ip::multicast::join_group @n
1046 * boost::asio::ip::multicast::leave_group @n
1047 * boost::asio::ip::multicast::enable_loopback @n
1048 * boost::asio::ip::multicast::outbound_interface @n
1049 * boost::asio::ip::multicast::hops @n
1050 * boost::asio::ip::tcp::no_delay
1053 * Setting the IPPROTO_TCP/TCP_NODELAY option:
1055 * boost::asio::ip::tcp::socket socket(my_context);
1057 * boost::asio::ip::tcp::no_delay option(true);
1058 * boost::system::error_code ec;
1059 * socket.set_option(option, ec);
1062 * // An error occurred.
1066 template <typename SettableSocketOption>
1067 BOOST_ASIO_SYNC_OP_VOID set_option(const SettableSocketOption& option,
1068 boost::system::error_code& ec)
1070 impl_.get_service().set_option(impl_.get_implementation(), option, ec);
1071 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1074 /// Get an option from the socket.
1076 * This function is used to get the current value of an option on the socket.
1078 * @param option The option value to be obtained from the socket.
1080 * @throws boost::system::system_error Thrown on failure.
1082 * @sa GettableSocketOption @n
1083 * boost::asio::socket_base::broadcast @n
1084 * boost::asio::socket_base::do_not_route @n
1085 * boost::asio::socket_base::keep_alive @n
1086 * boost::asio::socket_base::linger @n
1087 * boost::asio::socket_base::receive_buffer_size @n
1088 * boost::asio::socket_base::receive_low_watermark @n
1089 * boost::asio::socket_base::reuse_address @n
1090 * boost::asio::socket_base::send_buffer_size @n
1091 * boost::asio::socket_base::send_low_watermark @n
1092 * boost::asio::ip::multicast::join_group @n
1093 * boost::asio::ip::multicast::leave_group @n
1094 * boost::asio::ip::multicast::enable_loopback @n
1095 * boost::asio::ip::multicast::outbound_interface @n
1096 * boost::asio::ip::multicast::hops @n
1097 * boost::asio::ip::tcp::no_delay
1100 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
1102 * boost::asio::ip::tcp::socket socket(my_context);
1104 * boost::asio::ip::tcp::socket::keep_alive option;
1105 * socket.get_option(option);
1106 * bool is_set = option.value();
1109 template <typename GettableSocketOption>
1110 void get_option(GettableSocketOption& option) const
1112 boost::system::error_code ec;
1113 impl_.get_service().get_option(impl_.get_implementation(), option, ec);
1114 boost::asio::detail::throw_error(ec, "get_option");
1117 /// Get an option from the socket.
1119 * This function is used to get the current value of an option on the socket.
1121 * @param option The option value to be obtained from the socket.
1123 * @param ec Set to indicate what error occurred, if any.
1125 * @sa GettableSocketOption @n
1126 * boost::asio::socket_base::broadcast @n
1127 * boost::asio::socket_base::do_not_route @n
1128 * boost::asio::socket_base::keep_alive @n
1129 * boost::asio::socket_base::linger @n
1130 * boost::asio::socket_base::receive_buffer_size @n
1131 * boost::asio::socket_base::receive_low_watermark @n
1132 * boost::asio::socket_base::reuse_address @n
1133 * boost::asio::socket_base::send_buffer_size @n
1134 * boost::asio::socket_base::send_low_watermark @n
1135 * boost::asio::ip::multicast::join_group @n
1136 * boost::asio::ip::multicast::leave_group @n
1137 * boost::asio::ip::multicast::enable_loopback @n
1138 * boost::asio::ip::multicast::outbound_interface @n
1139 * boost::asio::ip::multicast::hops @n
1140 * boost::asio::ip::tcp::no_delay
1143 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
1145 * boost::asio::ip::tcp::socket socket(my_context);
1147 * boost::asio::ip::tcp::socket::keep_alive option;
1148 * boost::system::error_code ec;
1149 * socket.get_option(option, ec);
1152 * // An error occurred.
1154 * bool is_set = option.value();
1157 template <typename GettableSocketOption>
1158 BOOST_ASIO_SYNC_OP_VOID get_option(GettableSocketOption& option,
1159 boost::system::error_code& ec) const
1161 impl_.get_service().get_option(impl_.get_implementation(), option, ec);
1162 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1165 /// Perform an IO control command on the socket.
1167 * This function is used to execute an IO control command on the socket.
1169 * @param command The IO control command to be performed on the socket.
1171 * @throws boost::system::system_error Thrown on failure.
1173 * @sa IoControlCommand @n
1174 * boost::asio::socket_base::bytes_readable @n
1175 * boost::asio::socket_base::non_blocking_io
1178 * Getting the number of bytes ready to read:
1180 * boost::asio::ip::tcp::socket socket(my_context);
1182 * boost::asio::ip::tcp::socket::bytes_readable command;
1183 * socket.io_control(command);
1184 * std::size_t bytes_readable = command.get();
1187 template <typename IoControlCommand>
1188 void io_control(IoControlCommand& command)
1190 boost::system::error_code ec;
1191 impl_.get_service().io_control(impl_.get_implementation(), command, ec);
1192 boost::asio::detail::throw_error(ec, "io_control");
1195 /// Perform an IO control command on the socket.
1197 * This function is used to execute an IO control command on the socket.
1199 * @param command The IO control command to be performed on the socket.
1201 * @param ec Set to indicate what error occurred, if any.
1203 * @sa IoControlCommand @n
1204 * boost::asio::socket_base::bytes_readable @n
1205 * boost::asio::socket_base::non_blocking_io
1208 * Getting the number of bytes ready to read:
1210 * boost::asio::ip::tcp::socket socket(my_context);
1212 * boost::asio::ip::tcp::socket::bytes_readable command;
1213 * boost::system::error_code ec;
1214 * socket.io_control(command, ec);
1217 * // An error occurred.
1219 * std::size_t bytes_readable = command.get();
1222 template <typename IoControlCommand>
1223 BOOST_ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
1224 boost::system::error_code& ec)
1226 impl_.get_service().io_control(impl_.get_implementation(), command, ec);
1227 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1230 /// Gets the non-blocking mode of the socket.
1232 * @returns @c true if the socket's synchronous operations will fail with
1233 * boost::asio::error::would_block if they are unable to perform the requested
1234 * operation immediately. If @c false, synchronous operations will block
1237 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1238 * operations. Asynchronous operations will never fail with the error
1239 * boost::asio::error::would_block.
1241 bool non_blocking() const
1243 return impl_.get_service().non_blocking(impl_.get_implementation());
1246 /// Sets the non-blocking mode of the socket.
1248 * @param mode If @c true, the socket's synchronous operations will fail with
1249 * boost::asio::error::would_block if they are unable to perform the requested
1250 * operation immediately. If @c false, synchronous operations will block
1253 * @throws boost::system::system_error Thrown on failure.
1255 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1256 * operations. Asynchronous operations will never fail with the error
1257 * boost::asio::error::would_block.
1259 void non_blocking(bool mode)
1261 boost::system::error_code ec;
1262 impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
1263 boost::asio::detail::throw_error(ec, "non_blocking");
1266 /// Sets the non-blocking mode of the socket.
1268 * @param mode If @c true, the socket's synchronous operations will fail with
1269 * boost::asio::error::would_block if they are unable to perform the requested
1270 * operation immediately. If @c false, synchronous operations will block
1273 * @param ec Set to indicate what error occurred, if any.
1275 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1276 * operations. Asynchronous operations will never fail with the error
1277 * boost::asio::error::would_block.
1279 BOOST_ASIO_SYNC_OP_VOID non_blocking(
1280 bool mode, boost::system::error_code& ec)
1282 impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
1283 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1286 /// Gets the non-blocking mode of the native socket implementation.
1288 * This function is used to retrieve the non-blocking mode of the underlying
1289 * native socket. This mode has no effect on the behaviour of the socket
1290 * object's synchronous operations.
1292 * @returns @c true if the underlying socket is in non-blocking mode and
1293 * direct system calls may fail with boost::asio::error::would_block (or the
1294 * equivalent system error).
1296 * @note The current non-blocking mode is cached by the socket object.
1297 * Consequently, the return value may be incorrect if the non-blocking mode
1298 * was set directly on the native socket.
1301 * This function is intended to allow the encapsulation of arbitrary
1302 * non-blocking system calls as asynchronous operations, in a way that is
1303 * transparent to the user of the socket object. The following example
1304 * illustrates how Linux's @c sendfile system call might be encapsulated:
1305 * @code template <typename Handler>
1306 * struct sendfile_op
1308 * tcp::socket& sock_;
1312 * std::size_t total_bytes_transferred_;
1314 * // Function call operator meeting WriteHandler requirements.
1315 * // Used as the handler for the async_write_some operation.
1316 * void operator()(boost::system::error_code ec, std::size_t)
1318 * // Put the underlying socket into non-blocking mode.
1320 * if (!sock_.native_non_blocking())
1321 * sock_.native_non_blocking(true, ec);
1327 * // Try the system call.
1329 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1330 * ec = boost::system::error_code(n < 0 ? errno : 0,
1331 * boost::asio::error::get_system_category());
1332 * total_bytes_transferred_ += ec ? 0 : n;
1334 * // Retry operation immediately if interrupted by signal.
1335 * if (ec == boost::asio::error::interrupted)
1338 * // Check if we need to run the operation again.
1339 * if (ec == boost::asio::error::would_block
1340 * || ec == boost::asio::error::try_again)
1342 * // We have to wait for the socket to become ready again.
1343 * sock_.async_wait(tcp::socket::wait_write, *this);
1349 * // An error occurred, or we have reached the end of the file.
1350 * // Either way we must exit the loop so we can call the handler.
1354 * // Loop around to try calling sendfile again.
1358 * // Pass result back to user's handler.
1359 * handler_(ec, total_bytes_transferred_);
1363 * template <typename Handler>
1364 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1366 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1367 * sock.async_wait(tcp::socket::wait_write, op);
1370 bool native_non_blocking() const
1372 return impl_.get_service().native_non_blocking(impl_.get_implementation());
1375 /// Sets the non-blocking mode of the native socket implementation.
1377 * This function is used to modify the non-blocking mode of the underlying
1378 * native socket. It has no effect on the behaviour of the socket object's
1379 * synchronous operations.
1381 * @param mode If @c true, the underlying socket is put into non-blocking
1382 * mode and direct system calls may fail with boost::asio::error::would_block
1383 * (or the equivalent system error).
1385 * @throws boost::system::system_error Thrown on failure. If the @c mode is
1386 * @c false, but the current value of @c non_blocking() is @c true, this
1387 * function fails with boost::asio::error::invalid_argument, as the
1388 * combination does not make sense.
1391 * This function is intended to allow the encapsulation of arbitrary
1392 * non-blocking system calls as asynchronous operations, in a way that is
1393 * transparent to the user of the socket object. The following example
1394 * illustrates how Linux's @c sendfile system call might be encapsulated:
1395 * @code template <typename Handler>
1396 * struct sendfile_op
1398 * tcp::socket& sock_;
1402 * std::size_t total_bytes_transferred_;
1404 * // Function call operator meeting WriteHandler requirements.
1405 * // Used as the handler for the async_write_some operation.
1406 * void operator()(boost::system::error_code ec, std::size_t)
1408 * // Put the underlying socket into non-blocking mode.
1410 * if (!sock_.native_non_blocking())
1411 * sock_.native_non_blocking(true, ec);
1417 * // Try the system call.
1419 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1420 * ec = boost::system::error_code(n < 0 ? errno : 0,
1421 * boost::asio::error::get_system_category());
1422 * total_bytes_transferred_ += ec ? 0 : n;
1424 * // Retry operation immediately if interrupted by signal.
1425 * if (ec == boost::asio::error::interrupted)
1428 * // Check if we need to run the operation again.
1429 * if (ec == boost::asio::error::would_block
1430 * || ec == boost::asio::error::try_again)
1432 * // We have to wait for the socket to become ready again.
1433 * sock_.async_wait(tcp::socket::wait_write, *this);
1439 * // An error occurred, or we have reached the end of the file.
1440 * // Either way we must exit the loop so we can call the handler.
1444 * // Loop around to try calling sendfile again.
1448 * // Pass result back to user's handler.
1449 * handler_(ec, total_bytes_transferred_);
1453 * template <typename Handler>
1454 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1456 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1457 * sock.async_wait(tcp::socket::wait_write, op);
1460 void native_non_blocking(bool mode)
1462 boost::system::error_code ec;
1463 impl_.get_service().native_non_blocking(
1464 impl_.get_implementation(), mode, ec);
1465 boost::asio::detail::throw_error(ec, "native_non_blocking");
1468 /// Sets the non-blocking mode of the native socket implementation.
1470 * This function is used to modify the non-blocking mode of the underlying
1471 * native socket. It has no effect on the behaviour of the socket object's
1472 * synchronous operations.
1474 * @param mode If @c true, the underlying socket is put into non-blocking
1475 * mode and direct system calls may fail with boost::asio::error::would_block
1476 * (or the equivalent system error).
1478 * @param ec Set to indicate what error occurred, if any. If the @c mode is
1479 * @c false, but the current value of @c non_blocking() is @c true, this
1480 * function fails with boost::asio::error::invalid_argument, as the
1481 * combination does not make sense.
1484 * This function is intended to allow the encapsulation of arbitrary
1485 * non-blocking system calls as asynchronous operations, in a way that is
1486 * transparent to the user of the socket object. The following example
1487 * illustrates how Linux's @c sendfile system call might be encapsulated:
1488 * @code template <typename Handler>
1489 * struct sendfile_op
1491 * tcp::socket& sock_;
1495 * std::size_t total_bytes_transferred_;
1497 * // Function call operator meeting WriteHandler requirements.
1498 * // Used as the handler for the async_write_some operation.
1499 * void operator()(boost::system::error_code ec, std::size_t)
1501 * // Put the underlying socket into non-blocking mode.
1503 * if (!sock_.native_non_blocking())
1504 * sock_.native_non_blocking(true, ec);
1510 * // Try the system call.
1512 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1513 * ec = boost::system::error_code(n < 0 ? errno : 0,
1514 * boost::asio::error::get_system_category());
1515 * total_bytes_transferred_ += ec ? 0 : n;
1517 * // Retry operation immediately if interrupted by signal.
1518 * if (ec == boost::asio::error::interrupted)
1521 * // Check if we need to run the operation again.
1522 * if (ec == boost::asio::error::would_block
1523 * || ec == boost::asio::error::try_again)
1525 * // We have to wait for the socket to become ready again.
1526 * sock_.async_wait(tcp::socket::wait_write, *this);
1532 * // An error occurred, or we have reached the end of the file.
1533 * // Either way we must exit the loop so we can call the handler.
1537 * // Loop around to try calling sendfile again.
1541 * // Pass result back to user's handler.
1542 * handler_(ec, total_bytes_transferred_);
1546 * template <typename Handler>
1547 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1549 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1550 * sock.async_wait(tcp::socket::wait_write, op);
1553 BOOST_ASIO_SYNC_OP_VOID native_non_blocking(
1554 bool mode, boost::system::error_code& ec)
1556 impl_.get_service().native_non_blocking(
1557 impl_.get_implementation(), mode, ec);
1558 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1561 /// Get the local endpoint of the socket.
1563 * This function is used to obtain the locally bound endpoint of the socket.
1565 * @returns An object that represents the local endpoint of the socket.
1567 * @throws boost::system::system_error Thrown on failure.
1571 * boost::asio::ip::tcp::socket socket(my_context);
1573 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
1576 endpoint_type local_endpoint() const
1578 boost::system::error_code ec;
1579 endpoint_type ep = impl_.get_service().local_endpoint(
1580 impl_.get_implementation(), ec);
1581 boost::asio::detail::throw_error(ec, "local_endpoint");
1585 /// Get the local endpoint of the socket.
1587 * This function is used to obtain the locally bound endpoint of the socket.
1589 * @param ec Set to indicate what error occurred, if any.
1591 * @returns An object that represents the local endpoint of the socket.
1592 * Returns a default-constructed endpoint object if an error occurred.
1596 * boost::asio::ip::tcp::socket socket(my_context);
1598 * boost::system::error_code ec;
1599 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
1602 * // An error occurred.
1606 endpoint_type local_endpoint(boost::system::error_code& ec) const
1608 return impl_.get_service().local_endpoint(impl_.get_implementation(), ec);
1611 /// Get the remote endpoint of the socket.
1613 * This function is used to obtain the remote endpoint of the socket.
1615 * @returns An object that represents the remote endpoint of the socket.
1617 * @throws boost::system::system_error Thrown on failure.
1621 * boost::asio::ip::tcp::socket socket(my_context);
1623 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
1626 endpoint_type remote_endpoint() const
1628 boost::system::error_code ec;
1629 endpoint_type ep = impl_.get_service().remote_endpoint(
1630 impl_.get_implementation(), ec);
1631 boost::asio::detail::throw_error(ec, "remote_endpoint");
1635 /// Get the remote endpoint of the socket.
1637 * This function is used to obtain the remote endpoint of the socket.
1639 * @param ec Set to indicate what error occurred, if any.
1641 * @returns An object that represents the remote endpoint of the socket.
1642 * Returns a default-constructed endpoint object if an error occurred.
1646 * boost::asio::ip::tcp::socket socket(my_context);
1648 * boost::system::error_code ec;
1649 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
1652 * // An error occurred.
1656 endpoint_type remote_endpoint(boost::system::error_code& ec) const
1658 return impl_.get_service().remote_endpoint(impl_.get_implementation(), ec);
1661 /// Disable sends or receives on the socket.
1663 * This function is used to disable send operations, receive operations, or
1666 * @param what Determines what types of operation will no longer be allowed.
1668 * @throws boost::system::system_error Thrown on failure.
1671 * Shutting down the send side of the socket:
1673 * boost::asio::ip::tcp::socket socket(my_context);
1675 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
1678 void shutdown(shutdown_type what)
1680 boost::system::error_code ec;
1681 impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
1682 boost::asio::detail::throw_error(ec, "shutdown");
1685 /// Disable sends or receives on the socket.
1687 * This function is used to disable send operations, receive operations, or
1690 * @param what Determines what types of operation will no longer be allowed.
1692 * @param ec Set to indicate what error occurred, if any.
1695 * Shutting down the send side of the socket:
1697 * boost::asio::ip::tcp::socket socket(my_context);
1699 * boost::system::error_code ec;
1700 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
1703 * // An error occurred.
1707 BOOST_ASIO_SYNC_OP_VOID shutdown(shutdown_type what,
1708 boost::system::error_code& ec)
1710 impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
1711 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1714 /// Wait for the socket to become ready to read, ready to write, or to have
1715 /// pending error conditions.
1717 * This function is used to perform a blocking wait for a socket to enter
1718 * a ready to read, write or error condition state.
1720 * @param w Specifies the desired socket state.
1723 * Waiting for a socket to become readable.
1725 * boost::asio::ip::tcp::socket socket(my_context);
1727 * socket.wait(boost::asio::ip::tcp::socket::wait_read);
1730 void wait(wait_type w)
1732 boost::system::error_code ec;
1733 impl_.get_service().wait(impl_.get_implementation(), w, ec);
1734 boost::asio::detail::throw_error(ec, "wait");
1737 /// Wait for the socket to become ready to read, ready to write, or to have
1738 /// pending error conditions.
1740 * This function is used to perform a blocking wait for a socket to enter
1741 * a ready to read, write or error condition state.
1743 * @param w Specifies the desired socket state.
1745 * @param ec Set to indicate what error occurred, if any.
1748 * Waiting for a socket to become readable.
1750 * boost::asio::ip::tcp::socket socket(my_context);
1752 * boost::system::error_code ec;
1753 * socket.wait(boost::asio::ip::tcp::socket::wait_read, ec);
1756 BOOST_ASIO_SYNC_OP_VOID wait(wait_type w, boost::system::error_code& ec)
1758 impl_.get_service().wait(impl_.get_implementation(), w, ec);
1759 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1762 /// Asynchronously wait for the socket to become ready to read, ready to
1763 /// write, or to have pending error conditions.
1765 * This function is used to perform an asynchronous wait for a socket to enter
1766 * a ready to read, write or error condition state. It is an initiating
1767 * function for an @ref asynchronous_operation, and always returns
1770 * @param w Specifies the desired socket state.
1772 * @param token The @ref completion_token that will be used to produce a
1773 * completion handler, which will be called when the wait completes. Potential
1774 * completion tokens include @ref use_future, @ref use_awaitable, @ref
1775 * yield_context, or a function object with the correct completion signature.
1776 * The function signature of the completion handler must be:
1777 * @code void handler(
1778 * const boost::system::error_code& error // Result of operation.
1780 * Regardless of whether the asynchronous operation completes immediately or
1781 * not, the completion handler will not be invoked from within this function.
1782 * On immediate completion, invocation of the handler will be performed in a
1783 * manner equivalent to using boost::asio::post().
1785 * @par Completion Signature
1786 * @code void(boost::system::error_code) @endcode
1790 * void wait_handler(const boost::system::error_code& error)
1794 * // Wait succeeded.
1800 * boost::asio::ip::tcp::socket socket(my_context);
1802 * socket.async_wait(boost::asio::ip::tcp::socket::wait_read, wait_handler);
1805 * @par Per-Operation Cancellation
1806 * On POSIX or Windows operating systems, this asynchronous operation supports
1807 * cancellation for the following boost::asio::cancellation_type values:
1809 * @li @c cancellation_type::terminal
1811 * @li @c cancellation_type::partial
1813 * @li @c cancellation_type::total
1816 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
1817 WaitToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1818 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitToken,
1819 void (boost::system::error_code))
1820 async_wait(wait_type w,
1821 BOOST_ASIO_MOVE_ARG(WaitToken) token
1822 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1824 return async_initiate<WaitToken, void (boost::system::error_code)>(
1825 initiate_async_wait(this), token, w);
1829 /// Protected destructor to prevent deletion through this type.
1831 * This function destroys the socket, cancelling any outstanding asynchronous
1832 * operations associated with the socket as if by calling @c cancel.
1838 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
1839 detail::io_object_impl<
1840 detail::null_socket_service<Protocol>, Executor> impl_;
1841 #elif defined(BOOST_ASIO_HAS_IOCP)
1842 detail::io_object_impl<
1843 detail::win_iocp_socket_service<Protocol>, Executor> impl_;
1844 #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
1845 detail::io_object_impl<
1846 detail::io_uring_socket_service<Protocol>, Executor> impl_;
1848 detail::io_object_impl<
1849 detail::reactive_socket_service<Protocol>, Executor> impl_;
1853 // Disallow copying and assignment.
1854 basic_socket(const basic_socket&) BOOST_ASIO_DELETED;
1855 basic_socket& operator=(const basic_socket&) BOOST_ASIO_DELETED;
1857 class initiate_async_connect
1860 typedef Executor executor_type;
1862 explicit initiate_async_connect(basic_socket* self)
1867 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1869 return self_->get_executor();
1872 template <typename ConnectHandler>
1873 void operator()(BOOST_ASIO_MOVE_ARG(ConnectHandler) handler,
1874 const endpoint_type& peer_endpoint,
1875 const boost::system::error_code& open_ec) const
1877 // If you get an error on the following line it means that your handler
1878 // does not meet the documented type requirements for a ConnectHandler.
1879 BOOST_ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
1883 boost::asio::post(self_->impl_.get_executor(),
1884 boost::asio::detail::bind_handler(
1885 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler), open_ec));
1889 detail::non_const_lvalue<ConnectHandler> handler2(handler);
1890 self_->impl_.get_service().async_connect(
1891 self_->impl_.get_implementation(), peer_endpoint,
1892 handler2.value, self_->impl_.get_executor());
1897 basic_socket* self_;
1900 class initiate_async_wait
1903 typedef Executor executor_type;
1905 explicit initiate_async_wait(basic_socket* self)
1910 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1912 return self_->get_executor();
1915 template <typename WaitHandler>
1916 void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler, wait_type w) const
1918 // If you get an error on the following line it means that your handler
1919 // does not meet the documented type requirements for a WaitHandler.
1920 BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
1922 detail::non_const_lvalue<WaitHandler> handler2(handler);
1923 self_->impl_.get_service().async_wait(
1924 self_->impl_.get_implementation(), w,
1925 handler2.value, self_->impl_.get_executor());
1929 basic_socket* self_;
1934 } // namespace boost
1936 #include <boost/asio/detail/pop_options.hpp>
1938 #endif // BOOST_ASIO_BASIC_SOCKET_HPP