2 // basic_stream_socket.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_BASIC_STREAM_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_STREAM_SOCKET_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/basic_socket.hpp>
22 #include <boost/asio/detail/handler_type_requirements.hpp>
23 #include <boost/asio/detail/throw_error.hpp>
24 #include <boost/asio/error.hpp>
26 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
27 # include <boost/asio/stream_socket_service.hpp>
28 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
30 #include <boost/asio/detail/push_options.hpp>
35 /// Provides stream-oriented socket functionality.
37 * The basic_stream_socket class template provides asynchronous and blocking
38 * stream-oriented socket functionality.
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
45 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
47 template <typename Protocol
48 BOOST_ASIO_SVC_TPARAM_DEF1(= stream_socket_service<Protocol>)>
49 class basic_stream_socket
50 : public basic_socket<Protocol BOOST_ASIO_SVC_TARG>
53 /// The native representation of a socket.
54 #if defined(GENERATING_DOCUMENTATION)
55 typedef implementation_defined native_handle_type;
57 typedef typename basic_socket<
58 Protocol BOOST_ASIO_SVC_TARG>::native_handle_type native_handle_type;
61 /// The protocol type.
62 typedef Protocol protocol_type;
64 /// The endpoint type.
65 typedef typename Protocol::endpoint endpoint_type;
67 /// Construct a basic_stream_socket without opening it.
69 * This constructor creates a stream socket without opening it. The socket
70 * needs to be opened and then connected or accepted before data can be sent
73 * @param io_context The io_context object that the stream socket will use to
74 * dispatch handlers for any asynchronous operations performed on the socket.
76 explicit basic_stream_socket(boost::asio::io_context& io_context)
77 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context)
81 /// Construct and open a basic_stream_socket.
83 * This constructor creates and opens a stream socket. The socket needs to be
84 * connected or accepted before data can be sent or received on it.
86 * @param io_context The io_context object that the stream socket will use to
87 * dispatch handlers for any asynchronous operations performed on the socket.
89 * @param protocol An object specifying protocol parameters to be used.
91 * @throws boost::system::system_error Thrown on failure.
93 basic_stream_socket(boost::asio::io_context& io_context,
94 const protocol_type& protocol)
95 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, protocol)
99 /// Construct a basic_stream_socket, opening it and binding it to the given
102 * This constructor creates a stream socket and automatically opens it bound
103 * to the specified endpoint on the local machine. The protocol used is the
104 * protocol associated with the given endpoint.
106 * @param io_context The io_context object that the stream socket will use to
107 * dispatch handlers for any asynchronous operations performed on the socket.
109 * @param endpoint An endpoint on the local machine to which the stream
110 * socket will be bound.
112 * @throws boost::system::system_error Thrown on failure.
114 basic_stream_socket(boost::asio::io_context& io_context,
115 const endpoint_type& endpoint)
116 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, endpoint)
120 /// Construct a basic_stream_socket on an existing native socket.
122 * This constructor creates a stream socket object to hold an existing native
125 * @param io_context The io_context object that the stream socket will use to
126 * dispatch handlers for any asynchronous operations performed on the socket.
128 * @param protocol An object specifying protocol parameters to be used.
130 * @param native_socket The new underlying socket implementation.
132 * @throws boost::system::system_error Thrown on failure.
134 basic_stream_socket(boost::asio::io_context& io_context,
135 const protocol_type& protocol, const native_handle_type& native_socket)
136 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(
137 io_context, protocol, native_socket)
141 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
142 /// Move-construct a basic_stream_socket from another.
144 * This constructor moves a stream socket from one object to another.
146 * @param other The other basic_stream_socket object from which the move
149 * @note Following the move, the moved-from object is in the same state as if
150 * constructed using the @c basic_stream_socket(io_context&) constructor.
152 basic_stream_socket(basic_stream_socket&& other)
153 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other))
157 /// Move-assign a basic_stream_socket from another.
159 * This assignment operator moves a stream socket from one object to another.
161 * @param other The other basic_stream_socket object from which the move
164 * @note Following the move, the moved-from object is in the same state as if
165 * constructed using the @c basic_stream_socket(io_context&) constructor.
167 basic_stream_socket& operator=(basic_stream_socket&& other)
169 basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other));
173 /// Move-construct a basic_stream_socket from a socket of another protocol
176 * This constructor moves a stream socket from one object to another.
178 * @param other The other basic_stream_socket object from which the move
181 * @note Following the move, the moved-from object is in the same state as if
182 * constructed using the @c basic_stream_socket(io_context&) constructor.
184 template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1>
186 basic_stream_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other,
187 typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
188 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other))
192 /// Move-assign a basic_stream_socket from a socket of another protocol type.
194 * This assignment operator moves a stream socket from one object to another.
196 * @param other The other basic_stream_socket object from which the move
199 * @note Following the move, the moved-from object is in the same state as if
200 * constructed using the @c basic_stream_socket(io_context&) constructor.
202 template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1>
203 typename enable_if<is_convertible<Protocol1, Protocol>::value,
204 basic_stream_socket>::type& operator=(
205 basic_stream_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other)
207 basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other));
210 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
212 /// Destroys the socket.
214 * This function destroys the socket, cancelling any outstanding asynchronous
215 * operations associated with the socket as if by calling @c cancel.
217 ~basic_stream_socket()
221 /// Send some data on the socket.
223 * This function is used to send data on the stream socket. The function
224 * call will block until one or more bytes of the data has been sent
225 * successfully, or an until error occurs.
227 * @param buffers One or more data buffers to be sent on the socket.
229 * @returns The number of bytes sent.
231 * @throws boost::system::system_error Thrown on failure.
233 * @note The send operation may not transmit all of the data to the peer.
234 * Consider using the @ref write function if you need to ensure that all data
235 * is written before the blocking operation completes.
238 * To send a single data buffer use the @ref buffer function as follows:
240 * socket.send(boost::asio::buffer(data, size));
242 * See the @ref buffer documentation for information on sending multiple
243 * buffers in one go, and how to use it with arrays, boost::array or
246 template <typename ConstBufferSequence>
247 std::size_t send(const ConstBufferSequence& buffers)
249 boost::system::error_code ec;
250 std::size_t s = this->get_service().send(
251 this->get_implementation(), buffers, 0, ec);
252 boost::asio::detail::throw_error(ec, "send");
256 /// Send some data on the socket.
258 * This function is used to send data on the stream socket. The function
259 * call will block until one or more bytes of the data has been sent
260 * successfully, or an until error occurs.
262 * @param buffers One or more data buffers to be sent on the socket.
264 * @param flags Flags specifying how the send call is to be made.
266 * @returns The number of bytes sent.
268 * @throws boost::system::system_error Thrown on failure.
270 * @note The send operation may not transmit all of the data to the peer.
271 * Consider using the @ref write function if you need to ensure that all data
272 * is written before the blocking operation completes.
275 * To send a single data buffer use the @ref buffer function as follows:
277 * socket.send(boost::asio::buffer(data, size), 0);
279 * See the @ref buffer documentation for information on sending multiple
280 * buffers in one go, and how to use it with arrays, boost::array or
283 template <typename ConstBufferSequence>
284 std::size_t send(const ConstBufferSequence& buffers,
285 socket_base::message_flags flags)
287 boost::system::error_code ec;
288 std::size_t s = this->get_service().send(
289 this->get_implementation(), buffers, flags, ec);
290 boost::asio::detail::throw_error(ec, "send");
294 /// Send some data on the socket.
296 * This function is used to send data on the stream socket. The function
297 * call will block until one or more bytes of the data has been sent
298 * successfully, or an until error occurs.
300 * @param buffers One or more data buffers to be sent on the socket.
302 * @param flags Flags specifying how the send call is to be made.
304 * @param ec Set to indicate what error occurred, if any.
306 * @returns The number of bytes sent. Returns 0 if an error occurred.
308 * @note The send operation may not transmit all of the data to the peer.
309 * Consider using the @ref write function if you need to ensure that all data
310 * is written before the blocking operation completes.
312 template <typename ConstBufferSequence>
313 std::size_t send(const ConstBufferSequence& buffers,
314 socket_base::message_flags flags, boost::system::error_code& ec)
316 return this->get_service().send(
317 this->get_implementation(), buffers, flags, ec);
320 /// Start an asynchronous send.
322 * This function is used to asynchronously send data on the stream socket.
323 * The function call always returns immediately.
325 * @param buffers One or more data buffers to be sent on the socket. Although
326 * the buffers object may be copied as necessary, ownership of the underlying
327 * memory blocks is retained by the caller, which must guarantee that they
328 * remain valid until the handler is called.
330 * @param handler The handler to be called when the send operation completes.
331 * Copies will be made of the handler as required. The function signature of
332 * the handler must be:
333 * @code void handler(
334 * const boost::system::error_code& error, // Result of operation.
335 * std::size_t bytes_transferred // Number of bytes sent.
337 * Regardless of whether the asynchronous operation completes immediately or
338 * not, the handler will not be invoked from within this function. Invocation
339 * of the handler will be performed in a manner equivalent to using
340 * boost::asio::io_context::post().
342 * @note The send operation may not transmit all of the data to the peer.
343 * Consider using the @ref async_write function if you need to ensure that all
344 * data is written before the asynchronous operation completes.
347 * To send a single data buffer use the @ref buffer function as follows:
349 * socket.async_send(boost::asio::buffer(data, size), handler);
351 * See the @ref buffer documentation for information on sending multiple
352 * buffers in one go, and how to use it with arrays, boost::array or
355 template <typename ConstBufferSequence, typename WriteHandler>
356 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
357 void (boost::system::error_code, std::size_t))
358 async_send(const ConstBufferSequence& buffers,
359 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
361 // If you get an error on the following line it means that your handler does
362 // not meet the documented type requirements for a WriteHandler.
363 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
365 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
366 return this->get_service().async_send(
367 this->get_implementation(), buffers, 0,
368 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
369 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
370 async_completion<WriteHandler,
371 void (boost::system::error_code, std::size_t)> init(handler);
373 this->get_service().async_send(
374 this->get_implementation(), buffers, 0,
375 init.completion_handler);
377 return init.result.get();
378 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
381 /// Start an asynchronous send.
383 * This function is used to asynchronously send data on the stream socket.
384 * The function call always returns immediately.
386 * @param buffers One or more data buffers to be sent on the socket. Although
387 * the buffers object may be copied as necessary, ownership of the underlying
388 * memory blocks is retained by the caller, which must guarantee that they
389 * remain valid until the handler is called.
391 * @param flags Flags specifying how the send call is to be made.
393 * @param handler The handler to be called when the send operation completes.
394 * Copies will be made of the handler as required. The function signature of
395 * the handler must be:
396 * @code void handler(
397 * const boost::system::error_code& error, // Result of operation.
398 * std::size_t bytes_transferred // Number of bytes sent.
400 * Regardless of whether the asynchronous operation completes immediately or
401 * not, the handler will not be invoked from within this function. Invocation
402 * of the handler will be performed in a manner equivalent to using
403 * boost::asio::io_context::post().
405 * @note The send operation may not transmit all of the data to the peer.
406 * Consider using the @ref async_write function if you need to ensure that all
407 * data is written before the asynchronous operation completes.
410 * To send a single data buffer use the @ref buffer function as follows:
412 * socket.async_send(boost::asio::buffer(data, size), 0, handler);
414 * See the @ref buffer documentation for information on sending multiple
415 * buffers in one go, and how to use it with arrays, boost::array or
418 template <typename ConstBufferSequence, typename WriteHandler>
419 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
420 void (boost::system::error_code, std::size_t))
421 async_send(const ConstBufferSequence& buffers,
422 socket_base::message_flags flags,
423 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
425 // If you get an error on the following line it means that your handler does
426 // not meet the documented type requirements for a WriteHandler.
427 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
429 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
430 return this->get_service().async_send(
431 this->get_implementation(), buffers, flags,
432 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
433 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
434 async_completion<WriteHandler,
435 void (boost::system::error_code, std::size_t)> init(handler);
437 this->get_service().async_send(
438 this->get_implementation(), buffers, flags,
439 init.completion_handler);
441 return init.result.get();
442 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
445 /// Receive some data on the socket.
447 * This function is used to receive data on the stream socket. The function
448 * call will block until one or more bytes of data has been received
449 * successfully, or until an error occurs.
451 * @param buffers One or more buffers into which the data will be received.
453 * @returns The number of bytes received.
455 * @throws boost::system::system_error Thrown on failure. An error code of
456 * boost::asio::error::eof indicates that the connection was closed by the
459 * @note The receive operation may not receive all of the requested number of
460 * bytes. Consider using the @ref read function if you need to ensure that the
461 * requested amount of data is read before the blocking operation completes.
464 * To receive into a single data buffer use the @ref buffer function as
467 * socket.receive(boost::asio::buffer(data, size));
469 * See the @ref buffer documentation for information on receiving into
470 * multiple buffers in one go, and how to use it with arrays, boost::array or
473 template <typename MutableBufferSequence>
474 std::size_t receive(const MutableBufferSequence& buffers)
476 boost::system::error_code ec;
477 std::size_t s = this->get_service().receive(
478 this->get_implementation(), buffers, 0, ec);
479 boost::asio::detail::throw_error(ec, "receive");
483 /// Receive some data on the socket.
485 * This function is used to receive data on the stream socket. The function
486 * call will block until one or more bytes of data has been received
487 * successfully, or until an error occurs.
489 * @param buffers One or more buffers into which the data will be received.
491 * @param flags Flags specifying how the receive call is to be made.
493 * @returns The number of bytes received.
495 * @throws boost::system::system_error Thrown on failure. An error code of
496 * boost::asio::error::eof indicates that the connection was closed by the
499 * @note The receive operation may not receive all of the requested number of
500 * bytes. Consider using the @ref read function if you need to ensure that the
501 * requested amount of data is read before the blocking operation completes.
504 * To receive into a single data buffer use the @ref buffer function as
507 * socket.receive(boost::asio::buffer(data, size), 0);
509 * See the @ref buffer documentation for information on receiving into
510 * multiple buffers in one go, and how to use it with arrays, boost::array or
513 template <typename MutableBufferSequence>
514 std::size_t receive(const MutableBufferSequence& buffers,
515 socket_base::message_flags flags)
517 boost::system::error_code ec;
518 std::size_t s = this->get_service().receive(
519 this->get_implementation(), buffers, flags, ec);
520 boost::asio::detail::throw_error(ec, "receive");
524 /// Receive some data on a connected socket.
526 * This function is used to receive data on the stream socket. The function
527 * call will block until one or more bytes of data has been received
528 * successfully, or until an error occurs.
530 * @param buffers One or more buffers into which the data will be received.
532 * @param flags Flags specifying how the receive call is to be made.
534 * @param ec Set to indicate what error occurred, if any.
536 * @returns The number of bytes received. Returns 0 if an error occurred.
538 * @note The receive operation may not receive all of the requested number of
539 * bytes. Consider using the @ref read function if you need to ensure that the
540 * requested amount of data is read before the blocking operation completes.
542 template <typename MutableBufferSequence>
543 std::size_t receive(const MutableBufferSequence& buffers,
544 socket_base::message_flags flags, boost::system::error_code& ec)
546 return this->get_service().receive(
547 this->get_implementation(), buffers, flags, ec);
550 /// Start an asynchronous receive.
552 * This function is used to asynchronously receive data from the stream
553 * socket. The function call always returns immediately.
555 * @param buffers One or more buffers into which the data will be received.
556 * Although the buffers object may be copied as necessary, ownership of the
557 * underlying memory blocks is retained by the caller, which must guarantee
558 * that they remain valid until the handler is called.
560 * @param handler The handler to be called when the receive operation
561 * completes. Copies will be made of the handler as required. The function
562 * signature of the handler must be:
563 * @code void handler(
564 * const boost::system::error_code& error, // Result of operation.
565 * std::size_t bytes_transferred // Number of bytes received.
567 * Regardless of whether the asynchronous operation completes immediately or
568 * not, the handler will not be invoked from within this function. Invocation
569 * of the handler will be performed in a manner equivalent to using
570 * boost::asio::io_context::post().
572 * @note The receive operation may not receive all of the requested number of
573 * bytes. Consider using the @ref async_read function if you need to ensure
574 * that the requested amount of data is received before the asynchronous
575 * operation completes.
578 * To receive into a single data buffer use the @ref buffer function as
581 * socket.async_receive(boost::asio::buffer(data, size), handler);
583 * See the @ref buffer documentation for information on receiving into
584 * multiple buffers in one go, and how to use it with arrays, boost::array or
587 template <typename MutableBufferSequence, typename ReadHandler>
588 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
589 void (boost::system::error_code, std::size_t))
590 async_receive(const MutableBufferSequence& buffers,
591 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
593 // If you get an error on the following line it means that your handler does
594 // not meet the documented type requirements for a ReadHandler.
595 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
597 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
598 return this->get_service().async_receive(this->get_implementation(),
599 buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
600 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
601 async_completion<ReadHandler,
602 void (boost::system::error_code, std::size_t)> init(handler);
604 this->get_service().async_receive(this->get_implementation(),
605 buffers, 0, init.completion_handler);
607 return init.result.get();
608 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
611 /// Start an asynchronous receive.
613 * This function is used to asynchronously receive data from the stream
614 * socket. The function call always returns immediately.
616 * @param buffers One or more buffers into which the data will be received.
617 * Although the buffers object may be copied as necessary, ownership of the
618 * underlying memory blocks is retained by the caller, which must guarantee
619 * that they remain valid until the handler is called.
621 * @param flags Flags specifying how the receive call is to be made.
623 * @param handler The handler to be called when the receive operation
624 * completes. Copies will be made of the handler as required. The function
625 * signature of the handler must be:
626 * @code void handler(
627 * const boost::system::error_code& error, // Result of operation.
628 * std::size_t bytes_transferred // Number of bytes received.
630 * Regardless of whether the asynchronous operation completes immediately or
631 * not, the handler will not be invoked from within this function. Invocation
632 * of the handler will be performed in a manner equivalent to using
633 * boost::asio::io_context::post().
635 * @note The receive operation may not receive all of the requested number of
636 * bytes. Consider using the @ref async_read function if you need to ensure
637 * that the requested amount of data is received before the asynchronous
638 * operation completes.
641 * To receive into a single data buffer use the @ref buffer function as
644 * socket.async_receive(boost::asio::buffer(data, size), 0, handler);
646 * See the @ref buffer documentation for information on receiving into
647 * multiple buffers in one go, and how to use it with arrays, boost::array or
650 template <typename MutableBufferSequence, typename ReadHandler>
651 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
652 void (boost::system::error_code, std::size_t))
653 async_receive(const MutableBufferSequence& buffers,
654 socket_base::message_flags flags,
655 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
657 // If you get an error on the following line it means that your handler does
658 // not meet the documented type requirements for a ReadHandler.
659 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
661 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
662 return this->get_service().async_receive(this->get_implementation(),
663 buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
664 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
665 async_completion<ReadHandler,
666 void (boost::system::error_code, std::size_t)> init(handler);
668 this->get_service().async_receive(this->get_implementation(),
669 buffers, flags, init.completion_handler);
671 return init.result.get();
672 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
675 /// Write some data to the socket.
677 * This function is used to write data to the stream socket. The function call
678 * will block until one or more bytes of the data has been written
679 * successfully, or until an error occurs.
681 * @param buffers One or more data buffers to be written to the socket.
683 * @returns The number of bytes written.
685 * @throws boost::system::system_error Thrown on failure. An error code of
686 * boost::asio::error::eof indicates that the connection was closed by the
689 * @note The write_some operation may not transmit all of the data to the
690 * peer. Consider using the @ref write function if you need to ensure that
691 * all data is written before the blocking operation completes.
694 * To write a single data buffer use the @ref buffer function as follows:
696 * socket.write_some(boost::asio::buffer(data, size));
698 * See the @ref buffer documentation for information on writing multiple
699 * buffers in one go, and how to use it with arrays, boost::array or
702 template <typename ConstBufferSequence>
703 std::size_t write_some(const ConstBufferSequence& buffers)
705 boost::system::error_code ec;
706 std::size_t s = this->get_service().send(
707 this->get_implementation(), buffers, 0, ec);
708 boost::asio::detail::throw_error(ec, "write_some");
712 /// Write some data to the socket.
714 * This function is used to write data to the stream socket. The function call
715 * will block until one or more bytes of the data has been written
716 * successfully, or until an error occurs.
718 * @param buffers One or more data buffers to be written to the socket.
720 * @param ec Set to indicate what error occurred, if any.
722 * @returns The number of bytes written. Returns 0 if an error occurred.
724 * @note The write_some operation may not transmit all of the data to the
725 * peer. Consider using the @ref write function if you need to ensure that
726 * all data is written before the blocking operation completes.
728 template <typename ConstBufferSequence>
729 std::size_t write_some(const ConstBufferSequence& buffers,
730 boost::system::error_code& ec)
732 return this->get_service().send(this->get_implementation(), buffers, 0, ec);
735 /// Start an asynchronous write.
737 * This function is used to asynchronously write data to the stream socket.
738 * The function call always returns immediately.
740 * @param buffers One or more data buffers to be written to the socket.
741 * Although the buffers object may be copied as necessary, ownership of the
742 * underlying memory blocks is retained by the caller, which must guarantee
743 * that they remain valid until the handler is called.
745 * @param handler The handler to be called when the write operation completes.
746 * Copies will be made of the handler as required. The function signature of
747 * the handler must be:
748 * @code void handler(
749 * const boost::system::error_code& error, // Result of operation.
750 * std::size_t bytes_transferred // Number of bytes written.
752 * Regardless of whether the asynchronous operation completes immediately or
753 * not, the handler will not be invoked from within this function. Invocation
754 * of the handler will be performed in a manner equivalent to using
755 * boost::asio::io_context::post().
757 * @note The write operation may not transmit all of the data to the peer.
758 * Consider using the @ref async_write function if you need to ensure that all
759 * data is written before the asynchronous operation completes.
762 * To write a single data buffer use the @ref buffer function as follows:
764 * socket.async_write_some(boost::asio::buffer(data, size), handler);
766 * See the @ref buffer documentation for information on writing multiple
767 * buffers in one go, and how to use it with arrays, boost::array or
770 template <typename ConstBufferSequence, typename WriteHandler>
771 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
772 void (boost::system::error_code, std::size_t))
773 async_write_some(const ConstBufferSequence& buffers,
774 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
776 // If you get an error on the following line it means that your handler does
777 // not meet the documented type requirements for a WriteHandler.
778 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
780 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
781 return this->get_service().async_send(this->get_implementation(),
782 buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
783 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
784 async_completion<WriteHandler,
785 void (boost::system::error_code, std::size_t)> init(handler);
787 this->get_service().async_send(this->get_implementation(),
788 buffers, 0, init.completion_handler);
790 return init.result.get();
791 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
794 /// Read some data from the socket.
796 * This function is used to read data from the stream socket. The function
797 * call will block until one or more bytes of data has been read successfully,
798 * or until an error occurs.
800 * @param buffers One or more buffers into which the data will be read.
802 * @returns The number of bytes read.
804 * @throws boost::system::system_error Thrown on failure. An error code of
805 * boost::asio::error::eof indicates that the connection was closed by the
808 * @note The read_some operation may not read all of the requested number of
809 * bytes. Consider using the @ref read function if you need to ensure that
810 * the requested amount of data is read before the blocking operation
814 * To read into a single data buffer use the @ref buffer function as follows:
816 * socket.read_some(boost::asio::buffer(data, size));
818 * See the @ref buffer documentation for information on reading into multiple
819 * buffers in one go, and how to use it with arrays, boost::array or
822 template <typename MutableBufferSequence>
823 std::size_t read_some(const MutableBufferSequence& buffers)
825 boost::system::error_code ec;
826 std::size_t s = this->get_service().receive(
827 this->get_implementation(), buffers, 0, ec);
828 boost::asio::detail::throw_error(ec, "read_some");
832 /// Read some data from the socket.
834 * This function is used to read data from the stream socket. The function
835 * call will block until one or more bytes of data has been read successfully,
836 * or until an error occurs.
838 * @param buffers One or more buffers into which the data will be read.
840 * @param ec Set to indicate what error occurred, if any.
842 * @returns The number of bytes read. Returns 0 if an error occurred.
844 * @note The read_some operation may not read all of the requested number of
845 * bytes. Consider using the @ref read function if you need to ensure that
846 * the requested amount of data is read before the blocking operation
849 template <typename MutableBufferSequence>
850 std::size_t read_some(const MutableBufferSequence& buffers,
851 boost::system::error_code& ec)
853 return this->get_service().receive(
854 this->get_implementation(), buffers, 0, ec);
857 /// Start an asynchronous read.
859 * This function is used to asynchronously read data from the stream socket.
860 * The function call always returns immediately.
862 * @param buffers One or more buffers into which the data will be read.
863 * Although the buffers object may be copied as necessary, ownership of the
864 * underlying memory blocks is retained by the caller, which must guarantee
865 * that they remain valid until the handler is called.
867 * @param handler The handler to be called when the read operation completes.
868 * Copies will be made of the handler as required. The function signature of
869 * the handler must be:
870 * @code void handler(
871 * const boost::system::error_code& error, // Result of operation.
872 * std::size_t bytes_transferred // Number of bytes read.
874 * Regardless of whether the asynchronous operation completes immediately or
875 * not, the handler will not be invoked from within this function. Invocation
876 * of the handler will be performed in a manner equivalent to using
877 * boost::asio::io_context::post().
879 * @note The read operation may not read all of the requested number of bytes.
880 * Consider using the @ref async_read function if you need to ensure that the
881 * requested amount of data is read before the asynchronous operation
885 * To read into a single data buffer use the @ref buffer function as follows:
887 * socket.async_read_some(boost::asio::buffer(data, size), handler);
889 * See the @ref buffer documentation for information on reading into multiple
890 * buffers in one go, and how to use it with arrays, boost::array or
893 template <typename MutableBufferSequence, typename ReadHandler>
894 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
895 void (boost::system::error_code, std::size_t))
896 async_read_some(const MutableBufferSequence& buffers,
897 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
899 // If you get an error on the following line it means that your handler does
900 // not meet the documented type requirements for a ReadHandler.
901 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
903 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
904 return this->get_service().async_receive(this->get_implementation(),
905 buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
906 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
907 async_completion<ReadHandler,
908 void (boost::system::error_code, std::size_t)> init(handler);
910 this->get_service().async_receive(this->get_implementation(),
911 buffers, 0, init.completion_handler);
913 return init.result.get();
914 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
921 #include <boost/asio/detail/pop_options.hpp>
923 #endif // BOOST_ASIO_BASIC_STREAM_SOCKET_HPP