5 // Copyright (c) 2003-2016 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/detail/config.hpp>
19 #include <boost/asio/async_result.hpp>
20 #include <boost/asio/basic_io_object.hpp>
21 #include <boost/asio/detail/handler_type_requirements.hpp>
22 #include <boost/asio/detail/throw_error.hpp>
23 #include <boost/asio/detail/type_traits.hpp>
24 #include <boost/asio/error.hpp>
25 #include <boost/asio/socket_base.hpp>
27 #include <boost/asio/detail/push_options.hpp>
32 /// Provides socket functionality.
34 * The basic_socket class template provides functionality that is common to both
35 * stream-oriented and datagram-oriented sockets.
38 * @e Distinct @e objects: Safe.@n
39 * @e Shared @e objects: Unsafe.
41 template <typename Protocol, typename SocketService>
43 : public basic_io_object<SocketService>,
47 /// (Deprecated: Use native_handle_type.) The native representation of a
49 typedef typename SocketService::native_handle_type native_type;
51 /// The native representation of a socket.
52 typedef typename SocketService::native_handle_type native_handle_type;
54 /// The protocol type.
55 typedef Protocol protocol_type;
57 /// The endpoint type.
58 typedef typename Protocol::endpoint endpoint_type;
60 /// A basic_socket is always the lowest layer.
61 typedef basic_socket<Protocol, SocketService> lowest_layer_type;
63 /// Construct a basic_socket without opening it.
65 * This constructor creates a socket without opening it.
67 * @param io_service The io_service object that the socket will use to
68 * dispatch handlers for any asynchronous operations performed on the socket.
70 explicit basic_socket(boost::asio::io_service& io_service)
71 : basic_io_object<SocketService>(io_service)
75 /// Construct and open a basic_socket.
77 * This constructor creates and opens a socket.
79 * @param io_service The io_service object that the socket will use to
80 * dispatch handlers for any asynchronous operations performed on the socket.
82 * @param protocol An object specifying protocol parameters to be used.
84 * @throws boost::system::system_error Thrown on failure.
86 basic_socket(boost::asio::io_service& io_service,
87 const protocol_type& protocol)
88 : basic_io_object<SocketService>(io_service)
90 boost::system::error_code ec;
91 this->get_service().open(this->get_implementation(), protocol, ec);
92 boost::asio::detail::throw_error(ec, "open");
95 /// Construct a basic_socket, opening it and binding it to the given local
98 * This constructor creates a socket and automatically opens it bound to the
99 * specified endpoint on the local machine. The protocol used is the protocol
100 * associated with the given endpoint.
102 * @param io_service The io_service object that the socket will use to
103 * dispatch handlers for any asynchronous operations performed on the socket.
105 * @param endpoint An endpoint on the local machine to which the socket will
108 * @throws boost::system::system_error Thrown on failure.
110 basic_socket(boost::asio::io_service& io_service,
111 const endpoint_type& endpoint)
112 : basic_io_object<SocketService>(io_service)
114 boost::system::error_code ec;
115 const protocol_type protocol = endpoint.protocol();
116 this->get_service().open(this->get_implementation(), protocol, ec);
117 boost::asio::detail::throw_error(ec, "open");
118 this->get_service().bind(this->get_implementation(), endpoint, ec);
119 boost::asio::detail::throw_error(ec, "bind");
122 /// Construct a basic_socket on an existing native socket.
124 * This constructor creates a socket object to hold an existing native socket.
126 * @param io_service The io_service object that the socket will use to
127 * dispatch handlers for any asynchronous operations performed on the socket.
129 * @param protocol An object specifying protocol parameters to be used.
131 * @param native_socket A native socket.
133 * @throws boost::system::system_error Thrown on failure.
135 basic_socket(boost::asio::io_service& io_service,
136 const protocol_type& protocol, const native_handle_type& native_socket)
137 : basic_io_object<SocketService>(io_service)
139 boost::system::error_code ec;
140 this->get_service().assign(this->get_implementation(),
141 protocol, native_socket, ec);
142 boost::asio::detail::throw_error(ec, "assign");
145 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
146 /// Move-construct a basic_socket from another.
148 * This constructor moves a socket from one object to another.
150 * @param other The other basic_socket object from which the move will
153 * @note Following the move, the moved-from object is in the same state as if
154 * constructed using the @c basic_socket(io_service&) constructor.
156 basic_socket(basic_socket&& other)
157 : basic_io_object<SocketService>(
158 BOOST_ASIO_MOVE_CAST(basic_socket)(other))
162 /// Move-assign a basic_socket from another.
164 * This assignment operator moves a socket from one object to another.
166 * @param other The other basic_socket object from which the move will
169 * @note Following the move, the moved-from object is in the same state as if
170 * constructed using the @c basic_socket(io_service&) constructor.
172 basic_socket& operator=(basic_socket&& other)
174 basic_io_object<SocketService>::operator=(
175 BOOST_ASIO_MOVE_CAST(basic_socket)(other));
179 // All sockets have access to each other's implementations.
180 template <typename Protocol1, typename SocketService1>
181 friend class basic_socket;
183 /// Move-construct a basic_socket from a socket of another protocol type.
185 * This constructor moves a socket from one object to another.
187 * @param other The other basic_socket object from which the move will
190 * @note Following the move, the moved-from object is in the same state as if
191 * constructed using the @c basic_socket(io_service&) constructor.
193 template <typename Protocol1, typename SocketService1>
194 basic_socket(basic_socket<Protocol1, SocketService1>&& other,
195 typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
196 : basic_io_object<SocketService>(other.get_io_service())
198 this->get_service().template converting_move_construct<Protocol1>(
199 this->get_implementation(), other.get_implementation());
202 /// Move-assign a basic_socket from a socket of another protocol type.
204 * This assignment operator moves a socket from one object to another.
206 * @param other The other basic_socket object from which the move will
209 * @note Following the move, the moved-from object is in the same state as if
210 * constructed using the @c basic_socket(io_service&) constructor.
212 template <typename Protocol1, typename SocketService1>
213 typename enable_if<is_convertible<Protocol1, Protocol>::value,
214 basic_socket>::type& operator=(
215 basic_socket<Protocol1, SocketService1>&& other)
217 basic_socket tmp(BOOST_ASIO_MOVE_CAST2(basic_socket<
218 Protocol1, SocketService1>)(other));
219 basic_io_object<SocketService>::operator=(
220 BOOST_ASIO_MOVE_CAST(basic_socket)(tmp));
223 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
225 /// Get a reference to the lowest layer.
227 * This function returns a reference to the lowest layer in a stack of
228 * layers. Since a basic_socket cannot contain any further layers, it simply
229 * returns a reference to itself.
231 * @return A reference to the lowest layer in the stack of layers. Ownership
232 * is not transferred to the caller.
234 lowest_layer_type& lowest_layer()
239 /// Get a const reference to the lowest layer.
241 * This function returns a const reference to the lowest layer in a stack of
242 * layers. Since a basic_socket cannot contain any further layers, it simply
243 * returns a reference to itself.
245 * @return A const reference to the lowest layer in the stack of layers.
246 * Ownership is not transferred to the caller.
248 const lowest_layer_type& lowest_layer() const
253 /// Open the socket using the specified protocol.
255 * This function opens the socket so that it will use the specified protocol.
257 * @param protocol An object specifying protocol parameters to be used.
259 * @throws boost::system::system_error Thrown on failure.
263 * boost::asio::ip::tcp::socket socket(io_service);
264 * socket.open(boost::asio::ip::tcp::v4());
267 void open(const protocol_type& protocol = protocol_type())
269 boost::system::error_code ec;
270 this->get_service().open(this->get_implementation(), protocol, ec);
271 boost::asio::detail::throw_error(ec, "open");
274 /// Open the socket using the specified protocol.
276 * This function opens the socket so that it will use the specified protocol.
278 * @param protocol An object specifying which protocol is to be used.
280 * @param ec Set to indicate what error occurred, if any.
284 * boost::asio::ip::tcp::socket socket(io_service);
285 * boost::system::error_code ec;
286 * socket.open(boost::asio::ip::tcp::v4(), ec);
289 * // An error occurred.
293 boost::system::error_code open(const protocol_type& protocol,
294 boost::system::error_code& ec)
296 return this->get_service().open(this->get_implementation(), protocol, ec);
299 /// Assign an existing native socket to the socket.
301 * This function opens the socket to hold an existing native socket.
303 * @param protocol An object specifying which protocol is to be used.
305 * @param native_socket A native socket.
307 * @throws boost::system::system_error Thrown on failure.
309 void assign(const protocol_type& protocol,
310 const native_handle_type& native_socket)
312 boost::system::error_code ec;
313 this->get_service().assign(this->get_implementation(),
314 protocol, native_socket, ec);
315 boost::asio::detail::throw_error(ec, "assign");
318 /// Assign an existing native socket to the socket.
320 * This function opens the socket to hold an existing native socket.
322 * @param protocol An object specifying which protocol is to be used.
324 * @param native_socket A native socket.
326 * @param ec Set to indicate what error occurred, if any.
328 boost::system::error_code assign(const protocol_type& protocol,
329 const native_handle_type& native_socket, boost::system::error_code& ec)
331 return this->get_service().assign(this->get_implementation(),
332 protocol, native_socket, ec);
335 /// Determine whether the socket is open.
338 return this->get_service().is_open(this->get_implementation());
341 /// Close the socket.
343 * This function is used to close the socket. Any asynchronous send, receive
344 * or connect operations will be cancelled immediately, and will complete
345 * with the boost::asio::error::operation_aborted error.
347 * @throws boost::system::system_error Thrown on failure. Note that, even if
348 * the function indicates an error, the underlying descriptor is closed.
350 * @note For portable behaviour with respect to graceful closure of a
351 * connected socket, call shutdown() before closing the socket.
355 boost::system::error_code ec;
356 this->get_service().close(this->get_implementation(), ec);
357 boost::asio::detail::throw_error(ec, "close");
360 /// Close the socket.
362 * This function is used to close the socket. Any asynchronous send, receive
363 * or connect operations will be cancelled immediately, and will complete
364 * with the boost::asio::error::operation_aborted error.
366 * @param ec Set to indicate what error occurred, if any. Note that, even if
367 * the function indicates an error, the underlying descriptor is closed.
371 * boost::asio::ip::tcp::socket socket(io_service);
373 * boost::system::error_code ec;
377 * // An error occurred.
381 * @note For portable behaviour with respect to graceful closure of a
382 * connected socket, call shutdown() before closing the socket.
384 boost::system::error_code close(boost::system::error_code& ec)
386 return this->get_service().close(this->get_implementation(), ec);
389 /// (Deprecated: Use native_handle().) Get the native socket representation.
391 * This function may be used to obtain the underlying representation of the
392 * socket. This is intended to allow access to native socket functionality
393 * that is not otherwise provided.
397 return this->get_service().native_handle(this->get_implementation());
400 /// Get the native socket representation.
402 * This function may be used to obtain the underlying representation of the
403 * socket. This is intended to allow access to native socket functionality
404 * that is not otherwise provided.
406 native_handle_type native_handle()
408 return this->get_service().native_handle(this->get_implementation());
411 /// Cancel all asynchronous operations associated with the socket.
413 * This function causes all outstanding asynchronous connect, send and receive
414 * operations to finish immediately, and the handlers for cancelled operations
415 * will be passed the boost::asio::error::operation_aborted error.
417 * @throws boost::system::system_error Thrown on failure.
419 * @note Calls to cancel() will always fail with
420 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
421 * Server 2003, and earlier versions of Windows, unless
422 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
423 * two issues that should be considered before enabling its use:
425 * @li It will only cancel asynchronous operations that were initiated in the
428 * @li It can appear to complete without error, but the request to cancel the
429 * unfinished operations may be silently ignored by the operating system.
430 * Whether it works or not seems to depend on the drivers that are installed.
432 * For portable cancellation, consider using one of the following
435 * @li Disable asio's I/O completion port backend by defining
436 * BOOST_ASIO_DISABLE_IOCP.
438 * @li Use the close() function to simultaneously cancel the outstanding
439 * operations and close the socket.
441 * When running on Windows Vista, Windows Server 2008, and later, the
442 * CancelIoEx function is always used. This function does not have the
443 * problems described above.
445 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
446 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
447 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
448 __declspec(deprecated("By default, this function always fails with "
449 "operation_not_supported when used on Windows XP, Windows Server 2003, "
450 "or earlier. Consult documentation for details."))
454 boost::system::error_code ec;
455 this->get_service().cancel(this->get_implementation(), ec);
456 boost::asio::detail::throw_error(ec, "cancel");
459 /// Cancel all asynchronous operations associated with the socket.
461 * This function causes all outstanding asynchronous connect, send and receive
462 * operations to finish immediately, and the handlers for cancelled operations
463 * will be passed the boost::asio::error::operation_aborted error.
465 * @param ec Set to indicate what error occurred, if any.
467 * @note Calls to cancel() will always fail with
468 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
469 * Server 2003, and earlier versions of Windows, unless
470 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
471 * two issues that should be considered before enabling its use:
473 * @li It will only cancel asynchronous operations that were initiated in the
476 * @li It can appear to complete without error, but the request to cancel the
477 * unfinished operations may be silently ignored by the operating system.
478 * Whether it works or not seems to depend on the drivers that are installed.
480 * For portable cancellation, consider using one of the following
483 * @li Disable asio's I/O completion port backend by defining
484 * BOOST_ASIO_DISABLE_IOCP.
486 * @li Use the close() function to simultaneously cancel the outstanding
487 * operations and close the socket.
489 * When running on Windows Vista, Windows Server 2008, and later, the
490 * CancelIoEx function is always used. This function does not have the
491 * problems described above.
493 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
494 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
495 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
496 __declspec(deprecated("By default, this function always fails with "
497 "operation_not_supported when used on Windows XP, Windows Server 2003, "
498 "or earlier. Consult documentation for details."))
500 boost::system::error_code cancel(boost::system::error_code& ec)
502 return this->get_service().cancel(this->get_implementation(), ec);
505 /// Determine whether the socket is at the out-of-band data mark.
507 * This function is used to check whether the socket input is currently
508 * positioned at the out-of-band data mark.
510 * @return A bool indicating whether the socket is at the out-of-band data
513 * @throws boost::system::system_error Thrown on failure.
517 boost::system::error_code ec;
518 bool b = this->get_service().at_mark(this->get_implementation(), ec);
519 boost::asio::detail::throw_error(ec, "at_mark");
523 /// Determine whether the socket is at the out-of-band data mark.
525 * This function is used to check whether the socket input is currently
526 * positioned at the out-of-band data mark.
528 * @param ec Set to indicate what error occurred, if any.
530 * @return A bool indicating whether the socket is at the out-of-band data
533 bool at_mark(boost::system::error_code& ec) const
535 return this->get_service().at_mark(this->get_implementation(), ec);
538 /// Determine the number of bytes available for reading.
540 * This function is used to determine the number of bytes that may be read
543 * @return The number of bytes that may be read without blocking, or 0 if an
546 * @throws boost::system::system_error Thrown on failure.
548 std::size_t available() const
550 boost::system::error_code ec;
551 std::size_t s = this->get_service().available(
552 this->get_implementation(), ec);
553 boost::asio::detail::throw_error(ec, "available");
557 /// Determine the number of bytes available for reading.
559 * This function is used to determine the number of bytes that may be read
562 * @param ec Set to indicate what error occurred, if any.
564 * @return The number of bytes that may be read without blocking, or 0 if an
567 std::size_t available(boost::system::error_code& ec) const
569 return this->get_service().available(this->get_implementation(), ec);
572 /// Bind the socket to the given local endpoint.
574 * This function binds the socket to the specified endpoint on the local
577 * @param endpoint An endpoint on the local machine to which the socket will
580 * @throws boost::system::system_error Thrown on failure.
584 * boost::asio::ip::tcp::socket socket(io_service);
585 * socket.open(boost::asio::ip::tcp::v4());
586 * socket.bind(boost::asio::ip::tcp::endpoint(
587 * boost::asio::ip::tcp::v4(), 12345));
590 void bind(const endpoint_type& endpoint)
592 boost::system::error_code ec;
593 this->get_service().bind(this->get_implementation(), endpoint, ec);
594 boost::asio::detail::throw_error(ec, "bind");
597 /// Bind the socket to the given local endpoint.
599 * This function binds the socket to the specified endpoint on the local
602 * @param endpoint An endpoint on the local machine to which the socket will
605 * @param ec Set to indicate what error occurred, if any.
609 * boost::asio::ip::tcp::socket socket(io_service);
610 * socket.open(boost::asio::ip::tcp::v4());
611 * boost::system::error_code ec;
612 * socket.bind(boost::asio::ip::tcp::endpoint(
613 * boost::asio::ip::tcp::v4(), 12345), ec);
616 * // An error occurred.
620 boost::system::error_code bind(const endpoint_type& endpoint,
621 boost::system::error_code& ec)
623 return this->get_service().bind(this->get_implementation(), endpoint, ec);
626 /// Connect the socket to the specified endpoint.
628 * This function is used to connect a socket to the specified remote endpoint.
629 * The function call will block until the connection is successfully made or
632 * The socket is automatically opened if it is not already open. If the
633 * connect fails, and the socket was automatically opened, the socket is
634 * not returned to the closed state.
636 * @param peer_endpoint The remote endpoint to which the socket will be
639 * @throws boost::system::system_error Thrown on failure.
643 * boost::asio::ip::tcp::socket socket(io_service);
644 * boost::asio::ip::tcp::endpoint endpoint(
645 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
646 * socket.connect(endpoint);
649 void connect(const endpoint_type& peer_endpoint)
651 boost::system::error_code ec;
654 this->get_service().open(this->get_implementation(),
655 peer_endpoint.protocol(), ec);
656 boost::asio::detail::throw_error(ec, "connect");
658 this->get_service().connect(this->get_implementation(), peer_endpoint, ec);
659 boost::asio::detail::throw_error(ec, "connect");
662 /// Connect the socket to the specified endpoint.
664 * This function is used to connect a socket to the specified remote endpoint.
665 * The function call will block until the connection is successfully made or
668 * The socket is automatically opened if it is not already open. If the
669 * connect fails, and the socket was automatically opened, the socket is
670 * not returned to the closed state.
672 * @param peer_endpoint The remote endpoint to which the socket will be
675 * @param ec Set to indicate what error occurred, if any.
679 * boost::asio::ip::tcp::socket socket(io_service);
680 * boost::asio::ip::tcp::endpoint endpoint(
681 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
682 * boost::system::error_code ec;
683 * socket.connect(endpoint, ec);
686 * // An error occurred.
690 boost::system::error_code connect(const endpoint_type& peer_endpoint,
691 boost::system::error_code& ec)
695 if (this->get_service().open(this->get_implementation(),
696 peer_endpoint.protocol(), ec))
702 return this->get_service().connect(
703 this->get_implementation(), peer_endpoint, ec);
706 /// Start an asynchronous connect.
708 * This function is used to asynchronously connect a socket to the specified
709 * remote endpoint. The function call always returns immediately.
711 * The socket is automatically opened if it is not already open. If the
712 * connect fails, and the socket was automatically opened, the socket is
713 * not returned to the closed state.
715 * @param peer_endpoint The remote endpoint to which the socket will be
716 * connected. Copies will be made of the endpoint object as required.
718 * @param handler The handler to be called when the connection operation
719 * completes. Copies will be made of the handler as required. The function
720 * signature of the handler must be:
721 * @code void handler(
722 * const boost::system::error_code& error // Result of operation
724 * Regardless of whether the asynchronous operation completes immediately or
725 * not, the handler will not be invoked from within this function. Invocation
726 * of the handler will be performed in a manner equivalent to using
727 * boost::asio::io_service::post().
731 * void connect_handler(const boost::system::error_code& error)
735 * // Connect succeeded.
741 * boost::asio::ip::tcp::socket socket(io_service);
742 * boost::asio::ip::tcp::endpoint endpoint(
743 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
744 * socket.async_connect(endpoint, connect_handler);
747 template <typename ConnectHandler>
748 BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
749 void (boost::system::error_code))
750 async_connect(const endpoint_type& peer_endpoint,
751 BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
753 // If you get an error on the following line it means that your handler does
754 // not meet the documented type requirements for a ConnectHandler.
755 BOOST_ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
759 boost::system::error_code ec;
760 const protocol_type protocol = peer_endpoint.protocol();
761 if (this->get_service().open(this->get_implementation(), protocol, ec))
763 detail::async_result_init<
764 ConnectHandler, void (boost::system::error_code)> init(
765 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
767 this->get_io_service().post(
768 boost::asio::detail::bind_handler(
769 BOOST_ASIO_MOVE_CAST(BOOST_ASIO_HANDLER_TYPE(
770 ConnectHandler, void (boost::system::error_code)))(
773 return init.result.get();
777 return this->get_service().async_connect(this->get_implementation(),
778 peer_endpoint, BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
781 /// Set an option on the socket.
783 * This function is used to set an option on the socket.
785 * @param option The new option value to be set on the socket.
787 * @throws boost::system::system_error Thrown on failure.
789 * @sa SettableSocketOption @n
790 * boost::asio::socket_base::broadcast @n
791 * boost::asio::socket_base::do_not_route @n
792 * boost::asio::socket_base::keep_alive @n
793 * boost::asio::socket_base::linger @n
794 * boost::asio::socket_base::receive_buffer_size @n
795 * boost::asio::socket_base::receive_low_watermark @n
796 * boost::asio::socket_base::reuse_address @n
797 * boost::asio::socket_base::send_buffer_size @n
798 * boost::asio::socket_base::send_low_watermark @n
799 * boost::asio::ip::multicast::join_group @n
800 * boost::asio::ip::multicast::leave_group @n
801 * boost::asio::ip::multicast::enable_loopback @n
802 * boost::asio::ip::multicast::outbound_interface @n
803 * boost::asio::ip::multicast::hops @n
804 * boost::asio::ip::tcp::no_delay
807 * Setting the IPPROTO_TCP/TCP_NODELAY option:
809 * boost::asio::ip::tcp::socket socket(io_service);
811 * boost::asio::ip::tcp::no_delay option(true);
812 * socket.set_option(option);
815 template <typename SettableSocketOption>
816 void set_option(const SettableSocketOption& option)
818 boost::system::error_code ec;
819 this->get_service().set_option(this->get_implementation(), option, ec);
820 boost::asio::detail::throw_error(ec, "set_option");
823 /// Set an option on the socket.
825 * This function is used to set an option on the socket.
827 * @param option The new option value to be set on the socket.
829 * @param ec Set to indicate what error occurred, if any.
831 * @sa SettableSocketOption @n
832 * boost::asio::socket_base::broadcast @n
833 * boost::asio::socket_base::do_not_route @n
834 * boost::asio::socket_base::keep_alive @n
835 * boost::asio::socket_base::linger @n
836 * boost::asio::socket_base::receive_buffer_size @n
837 * boost::asio::socket_base::receive_low_watermark @n
838 * boost::asio::socket_base::reuse_address @n
839 * boost::asio::socket_base::send_buffer_size @n
840 * boost::asio::socket_base::send_low_watermark @n
841 * boost::asio::ip::multicast::join_group @n
842 * boost::asio::ip::multicast::leave_group @n
843 * boost::asio::ip::multicast::enable_loopback @n
844 * boost::asio::ip::multicast::outbound_interface @n
845 * boost::asio::ip::multicast::hops @n
846 * boost::asio::ip::tcp::no_delay
849 * Setting the IPPROTO_TCP/TCP_NODELAY option:
851 * boost::asio::ip::tcp::socket socket(io_service);
853 * boost::asio::ip::tcp::no_delay option(true);
854 * boost::system::error_code ec;
855 * socket.set_option(option, ec);
858 * // An error occurred.
862 template <typename SettableSocketOption>
863 boost::system::error_code set_option(const SettableSocketOption& option,
864 boost::system::error_code& ec)
866 return this->get_service().set_option(
867 this->get_implementation(), option, ec);
870 /// Get an option from the socket.
872 * This function is used to get the current value of an option on the socket.
874 * @param option The option value to be obtained from the socket.
876 * @throws boost::system::system_error Thrown on failure.
878 * @sa GettableSocketOption @n
879 * boost::asio::socket_base::broadcast @n
880 * boost::asio::socket_base::do_not_route @n
881 * boost::asio::socket_base::keep_alive @n
882 * boost::asio::socket_base::linger @n
883 * boost::asio::socket_base::receive_buffer_size @n
884 * boost::asio::socket_base::receive_low_watermark @n
885 * boost::asio::socket_base::reuse_address @n
886 * boost::asio::socket_base::send_buffer_size @n
887 * boost::asio::socket_base::send_low_watermark @n
888 * boost::asio::ip::multicast::join_group @n
889 * boost::asio::ip::multicast::leave_group @n
890 * boost::asio::ip::multicast::enable_loopback @n
891 * boost::asio::ip::multicast::outbound_interface @n
892 * boost::asio::ip::multicast::hops @n
893 * boost::asio::ip::tcp::no_delay
896 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
898 * boost::asio::ip::tcp::socket socket(io_service);
900 * boost::asio::ip::tcp::socket::keep_alive option;
901 * socket.get_option(option);
902 * bool is_set = option.value();
905 template <typename GettableSocketOption>
906 void get_option(GettableSocketOption& option) const
908 boost::system::error_code ec;
909 this->get_service().get_option(this->get_implementation(), option, ec);
910 boost::asio::detail::throw_error(ec, "get_option");
913 /// Get an option from the socket.
915 * This function is used to get the current value of an option on the socket.
917 * @param option The option value to be obtained from the socket.
919 * @param ec Set to indicate what error occurred, if any.
921 * @sa GettableSocketOption @n
922 * boost::asio::socket_base::broadcast @n
923 * boost::asio::socket_base::do_not_route @n
924 * boost::asio::socket_base::keep_alive @n
925 * boost::asio::socket_base::linger @n
926 * boost::asio::socket_base::receive_buffer_size @n
927 * boost::asio::socket_base::receive_low_watermark @n
928 * boost::asio::socket_base::reuse_address @n
929 * boost::asio::socket_base::send_buffer_size @n
930 * boost::asio::socket_base::send_low_watermark @n
931 * boost::asio::ip::multicast::join_group @n
932 * boost::asio::ip::multicast::leave_group @n
933 * boost::asio::ip::multicast::enable_loopback @n
934 * boost::asio::ip::multicast::outbound_interface @n
935 * boost::asio::ip::multicast::hops @n
936 * boost::asio::ip::tcp::no_delay
939 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
941 * boost::asio::ip::tcp::socket socket(io_service);
943 * boost::asio::ip::tcp::socket::keep_alive option;
944 * boost::system::error_code ec;
945 * socket.get_option(option, ec);
948 * // An error occurred.
950 * bool is_set = option.value();
953 template <typename GettableSocketOption>
954 boost::system::error_code get_option(GettableSocketOption& option,
955 boost::system::error_code& ec) const
957 return this->get_service().get_option(
958 this->get_implementation(), option, ec);
961 /// Perform an IO control command on the socket.
963 * This function is used to execute an IO control command on the socket.
965 * @param command The IO control command to be performed on the socket.
967 * @throws boost::system::system_error Thrown on failure.
969 * @sa IoControlCommand @n
970 * boost::asio::socket_base::bytes_readable @n
971 * boost::asio::socket_base::non_blocking_io
974 * Getting the number of bytes ready to read:
976 * boost::asio::ip::tcp::socket socket(io_service);
978 * boost::asio::ip::tcp::socket::bytes_readable command;
979 * socket.io_control(command);
980 * std::size_t bytes_readable = command.get();
983 template <typename IoControlCommand>
984 void io_control(IoControlCommand& command)
986 boost::system::error_code ec;
987 this->get_service().io_control(this->get_implementation(), command, ec);
988 boost::asio::detail::throw_error(ec, "io_control");
991 /// Perform an IO control command on the socket.
993 * This function is used to execute an IO control command on the socket.
995 * @param command The IO control command to be performed on the socket.
997 * @param ec Set to indicate what error occurred, if any.
999 * @sa IoControlCommand @n
1000 * boost::asio::socket_base::bytes_readable @n
1001 * boost::asio::socket_base::non_blocking_io
1004 * Getting the number of bytes ready to read:
1006 * boost::asio::ip::tcp::socket socket(io_service);
1008 * boost::asio::ip::tcp::socket::bytes_readable command;
1009 * boost::system::error_code ec;
1010 * socket.io_control(command, ec);
1013 * // An error occurred.
1015 * std::size_t bytes_readable = command.get();
1018 template <typename IoControlCommand>
1019 boost::system::error_code io_control(IoControlCommand& command,
1020 boost::system::error_code& ec)
1022 return this->get_service().io_control(
1023 this->get_implementation(), command, ec);
1026 /// Gets the non-blocking mode of the socket.
1028 * @returns @c true if the socket's synchronous operations will fail with
1029 * boost::asio::error::would_block if they are unable to perform the requested
1030 * operation immediately. If @c false, synchronous operations will block
1033 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1034 * operations. Asynchronous operations will never fail with the error
1035 * boost::asio::error::would_block.
1037 bool non_blocking() const
1039 return this->get_service().non_blocking(this->get_implementation());
1042 /// Sets the non-blocking mode of the socket.
1044 * @param mode If @c true, the socket's synchronous operations will fail with
1045 * boost::asio::error::would_block if they are unable to perform the requested
1046 * operation immediately. If @c false, synchronous operations will block
1049 * @throws boost::system::system_error Thrown on failure.
1051 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1052 * operations. Asynchronous operations will never fail with the error
1053 * boost::asio::error::would_block.
1055 void non_blocking(bool mode)
1057 boost::system::error_code ec;
1058 this->get_service().non_blocking(this->get_implementation(), mode, ec);
1059 boost::asio::detail::throw_error(ec, "non_blocking");
1062 /// Sets the non-blocking mode of the socket.
1064 * @param mode If @c true, the socket's synchronous operations will fail with
1065 * boost::asio::error::would_block if they are unable to perform the requested
1066 * operation immediately. If @c false, synchronous operations will block
1069 * @param ec Set to indicate what error occurred, if any.
1071 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1072 * operations. Asynchronous operations will never fail with the error
1073 * boost::asio::error::would_block.
1075 boost::system::error_code non_blocking(
1076 bool mode, boost::system::error_code& ec)
1078 return this->get_service().non_blocking(
1079 this->get_implementation(), mode, ec);
1082 /// Gets the non-blocking mode of the native socket implementation.
1084 * This function is used to retrieve the non-blocking mode of the underlying
1085 * native socket. This mode has no effect on the behaviour of the socket
1086 * object's synchronous operations.
1088 * @returns @c true if the underlying socket is in non-blocking mode and
1089 * direct system calls may fail with boost::asio::error::would_block (or the
1090 * equivalent system error).
1092 * @note The current non-blocking mode is cached by the socket object.
1093 * Consequently, the return value may be incorrect if the non-blocking mode
1094 * was set directly on the native socket.
1097 * This function is intended to allow the encapsulation of arbitrary
1098 * non-blocking system calls as asynchronous operations, in a way that is
1099 * transparent to the user of the socket object. The following example
1100 * illustrates how Linux's @c sendfile system call might be encapsulated:
1101 * @code template <typename Handler>
1102 * struct sendfile_op
1104 * tcp::socket& sock_;
1108 * std::size_t total_bytes_transferred_;
1110 * // Function call operator meeting WriteHandler requirements.
1111 * // Used as the handler for the async_write_some operation.
1112 * void operator()(boost::system::error_code ec, std::size_t)
1114 * // Put the underlying socket into non-blocking mode.
1116 * if (!sock_.native_non_blocking())
1117 * sock_.native_non_blocking(true, ec);
1123 * // Try the system call.
1125 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1126 * ec = boost::system::error_code(n < 0 ? errno : 0,
1127 * boost::asio::error::get_system_category());
1128 * total_bytes_transferred_ += ec ? 0 : n;
1130 * // Retry operation immediately if interrupted by signal.
1131 * if (ec == boost::asio::error::interrupted)
1134 * // Check if we need to run the operation again.
1135 * if (ec == boost::asio::error::would_block
1136 * || ec == boost::asio::error::try_again)
1138 * // We have to wait for the socket to become ready again.
1139 * sock_.async_write_some(boost::asio::null_buffers(), *this);
1145 * // An error occurred, or we have reached the end of the file.
1146 * // Either way we must exit the loop so we can call the handler.
1150 * // Loop around to try calling sendfile again.
1154 * // Pass result back to user's handler.
1155 * handler_(ec, total_bytes_transferred_);
1159 * template <typename Handler>
1160 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1162 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1163 * sock.async_write_some(boost::asio::null_buffers(), op);
1166 bool native_non_blocking() const
1168 return this->get_service().native_non_blocking(this->get_implementation());
1171 /// Sets the non-blocking mode of the native socket implementation.
1173 * This function is used to modify the non-blocking mode of the underlying
1174 * native socket. It has no effect on the behaviour of the socket object's
1175 * synchronous operations.
1177 * @param mode If @c true, the underlying socket is put into non-blocking
1178 * mode and direct system calls may fail with boost::asio::error::would_block
1179 * (or the equivalent system error).
1181 * @throws boost::system::system_error Thrown on failure. If the @c mode is
1182 * @c false, but the current value of @c non_blocking() is @c true, this
1183 * function fails with boost::asio::error::invalid_argument, as the
1184 * combination does not make sense.
1187 * This function is intended to allow the encapsulation of arbitrary
1188 * non-blocking system calls as asynchronous operations, in a way that is
1189 * transparent to the user of the socket object. The following example
1190 * illustrates how Linux's @c sendfile system call might be encapsulated:
1191 * @code template <typename Handler>
1192 * struct sendfile_op
1194 * tcp::socket& sock_;
1198 * std::size_t total_bytes_transferred_;
1200 * // Function call operator meeting WriteHandler requirements.
1201 * // Used as the handler for the async_write_some operation.
1202 * void operator()(boost::system::error_code ec, std::size_t)
1204 * // Put the underlying socket into non-blocking mode.
1206 * if (!sock_.native_non_blocking())
1207 * sock_.native_non_blocking(true, ec);
1213 * // Try the system call.
1215 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1216 * ec = boost::system::error_code(n < 0 ? errno : 0,
1217 * boost::asio::error::get_system_category());
1218 * total_bytes_transferred_ += ec ? 0 : n;
1220 * // Retry operation immediately if interrupted by signal.
1221 * if (ec == boost::asio::error::interrupted)
1224 * // Check if we need to run the operation again.
1225 * if (ec == boost::asio::error::would_block
1226 * || ec == boost::asio::error::try_again)
1228 * // We have to wait for the socket to become ready again.
1229 * sock_.async_write_some(boost::asio::null_buffers(), *this);
1235 * // An error occurred, or we have reached the end of the file.
1236 * // Either way we must exit the loop so we can call the handler.
1240 * // Loop around to try calling sendfile again.
1244 * // Pass result back to user's handler.
1245 * handler_(ec, total_bytes_transferred_);
1249 * template <typename Handler>
1250 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1252 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1253 * sock.async_write_some(boost::asio::null_buffers(), op);
1256 void native_non_blocking(bool mode)
1258 boost::system::error_code ec;
1259 this->get_service().native_non_blocking(
1260 this->get_implementation(), mode, ec);
1261 boost::asio::detail::throw_error(ec, "native_non_blocking");
1264 /// Sets the non-blocking mode of the native socket implementation.
1266 * This function is used to modify the non-blocking mode of the underlying
1267 * native socket. It has no effect on the behaviour of the socket object's
1268 * synchronous operations.
1270 * @param mode If @c true, the underlying socket is put into non-blocking
1271 * mode and direct system calls may fail with boost::asio::error::would_block
1272 * (or the equivalent system error).
1274 * @param ec Set to indicate what error occurred, if any. If the @c mode is
1275 * @c false, but the current value of @c non_blocking() is @c true, this
1276 * function fails with boost::asio::error::invalid_argument, as the
1277 * combination does not make sense.
1280 * This function is intended to allow the encapsulation of arbitrary
1281 * non-blocking system calls as asynchronous operations, in a way that is
1282 * transparent to the user of the socket object. The following example
1283 * illustrates how Linux's @c sendfile system call might be encapsulated:
1284 * @code template <typename Handler>
1285 * struct sendfile_op
1287 * tcp::socket& sock_;
1291 * std::size_t total_bytes_transferred_;
1293 * // Function call operator meeting WriteHandler requirements.
1294 * // Used as the handler for the async_write_some operation.
1295 * void operator()(boost::system::error_code ec, std::size_t)
1297 * // Put the underlying socket into non-blocking mode.
1299 * if (!sock_.native_non_blocking())
1300 * sock_.native_non_blocking(true, ec);
1306 * // Try the system call.
1308 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1309 * ec = boost::system::error_code(n < 0 ? errno : 0,
1310 * boost::asio::error::get_system_category());
1311 * total_bytes_transferred_ += ec ? 0 : n;
1313 * // Retry operation immediately if interrupted by signal.
1314 * if (ec == boost::asio::error::interrupted)
1317 * // Check if we need to run the operation again.
1318 * if (ec == boost::asio::error::would_block
1319 * || ec == boost::asio::error::try_again)
1321 * // We have to wait for the socket to become ready again.
1322 * sock_.async_write_some(boost::asio::null_buffers(), *this);
1328 * // An error occurred, or we have reached the end of the file.
1329 * // Either way we must exit the loop so we can call the handler.
1333 * // Loop around to try calling sendfile again.
1337 * // Pass result back to user's handler.
1338 * handler_(ec, total_bytes_transferred_);
1342 * template <typename Handler>
1343 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1345 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1346 * sock.async_write_some(boost::asio::null_buffers(), op);
1349 boost::system::error_code native_non_blocking(
1350 bool mode, boost::system::error_code& ec)
1352 return this->get_service().native_non_blocking(
1353 this->get_implementation(), mode, ec);
1356 /// Get the local endpoint of the socket.
1358 * This function is used to obtain the locally bound endpoint of the socket.
1360 * @returns An object that represents the local endpoint of the socket.
1362 * @throws boost::system::system_error Thrown on failure.
1366 * boost::asio::ip::tcp::socket socket(io_service);
1368 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
1371 endpoint_type local_endpoint() const
1373 boost::system::error_code ec;
1374 endpoint_type ep = this->get_service().local_endpoint(
1375 this->get_implementation(), ec);
1376 boost::asio::detail::throw_error(ec, "local_endpoint");
1380 /// Get the local endpoint of the socket.
1382 * This function is used to obtain the locally bound endpoint of the socket.
1384 * @param ec Set to indicate what error occurred, if any.
1386 * @returns An object that represents the local endpoint of the socket.
1387 * Returns a default-constructed endpoint object if an error occurred.
1391 * boost::asio::ip::tcp::socket socket(io_service);
1393 * boost::system::error_code ec;
1394 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
1397 * // An error occurred.
1401 endpoint_type local_endpoint(boost::system::error_code& ec) const
1403 return this->get_service().local_endpoint(this->get_implementation(), ec);
1406 /// Get the remote endpoint of the socket.
1408 * This function is used to obtain the remote endpoint of the socket.
1410 * @returns An object that represents the remote endpoint of the socket.
1412 * @throws boost::system::system_error Thrown on failure.
1416 * boost::asio::ip::tcp::socket socket(io_service);
1418 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
1421 endpoint_type remote_endpoint() const
1423 boost::system::error_code ec;
1424 endpoint_type ep = this->get_service().remote_endpoint(
1425 this->get_implementation(), ec);
1426 boost::asio::detail::throw_error(ec, "remote_endpoint");
1430 /// Get the remote endpoint of the socket.
1432 * This function is used to obtain the remote endpoint of the socket.
1434 * @param ec Set to indicate what error occurred, if any.
1436 * @returns An object that represents the remote endpoint of the socket.
1437 * Returns a default-constructed endpoint object if an error occurred.
1441 * boost::asio::ip::tcp::socket socket(io_service);
1443 * boost::system::error_code ec;
1444 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
1447 * // An error occurred.
1451 endpoint_type remote_endpoint(boost::system::error_code& ec) const
1453 return this->get_service().remote_endpoint(this->get_implementation(), ec);
1456 /// Disable sends or receives on the socket.
1458 * This function is used to disable send operations, receive operations, or
1461 * @param what Determines what types of operation will no longer be allowed.
1463 * @throws boost::system::system_error Thrown on failure.
1466 * Shutting down the send side of the socket:
1468 * boost::asio::ip::tcp::socket socket(io_service);
1470 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
1473 void shutdown(shutdown_type what)
1475 boost::system::error_code ec;
1476 this->get_service().shutdown(this->get_implementation(), what, ec);
1477 boost::asio::detail::throw_error(ec, "shutdown");
1480 /// Disable sends or receives on the socket.
1482 * This function is used to disable send operations, receive operations, or
1485 * @param what Determines what types of operation will no longer be allowed.
1487 * @param ec Set to indicate what error occurred, if any.
1490 * Shutting down the send side of the socket:
1492 * boost::asio::ip::tcp::socket socket(io_service);
1494 * boost::system::error_code ec;
1495 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
1498 * // An error occurred.
1502 boost::system::error_code shutdown(shutdown_type what,
1503 boost::system::error_code& ec)
1505 return this->get_service().shutdown(this->get_implementation(), what, ec);
1509 /// Protected destructor to prevent deletion through this type.
1516 } // namespace boost
1518 #include <boost/asio/detail/pop_options.hpp>
1520 #endif // BOOST_ASIO_BASIC_SOCKET_HPP