5
// Copyright (c) 2003-2008 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 ASIO_BASIC_SOCKET_HPP
12
#define ASIO_BASIC_SOCKET_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
21
#include <boost/config.hpp>
22
#include "asio/detail/pop_options.hpp"
24
#include "asio/basic_io_object.hpp"
25
#include "asio/error.hpp"
26
#include "asio/socket_base.hpp"
27
#include "asio/detail/throw_error.hpp"
31
/// Provides socket functionality.
33
* The basic_socket class template provides functionality that is common to both
34
* stream-oriented and datagram-oriented sockets.
37
* @e Distinct @e objects: Safe.@n
38
* @e Shared @e objects: Unsafe.
40
template <typename Protocol, typename SocketService>
42
: public basic_io_object<SocketService>,
46
/// The native representation of a socket.
47
typedef typename SocketService::native_type native_type;
49
/// The protocol type.
50
typedef Protocol protocol_type;
52
/// The endpoint type.
53
typedef typename Protocol::endpoint endpoint_type;
55
/// A basic_socket is always the lowest layer.
56
typedef basic_socket<Protocol, SocketService> lowest_layer_type;
58
/// Construct a basic_socket without opening it.
60
* This constructor creates a socket without opening it.
62
* @param io_service The io_service object that the socket will use to
63
* dispatch handlers for any asynchronous operations performed on the socket.
65
explicit basic_socket(asio::io_service& io_service)
66
: basic_io_object<SocketService>(io_service)
70
/// Construct and open a basic_socket.
72
* This constructor creates and opens a socket.
74
* @param io_service The io_service object that the socket will use to
75
* dispatch handlers for any asynchronous operations performed on the socket.
77
* @param protocol An object specifying protocol parameters to be used.
79
* @throws asio::system_error Thrown on failure.
81
basic_socket(asio::io_service& io_service,
82
const protocol_type& protocol)
83
: basic_io_object<SocketService>(io_service)
86
this->service.open(this->implementation, protocol, ec);
87
asio::detail::throw_error(ec);
90
/// Construct a basic_socket, opening it and binding it to the given local
93
* This constructor creates a socket and automatically opens it bound to the
94
* specified endpoint on the local machine. The protocol used is the protocol
95
* associated with the given endpoint.
97
* @param io_service The io_service object that the socket will use to
98
* dispatch handlers for any asynchronous operations performed on the socket.
100
* @param endpoint An endpoint on the local machine to which the socket will
103
* @throws asio::system_error Thrown on failure.
105
basic_socket(asio::io_service& io_service,
106
const endpoint_type& endpoint)
107
: basic_io_object<SocketService>(io_service)
110
this->service.open(this->implementation, endpoint.protocol(), ec);
111
asio::detail::throw_error(ec);
112
this->service.bind(this->implementation, endpoint, ec);
113
asio::detail::throw_error(ec);
116
/// Construct a basic_socket on an existing native socket.
118
* This constructor creates a socket object to hold an existing native socket.
120
* @param io_service The io_service object that the socket will use to
121
* dispatch handlers for any asynchronous operations performed on the socket.
123
* @param protocol An object specifying protocol parameters to be used.
125
* @param native_socket A native socket.
127
* @throws asio::system_error Thrown on failure.
129
basic_socket(asio::io_service& io_service,
130
const protocol_type& protocol, const native_type& native_socket)
131
: basic_io_object<SocketService>(io_service)
134
this->service.assign(this->implementation, protocol, native_socket, ec);
135
asio::detail::throw_error(ec);
138
/// Get a reference to the lowest layer.
140
* This function returns a reference to the lowest layer in a stack of
141
* layers. Since a basic_socket cannot contain any further layers, it simply
142
* returns a reference to itself.
144
* @return A reference to the lowest layer in the stack of layers. Ownership
145
* is not transferred to the caller.
147
lowest_layer_type& lowest_layer()
152
/// Open the socket using the specified protocol.
154
* This function opens the socket so that it will use the specified protocol.
156
* @param protocol An object specifying protocol parameters to be used.
158
* @throws asio::system_error Thrown on failure.
162
* asio::ip::tcp::socket socket(io_service);
163
* socket.open(asio::ip::tcp::v4());
166
void open(const protocol_type& protocol = protocol_type())
169
this->service.open(this->implementation, protocol, ec);
170
asio::detail::throw_error(ec);
173
/// Open the socket using the specified protocol.
175
* This function opens the socket so that it will use the specified protocol.
177
* @param protocol An object specifying which protocol is to be used.
179
* @param ec Set to indicate what error occurred, if any.
183
* asio::ip::tcp::socket socket(io_service);
184
* asio::error_code ec;
185
* socket.open(asio::ip::tcp::v4(), ec);
188
* // An error occurred.
192
asio::error_code open(const protocol_type& protocol,
193
asio::error_code& ec)
195
return this->service.open(this->implementation, protocol, ec);
198
/// Assign an existing native socket to the socket.
200
* This function opens the socket to hold an existing native socket.
202
* @param protocol An object specifying which protocol is to be used.
204
* @param native_socket A native socket.
206
* @throws asio::system_error Thrown on failure.
208
void assign(const protocol_type& protocol, const native_type& native_socket)
211
this->service.assign(this->implementation, protocol, native_socket, ec);
212
asio::detail::throw_error(ec);
215
/// Assign an existing native socket to the socket.
217
* This function opens the socket to hold an existing native socket.
219
* @param protocol An object specifying which protocol is to be used.
221
* @param native_socket A native socket.
223
* @param ec Set to indicate what error occurred, if any.
225
asio::error_code assign(const protocol_type& protocol,
226
const native_type& native_socket, asio::error_code& ec)
228
return this->service.assign(this->implementation,
229
protocol, native_socket, ec);
232
/// Determine whether the socket is open.
235
return this->service.is_open(this->implementation);
238
/// Close the socket.
240
* This function is used to close the socket. Any asynchronous send, receive
241
* or connect operations will be cancelled immediately, and will complete
242
* with the asio::error::operation_aborted error.
244
* @throws asio::system_error Thrown on failure.
246
* @note For portable behaviour with respect to graceful closure of a
247
* connected socket, call shutdown() before closing the socket.
252
this->service.close(this->implementation, ec);
253
asio::detail::throw_error(ec);
256
/// Close the socket.
258
* This function is used to close the socket. Any asynchronous send, receive
259
* or connect operations will be cancelled immediately, and will complete
260
* with the asio::error::operation_aborted error.
262
* @param ec Set to indicate what error occurred, if any.
266
* asio::ip::tcp::socket socket(io_service);
268
* asio::error_code ec;
272
* // An error occurred.
276
* @note For portable behaviour with respect to graceful closure of a
277
* connected socket, call shutdown() before closing the socket.
279
asio::error_code close(asio::error_code& ec)
281
return this->service.close(this->implementation, ec);
284
/// Get the native socket representation.
286
* This function may be used to obtain the underlying representation of the
287
* socket. This is intended to allow access to native socket functionality
288
* that is not otherwise provided.
292
return this->service.native(this->implementation);
295
/// Cancel all asynchronous operations associated with the socket.
297
* This function causes all outstanding asynchronous connect, send and receive
298
* operations to finish immediately, and the handlers for cancelled operations
299
* will be passed the asio::error::operation_aborted error.
301
* @throws asio::system_error Thrown on failure.
303
* @note Calls to cancel() will always fail with
304
* asio::error::operation_not_supported when run on Windows XP, Windows
305
* Server 2003, and earlier versions of Windows, unless
306
* ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
307
* two issues that should be considered before enabling its use:
309
* @li It will only cancel asynchronous operations that were initiated in the
312
* @li It can appear to complete without error, but the request to cancel the
313
* unfinished operations may be silently ignored by the operating system.
314
* Whether it works or not seems to depend on the drivers that are installed.
316
* For portable cancellation, consider using one of the following
319
* @li Disable asio's I/O completion port backend by defining
322
* @li Use the close() function to simultaneously cancel the outstanding
323
* operations and close the socket.
325
* When running on Windows Vista, Windows Server 2008, and later, the
326
* CancelIoEx function is always used. This function does not have the
327
* problems described above.
329
#if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
330
&& (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
331
&& !defined(ASIO_ENABLE_CANCELIO)
332
__declspec(deprecated("By default, this function always fails with "
333
"operation_not_supported when used on Windows XP, Windows Server 2003, "
334
"or earlier. Consult documentation for details."))
339
this->service.cancel(this->implementation, ec);
340
asio::detail::throw_error(ec);
343
/// Cancel all asynchronous operations associated with the socket.
345
* This function causes all outstanding asynchronous connect, send and receive
346
* operations to finish immediately, and the handlers for cancelled operations
347
* will be passed the asio::error::operation_aborted error.
349
* @param ec Set to indicate what error occurred, if any.
351
* @note Calls to cancel() will always fail with
352
* asio::error::operation_not_supported when run on Windows XP, Windows
353
* Server 2003, and earlier versions of Windows, unless
354
* ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
355
* two issues that should be considered before enabling its use:
357
* @li It will only cancel asynchronous operations that were initiated in the
360
* @li It can appear to complete without error, but the request to cancel the
361
* unfinished operations may be silently ignored by the operating system.
362
* Whether it works or not seems to depend on the drivers that are installed.
364
* For portable cancellation, consider using one of the following
367
* @li Disable asio's I/O completion port backend by defining
370
* @li Use the close() function to simultaneously cancel the outstanding
371
* operations and close the socket.
373
* When running on Windows Vista, Windows Server 2008, and later, the
374
* CancelIoEx function is always used. This function does not have the
375
* problems described above.
377
#if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
378
&& (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
379
&& !defined(ASIO_ENABLE_CANCELIO)
380
__declspec(deprecated("By default, this function always fails with "
381
"operation_not_supported when used on Windows XP, Windows Server 2003, "
382
"or earlier. Consult documentation for details."))
384
asio::error_code cancel(asio::error_code& ec)
386
return this->service.cancel(this->implementation, ec);
389
/// Determine whether the socket is at the out-of-band data mark.
391
* This function is used to check whether the socket input is currently
392
* positioned at the out-of-band data mark.
394
* @return A bool indicating whether the socket is at the out-of-band data
397
* @throws asio::system_error Thrown on failure.
402
bool b = this->service.at_mark(this->implementation, ec);
403
asio::detail::throw_error(ec);
407
/// Determine whether the socket is at the out-of-band data mark.
409
* This function is used to check whether the socket input is currently
410
* positioned at the out-of-band data mark.
412
* @param ec Set to indicate what error occurred, if any.
414
* @return A bool indicating whether the socket is at the out-of-band data
417
bool at_mark(asio::error_code& ec) const
419
return this->service.at_mark(this->implementation, ec);
422
/// Determine the number of bytes available for reading.
424
* This function is used to determine the number of bytes that may be read
427
* @return The number of bytes that may be read without blocking, or 0 if an
430
* @throws asio::system_error Thrown on failure.
432
std::size_t available() const
435
std::size_t s = this->service.available(this->implementation, ec);
436
asio::detail::throw_error(ec);
440
/// Determine the number of bytes available for reading.
442
* This function is used to determine the number of bytes that may be read
445
* @param ec Set to indicate what error occurred, if any.
447
* @return The number of bytes that may be read without blocking, or 0 if an
450
std::size_t available(asio::error_code& ec) const
452
return this->service.available(this->implementation, ec);
455
/// Bind the socket to the given local endpoint.
457
* This function binds the socket to the specified endpoint on the local
460
* @param endpoint An endpoint on the local machine to which the socket will
463
* @throws asio::system_error Thrown on failure.
467
* asio::ip::tcp::socket socket(io_service);
468
* socket.open(asio::ip::tcp::v4());
469
* socket.bind(asio::ip::tcp::endpoint(
470
* asio::ip::tcp::v4(), 12345));
473
void bind(const endpoint_type& endpoint)
476
this->service.bind(this->implementation, endpoint, ec);
477
asio::detail::throw_error(ec);
480
/// Bind the socket to the given local endpoint.
482
* This function binds the socket to the specified endpoint on the local
485
* @param endpoint An endpoint on the local machine to which the socket will
488
* @param ec Set to indicate what error occurred, if any.
492
* asio::ip::tcp::socket socket(io_service);
493
* socket.open(asio::ip::tcp::v4());
494
* asio::error_code ec;
495
* socket.bind(asio::ip::tcp::endpoint(
496
* asio::ip::tcp::v4(), 12345), ec);
499
* // An error occurred.
503
asio::error_code bind(const endpoint_type& endpoint,
504
asio::error_code& ec)
506
return this->service.bind(this->implementation, endpoint, ec);
509
/// Connect the socket to the specified endpoint.
511
* This function is used to connect a socket to the specified remote endpoint.
512
* The function call will block until the connection is successfully made or
515
* The socket is automatically opened if it is not already open. If the
516
* connect fails, and the socket was automatically opened, the socket is
517
* returned to the closed state.
519
* @param peer_endpoint The remote endpoint to which the socket will be
522
* @throws asio::system_error Thrown on failure.
526
* asio::ip::tcp::socket socket(io_service);
527
* asio::ip::tcp::endpoint endpoint(
528
* asio::ip::address::from_string("1.2.3.4"), 12345);
529
* socket.connect(endpoint);
532
void connect(const endpoint_type& peer_endpoint)
537
this->service.open(this->implementation, peer_endpoint.protocol(), ec);
538
asio::detail::throw_error(ec);
540
this->service.connect(this->implementation, peer_endpoint, ec);
541
asio::detail::throw_error(ec);
544
/// Connect the socket to the specified endpoint.
546
* This function is used to connect a socket to the specified remote endpoint.
547
* The function call will block until the connection is successfully made or
550
* The socket is automatically opened if it is not already open. If the
551
* connect fails, and the socket was automatically opened, the socket is
552
* returned to the closed state.
554
* @param peer_endpoint The remote endpoint to which the socket will be
557
* @param ec Set to indicate what error occurred, if any.
561
* asio::ip::tcp::socket socket(io_service);
562
* asio::ip::tcp::endpoint endpoint(
563
* asio::ip::address::from_string("1.2.3.4"), 12345);
564
* asio::error_code ec;
565
* socket.connect(endpoint, ec);
568
* // An error occurred.
572
asio::error_code connect(const endpoint_type& peer_endpoint,
573
asio::error_code& ec)
577
if (this->service.open(this->implementation,
578
peer_endpoint.protocol(), ec))
584
return this->service.connect(this->implementation, peer_endpoint, ec);
587
/// Start an asynchronous connect.
589
* This function is used to asynchronously connect a socket to the specified
590
* remote endpoint. The function call always returns immediately.
592
* The socket is automatically opened if it is not already open. If the
593
* connect fails, and the socket was automatically opened, the socket is
594
* returned to the closed state.
596
* @param peer_endpoint The remote endpoint to which the socket will be
597
* connected. Copies will be made of the endpoint object as required.
599
* @param handler The handler to be called when the connection operation
600
* completes. Copies will be made of the handler as required. The function
601
* signature of the handler must be:
602
* @code void handler(
603
* const asio::error_code& error // Result of operation
605
* Regardless of whether the asynchronous operation completes immediately or
606
* not, the handler will not be invoked from within this function. Invocation
607
* of the handler will be performed in a manner equivalent to using
608
* asio::io_service::post().
612
* void connect_handler(const asio::error_code& error)
616
* // Connect succeeded.
622
* asio::ip::tcp::socket socket(io_service);
623
* asio::ip::tcp::endpoint endpoint(
624
* asio::ip::address::from_string("1.2.3.4"), 12345);
625
* socket.async_connect(endpoint, connect_handler);
628
template <typename ConnectHandler>
629
void async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler)
634
if (this->service.open(this->implementation,
635
peer_endpoint.protocol(), ec))
637
this->get_io_service().post(
638
asio::detail::bind_handler(handler, ec));
643
this->service.async_connect(this->implementation, peer_endpoint, handler);
646
/// Set an option on the socket.
648
* This function is used to set an option on the socket.
650
* @param option The new option value to be set on the socket.
652
* @throws asio::system_error Thrown on failure.
654
* @sa SettableSocketOption @n
655
* asio::socket_base::broadcast @n
656
* asio::socket_base::do_not_route @n
657
* asio::socket_base::keep_alive @n
658
* asio::socket_base::linger @n
659
* asio::socket_base::receive_buffer_size @n
660
* asio::socket_base::receive_low_watermark @n
661
* asio::socket_base::reuse_address @n
662
* asio::socket_base::send_buffer_size @n
663
* asio::socket_base::send_low_watermark @n
664
* asio::ip::multicast::join_group @n
665
* asio::ip::multicast::leave_group @n
666
* asio::ip::multicast::enable_loopback @n
667
* asio::ip::multicast::outbound_interface @n
668
* asio::ip::multicast::hops @n
669
* asio::ip::tcp::no_delay
672
* Setting the IPPROTO_TCP/TCP_NODELAY option:
674
* asio::ip::tcp::socket socket(io_service);
676
* asio::ip::tcp::no_delay option(true);
677
* socket.set_option(option);
680
template <typename SettableSocketOption>
681
void set_option(const SettableSocketOption& option)
684
this->service.set_option(this->implementation, option, ec);
685
asio::detail::throw_error(ec);
688
/// Set an option on the socket.
690
* This function is used to set an option on the socket.
692
* @param option The new option value to be set on the socket.
694
* @param ec Set to indicate what error occurred, if any.
696
* @sa SettableSocketOption @n
697
* asio::socket_base::broadcast @n
698
* asio::socket_base::do_not_route @n
699
* asio::socket_base::keep_alive @n
700
* asio::socket_base::linger @n
701
* asio::socket_base::receive_buffer_size @n
702
* asio::socket_base::receive_low_watermark @n
703
* asio::socket_base::reuse_address @n
704
* asio::socket_base::send_buffer_size @n
705
* asio::socket_base::send_low_watermark @n
706
* asio::ip::multicast::join_group @n
707
* asio::ip::multicast::leave_group @n
708
* asio::ip::multicast::enable_loopback @n
709
* asio::ip::multicast::outbound_interface @n
710
* asio::ip::multicast::hops @n
711
* asio::ip::tcp::no_delay
714
* Setting the IPPROTO_TCP/TCP_NODELAY option:
716
* asio::ip::tcp::socket socket(io_service);
718
* asio::ip::tcp::no_delay option(true);
719
* asio::error_code ec;
720
* socket.set_option(option, ec);
723
* // An error occurred.
727
template <typename SettableSocketOption>
728
asio::error_code set_option(const SettableSocketOption& option,
729
asio::error_code& ec)
731
return this->service.set_option(this->implementation, option, ec);
734
/// Get an option from the socket.
736
* This function is used to get the current value of an option on the socket.
738
* @param option The option value to be obtained from the socket.
740
* @throws asio::system_error Thrown on failure.
742
* @sa GettableSocketOption @n
743
* asio::socket_base::broadcast @n
744
* asio::socket_base::do_not_route @n
745
* asio::socket_base::keep_alive @n
746
* asio::socket_base::linger @n
747
* asio::socket_base::receive_buffer_size @n
748
* asio::socket_base::receive_low_watermark @n
749
* asio::socket_base::reuse_address @n
750
* asio::socket_base::send_buffer_size @n
751
* asio::socket_base::send_low_watermark @n
752
* asio::ip::multicast::join_group @n
753
* asio::ip::multicast::leave_group @n
754
* asio::ip::multicast::enable_loopback @n
755
* asio::ip::multicast::outbound_interface @n
756
* asio::ip::multicast::hops @n
757
* asio::ip::tcp::no_delay
760
* Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
762
* asio::ip::tcp::socket socket(io_service);
764
* asio::ip::tcp::socket::keep_alive option;
765
* socket.get_option(option);
766
* bool is_set = option.get();
769
template <typename GettableSocketOption>
770
void get_option(GettableSocketOption& option) const
773
this->service.get_option(this->implementation, option, ec);
774
asio::detail::throw_error(ec);
777
/// Get an option from the socket.
779
* This function is used to get the current value of an option on the socket.
781
* @param option The option value to be obtained from the socket.
783
* @param ec Set to indicate what error occurred, if any.
785
* @sa GettableSocketOption @n
786
* asio::socket_base::broadcast @n
787
* asio::socket_base::do_not_route @n
788
* asio::socket_base::keep_alive @n
789
* asio::socket_base::linger @n
790
* asio::socket_base::receive_buffer_size @n
791
* asio::socket_base::receive_low_watermark @n
792
* asio::socket_base::reuse_address @n
793
* asio::socket_base::send_buffer_size @n
794
* asio::socket_base::send_low_watermark @n
795
* asio::ip::multicast::join_group @n
796
* asio::ip::multicast::leave_group @n
797
* asio::ip::multicast::enable_loopback @n
798
* asio::ip::multicast::outbound_interface @n
799
* asio::ip::multicast::hops @n
800
* asio::ip::tcp::no_delay
803
* Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
805
* asio::ip::tcp::socket socket(io_service);
807
* asio::ip::tcp::socket::keep_alive option;
808
* asio::error_code ec;
809
* socket.get_option(option, ec);
812
* // An error occurred.
814
* bool is_set = option.get();
817
template <typename GettableSocketOption>
818
asio::error_code get_option(GettableSocketOption& option,
819
asio::error_code& ec) const
821
return this->service.get_option(this->implementation, option, ec);
824
/// Perform an IO control command on the socket.
826
* This function is used to execute an IO control command on the socket.
828
* @param command The IO control command to be performed on the socket.
830
* @throws asio::system_error Thrown on failure.
832
* @sa IoControlCommand @n
833
* asio::socket_base::bytes_readable @n
834
* asio::socket_base::non_blocking_io
837
* Getting the number of bytes ready to read:
839
* asio::ip::tcp::socket socket(io_service);
841
* asio::ip::tcp::socket::bytes_readable command;
842
* socket.io_control(command);
843
* std::size_t bytes_readable = command.get();
846
template <typename IoControlCommand>
847
void io_control(IoControlCommand& command)
850
this->service.io_control(this->implementation, command, ec);
851
asio::detail::throw_error(ec);
854
/// Perform an IO control command on the socket.
856
* This function is used to execute an IO control command on the socket.
858
* @param command The IO control command to be performed on the socket.
860
* @param ec Set to indicate what error occurred, if any.
862
* @sa IoControlCommand @n
863
* asio::socket_base::bytes_readable @n
864
* asio::socket_base::non_blocking_io
867
* Getting the number of bytes ready to read:
869
* asio::ip::tcp::socket socket(io_service);
871
* asio::ip::tcp::socket::bytes_readable command;
872
* asio::error_code ec;
873
* socket.io_control(command, ec);
876
* // An error occurred.
878
* std::size_t bytes_readable = command.get();
881
template <typename IoControlCommand>
882
asio::error_code io_control(IoControlCommand& command,
883
asio::error_code& ec)
885
return this->service.io_control(this->implementation, command, ec);
888
/// Get the local endpoint of the socket.
890
* This function is used to obtain the locally bound endpoint of the socket.
892
* @returns An object that represents the local endpoint of the socket.
894
* @throws asio::system_error Thrown on failure.
898
* asio::ip::tcp::socket socket(io_service);
900
* asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
903
endpoint_type local_endpoint() const
906
endpoint_type ep = this->service.local_endpoint(this->implementation, ec);
907
asio::detail::throw_error(ec);
911
/// Get the local endpoint of the socket.
913
* This function is used to obtain the locally bound endpoint of the socket.
915
* @param ec Set to indicate what error occurred, if any.
917
* @returns An object that represents the local endpoint of the socket.
918
* Returns a default-constructed endpoint object if an error occurred.
922
* asio::ip::tcp::socket socket(io_service);
924
* asio::error_code ec;
925
* asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
928
* // An error occurred.
932
endpoint_type local_endpoint(asio::error_code& ec) const
934
return this->service.local_endpoint(this->implementation, ec);
937
/// Get the remote endpoint of the socket.
939
* This function is used to obtain the remote endpoint of the socket.
941
* @returns An object that represents the remote endpoint of the socket.
943
* @throws asio::system_error Thrown on failure.
947
* asio::ip::tcp::socket socket(io_service);
949
* asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
952
endpoint_type remote_endpoint() const
955
endpoint_type ep = this->service.remote_endpoint(this->implementation, ec);
956
asio::detail::throw_error(ec);
960
/// Get the remote endpoint of the socket.
962
* This function is used to obtain the remote endpoint of the socket.
964
* @param ec Set to indicate what error occurred, if any.
966
* @returns An object that represents the remote endpoint of the socket.
967
* Returns a default-constructed endpoint object if an error occurred.
971
* asio::ip::tcp::socket socket(io_service);
973
* asio::error_code ec;
974
* asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
977
* // An error occurred.
981
endpoint_type remote_endpoint(asio::error_code& ec) const
983
return this->service.remote_endpoint(this->implementation, ec);
986
/// Disable sends or receives on the socket.
988
* This function is used to disable send operations, receive operations, or
991
* @param what Determines what types of operation will no longer be allowed.
993
* @throws asio::system_error Thrown on failure.
996
* Shutting down the send side of the socket:
998
* asio::ip::tcp::socket socket(io_service);
1000
* socket.shutdown(asio::ip::tcp::socket::shutdown_send);
1003
void shutdown(shutdown_type what)
1005
asio::error_code ec;
1006
this->service.shutdown(this->implementation, what, ec);
1007
asio::detail::throw_error(ec);
1010
/// Disable sends or receives on the socket.
1012
* This function is used to disable send operations, receive operations, or
1015
* @param what Determines what types of operation will no longer be allowed.
1017
* @param ec Set to indicate what error occurred, if any.
1020
* Shutting down the send side of the socket:
1022
* asio::ip::tcp::socket socket(io_service);
1024
* asio::error_code ec;
1025
* socket.shutdown(asio::ip::tcp::socket::shutdown_send, ec);
1028
* // An error occurred.
1032
asio::error_code shutdown(shutdown_type what,
1033
asio::error_code& ec)
1035
return this->service.shutdown(this->implementation, what, ec);
1039
/// Protected destructor to prevent deletion through this type.
1047
#include "asio/detail/pop_options.hpp"
1049
#endif // ASIO_BASIC_SOCKET_HPP