Provides socket functionality. More...
#include <basic_socket.hpp>
Public Types | |
typedef Protocol::endpoint | endpoint_type |
The endpoint type. | |
typedef basic_socket< Protocol, SocketService > | lowest_layer_type |
A basic_socket is always the lowest layer. | |
typedef SocketService::native_type | native_type |
The native representation of a socket. | |
typedef Protocol | protocol_type |
The protocol type. | |
Public Member Functions | |
void | assign (const protocol_type &protocol, const native_type &native_socket) |
Assign an existing native socket to the socket. | |
asio::error_code | assign (const protocol_type &protocol, const native_type &native_socket, asio::error_code &ec) |
Assign an existing native socket to the socket. | |
template<typename ConnectHandler > | |
void | async_connect (const endpoint_type &peer_endpoint, ConnectHandler handler) |
Start an asynchronous connect. | |
bool | at_mark () const |
Determine whether the socket is at the out-of-band data mark. | |
bool | at_mark (asio::error_code &ec) const |
Determine whether the socket is at the out-of-band data mark. | |
std::size_t | available () const |
Determine the number of bytes available for reading. | |
std::size_t | available (asio::error_code &ec) const |
Determine the number of bytes available for reading. | |
basic_socket (asio::io_service &io_service) | |
Construct a basic_socket without opening it. | |
basic_socket (asio::io_service &io_service, const protocol_type &protocol) | |
Construct and open a basic_socket. | |
basic_socket (asio::io_service &io_service, const endpoint_type &endpoint) | |
basic_socket (asio::io_service &io_service, const protocol_type &protocol, const native_type &native_socket) | |
Construct a basic_socket on an existing native socket. | |
void | bind (const endpoint_type &endpoint) |
Bind the socket to the given local endpoint. | |
asio::error_code | bind (const endpoint_type &endpoint, asio::error_code &ec) |
Bind the socket to the given local endpoint. | |
void | cancel () |
Cancel all asynchronous operations associated with the socket. | |
asio::error_code | cancel (asio::error_code &ec) |
Cancel all asynchronous operations associated with the socket. | |
void | close () |
Close the socket. | |
asio::error_code | close (asio::error_code &ec) |
Close the socket. | |
void | connect (const endpoint_type &peer_endpoint) |
Connect the socket to the specified endpoint. | |
asio::error_code | connect (const endpoint_type &peer_endpoint, asio::error_code &ec) |
Connect the socket to the specified endpoint. | |
template<typename GettableSocketOption > | |
void | get_option (GettableSocketOption &option) const |
Get an option from the socket. | |
template<typename GettableSocketOption > | |
asio::error_code | get_option (GettableSocketOption &option, asio::error_code &ec) const |
Get an option from the socket. | |
template<typename IoControlCommand > | |
void | io_control (IoControlCommand &command) |
Perform an IO control command on the socket. | |
template<typename IoControlCommand > | |
asio::error_code | io_control (IoControlCommand &command, asio::error_code &ec) |
Perform an IO control command on the socket. | |
bool | is_open () const |
Determine whether the socket is open. | |
endpoint_type | local_endpoint () const |
Get the local endpoint of the socket. | |
endpoint_type | local_endpoint (asio::error_code &ec) const |
Get the local endpoint of the socket. | |
lowest_layer_type & | lowest_layer () |
Get a reference to the lowest layer. | |
native_type | native () |
Get the native socket representation. | |
void | open (const protocol_type &protocol=protocol_type()) |
Open the socket using the specified protocol. | |
asio::error_code | open (const protocol_type &protocol, asio::error_code &ec) |
Open the socket using the specified protocol. | |
endpoint_type | remote_endpoint () const |
Get the remote endpoint of the socket. | |
endpoint_type | remote_endpoint (asio::error_code &ec) const |
Get the remote endpoint of the socket. | |
template<typename SettableSocketOption > | |
void | set_option (const SettableSocketOption &option) |
Set an option on the socket. | |
template<typename SettableSocketOption > | |
asio::error_code | set_option (const SettableSocketOption &option, asio::error_code &ec) |
Set an option on the socket. | |
void | shutdown (shutdown_type what) |
Disable sends or receives on the socket. | |
asio::error_code | shutdown (shutdown_type what, asio::error_code &ec) |
Disable sends or receives on the socket. | |
Protected Member Functions | |
~basic_socket () | |
Protected destructor to prevent deletion through this type. |
Provides socket functionality.
The basic_socket class template provides functionality that is common to both stream-oriented and datagram-oriented sockets.
Definition at line 41 of file basic_socket.hpp.
typedef Protocol::endpoint asio::basic_socket< Protocol, SocketService >::endpoint_type |
The endpoint type.
Reimplemented in asio::basic_socket_streambuf< Protocol, StreamSocketService >, asio::basic_stream_socket< Protocol, StreamSocketService >, asio::basic_datagram_socket< Protocol, DatagramSocketService >, and asio::basic_raw_socket< Protocol, RawSocketService >.
Definition at line 53 of file basic_socket.hpp.
typedef basic_socket<Protocol, SocketService> asio::basic_socket< Protocol, SocketService >::lowest_layer_type |
A basic_socket is always the lowest layer.
Definition at line 56 of file basic_socket.hpp.
typedef SocketService::native_type asio::basic_socket< Protocol, SocketService >::native_type |
The native representation of a socket.
Reimplemented in asio::basic_stream_socket< Protocol, StreamSocketService >, asio::basic_datagram_socket< Protocol, DatagramSocketService >, and asio::basic_raw_socket< Protocol, RawSocketService >.
Definition at line 47 of file basic_socket.hpp.
typedef Protocol asio::basic_socket< Protocol, SocketService >::protocol_type |
The protocol type.
Reimplemented in asio::basic_stream_socket< Protocol, StreamSocketService >, asio::basic_datagram_socket< Protocol, DatagramSocketService >, and asio::basic_raw_socket< Protocol, RawSocketService >.
Definition at line 50 of file basic_socket.hpp.
asio::basic_socket< Protocol, SocketService >::basic_socket | ( | asio::io_service & | io_service | ) | [inline, explicit] |
Construct a basic_socket without opening it.
This constructor creates a socket without opening it.
io_service | The io_service object that the socket will use to dispatch handlers for any asynchronous operations performed on the socket. |
Definition at line 65 of file basic_socket.hpp.
asio::basic_socket< Protocol, SocketService >::basic_socket | ( | asio::io_service & | io_service, |
const protocol_type & | protocol | ||
) | [inline] |
Construct and open a basic_socket.
This constructor creates and opens a socket.
io_service | The io_service object that the socket will use to dispatch handlers for any asynchronous operations performed on the socket. |
protocol | An object specifying protocol parameters to be used. |
asio::system_error | Thrown on failure. |
Definition at line 81 of file basic_socket.hpp.
asio::basic_socket< Protocol, SocketService >::basic_socket | ( | asio::io_service & | io_service, |
const endpoint_type & | endpoint | ||
) | [inline] |
Construct a basic_socket, opening it and binding it to the given local endpoint. This constructor creates a socket and automatically opens it bound to the specified endpoint on the local machine. The protocol used is the protocol associated with the given endpoint.
io_service | The io_service object that the socket will use to dispatch handlers for any asynchronous operations performed on the socket. |
endpoint | An endpoint on the local machine to which the socket will be bound. |
asio::system_error | Thrown on failure. |
Definition at line 105 of file basic_socket.hpp.
asio::basic_socket< Protocol, SocketService >::basic_socket | ( | asio::io_service & | io_service, |
const protocol_type & | protocol, | ||
const native_type & | native_socket | ||
) | [inline] |
Construct a basic_socket on an existing native socket.
This constructor creates a socket object to hold an existing native socket.
io_service | The io_service object that the socket will use to dispatch handlers for any asynchronous operations performed on the socket. |
protocol | An object specifying protocol parameters to be used. |
native_socket | A native socket. |
asio::system_error | Thrown on failure. |
Definition at line 129 of file basic_socket.hpp.
asio::basic_socket< Protocol, SocketService >::~basic_socket | ( | ) | [inline, protected] |
Protected destructor to prevent deletion through this type.
Definition at line 1040 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::assign | ( | const protocol_type & | protocol, |
const native_type & | native_socket | ||
) | [inline] |
Assign an existing native socket to the socket.
Definition at line 208 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::assign | ( | const protocol_type & | protocol, |
const native_type & | native_socket, | ||
asio::error_code & | ec | ||
) | [inline] |
Assign an existing native socket to the socket.
Definition at line 225 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::async_connect | ( | const endpoint_type & | peer_endpoint, |
ConnectHandler | handler | ||
) | [inline] |
Start an asynchronous connect.
This function is used to asynchronously connect a socket to the specified remote endpoint. The function call always returns immediately.
The socket is automatically opened if it is not already open. If the connect fails, and the socket was automatically opened, the socket is not returned to the closed state.
peer_endpoint | The remote endpoint to which the socket will be connected. Copies will be made of the endpoint object as required. |
handler | The handler to be called when the connection operation completes. Copies will be made of the handler as required. The function signature of the handler must be: void handler( const asio::error_code& error // Result of operation ); |
void connect_handler(const asio::error_code& error) { if (!error) { // Connect succeeded. } } ... asio::ip::tcp::socket socket(io_service); asio::ip::tcp::endpoint endpoint( asio::ip::address::from_string("1.2.3.4"), 12345); socket.async_connect(endpoint, connect_handler);
Definition at line 629 of file basic_socket.hpp.
bool asio::basic_socket< Protocol, SocketService >::at_mark | ( | ) | const [inline] |
Determine whether the socket is at the out-of-band data mark.
This function is used to check whether the socket input is currently positioned at the out-of-band data mark.
asio::system_error | Thrown on failure. |
Definition at line 399 of file basic_socket.hpp.
bool asio::basic_socket< Protocol, SocketService >::at_mark | ( | asio::error_code & | ec | ) | const [inline] |
Determine whether the socket is at the out-of-band data mark.
This function is used to check whether the socket input is currently positioned at the out-of-band data mark.
ec | Set to indicate what error occurred, if any. |
Definition at line 417 of file basic_socket.hpp.
std::size_t asio::basic_socket< Protocol, SocketService >::available | ( | ) | const [inline] |
Determine the number of bytes available for reading.
This function is used to determine the number of bytes that may be read without blocking.
asio::system_error | Thrown on failure. |
Definition at line 432 of file basic_socket.hpp.
std::size_t asio::basic_socket< Protocol, SocketService >::available | ( | asio::error_code & | ec | ) | const [inline] |
Determine the number of bytes available for reading.
This function is used to determine the number of bytes that may be read without blocking.
ec | Set to indicate what error occurred, if any. |
Definition at line 450 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::bind | ( | const endpoint_type & | endpoint | ) | [inline] |
Bind the socket to the given local endpoint.
This function binds the socket to the specified endpoint on the local machine.
endpoint | An endpoint on the local machine to which the socket will be bound. |
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); socket.open(asio::ip::tcp::v4()); socket.bind(asio::ip::tcp::endpoint( asio::ip::tcp::v4(), 12345));
Definition at line 473 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::bind | ( | const endpoint_type & | endpoint, |
asio::error_code & | ec | ||
) | [inline] |
Bind the socket to the given local endpoint.
This function binds the socket to the specified endpoint on the local machine.
endpoint | An endpoint on the local machine to which the socket will be bound. |
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); socket.open(asio::ip::tcp::v4()); asio::error_code ec; socket.bind(asio::ip::tcp::endpoint( asio::ip::tcp::v4(), 12345), ec); if (ec) { // An error occurred. }
Definition at line 503 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::cancel | ( | ) | [inline] |
Cancel all asynchronous operations associated with the socket.
This function causes all outstanding asynchronous connect, send and receive operations to finish immediately, and the handlers for cancelled operations will be passed the asio::error::operation_aborted error.
asio::system_error | Thrown on failure. |
For portable cancellation, consider using one of the following alternatives:
When running on Windows Vista, Windows Server 2008, and later, the CancelIoEx function is always used. This function does not have the problems described above.
Definition at line 336 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::cancel | ( | asio::error_code & | ec | ) | [inline] |
Cancel all asynchronous operations associated with the socket.
This function causes all outstanding asynchronous connect, send and receive operations to finish immediately, and the handlers for cancelled operations will be passed the asio::error::operation_aborted error.
ec | Set to indicate what error occurred, if any. |
For portable cancellation, consider using one of the following alternatives:
When running on Windows Vista, Windows Server 2008, and later, the CancelIoEx function is always used. This function does not have the problems described above.
Definition at line 384 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::close | ( | ) | [inline] |
Close the socket.
This function is used to close the socket. Any asynchronous send, receive or connect operations will be cancelled immediately, and will complete with the asio::error::operation_aborted error.
asio::system_error | Thrown on failure. |
Reimplemented in asio::basic_socket_streambuf< Protocol, StreamSocketService >.
Definition at line 249 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::close | ( | asio::error_code & | ec | ) | [inline] |
Close the socket.
This function is used to close the socket. Any asynchronous send, receive or connect operations will be cancelled immediately, and will complete with the asio::error::operation_aborted error.
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); ... asio::error_code ec; socket.close(ec); if (ec) { // An error occurred. }
Definition at line 279 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::connect | ( | const endpoint_type & | peer_endpoint | ) | [inline] |
Connect the socket to the specified endpoint.
This function is used to connect a socket to the specified remote endpoint. The function call will block until the connection is successfully made or an error occurs.
The socket is automatically opened if it is not already open. If the connect fails, and the socket was automatically opened, the socket is not returned to the closed state.
peer_endpoint | The remote endpoint to which the socket will be connected. |
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); asio::ip::tcp::endpoint endpoint( asio::ip::address::from_string("1.2.3.4"), 12345); socket.connect(endpoint);
Reimplemented in asio::basic_socket_streambuf< Protocol, StreamSocketService >.
Definition at line 532 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::connect | ( | const endpoint_type & | peer_endpoint, |
asio::error_code & | ec | ||
) | [inline] |
Connect the socket to the specified endpoint.
This function is used to connect a socket to the specified remote endpoint. The function call will block until the connection is successfully made or an error occurs.
The socket is automatically opened if it is not already open. If the connect fails, and the socket was automatically opened, the socket is not returned to the closed state.
peer_endpoint | The remote endpoint to which the socket will be connected. |
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); asio::ip::tcp::endpoint endpoint( asio::ip::address::from_string("1.2.3.4"), 12345); asio::error_code ec; socket.connect(endpoint, ec); if (ec) { // An error occurred. }
Definition at line 572 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::get_option | ( | GettableSocketOption & | option | ) | const [inline] |
Get an option from the socket.
This function is used to get the current value of an option on the socket.
option | The option value to be obtained from the socket. |
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::socket::keep_alive option; socket.get_option(option); bool is_set = option.get();
Definition at line 770 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::get_option | ( | GettableSocketOption & | option, |
asio::error_code & | ec | ||
) | const [inline] |
Get an option from the socket.
This function is used to get the current value of an option on the socket.
option | The option value to be obtained from the socket. |
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::socket::keep_alive option; asio::error_code ec; socket.get_option(option, ec); if (ec) { // An error occurred. } bool is_set = option.get();
Definition at line 818 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::io_control | ( | IoControlCommand & | command | ) | [inline] |
Perform an IO control command on the socket.
This function is used to execute an IO control command on the socket.
command | The IO control command to be performed on the socket. |
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::socket::bytes_readable command; socket.io_control(command); std::size_t bytes_readable = command.get();
Definition at line 847 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::io_control | ( | IoControlCommand & | command, |
asio::error_code & | ec | ||
) | [inline] |
Perform an IO control command on the socket.
This function is used to execute an IO control command on the socket.
command | The IO control command to be performed on the socket. |
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::socket::bytes_readable command; asio::error_code ec; socket.io_control(command, ec); if (ec) { // An error occurred. } std::size_t bytes_readable = command.get();
Definition at line 882 of file basic_socket.hpp.
bool asio::basic_socket< Protocol, SocketService >::is_open | ( | ) | const [inline] |
Determine whether the socket is open.
Definition at line 233 of file basic_socket.hpp.
endpoint_type asio::basic_socket< Protocol, SocketService >::local_endpoint | ( | ) | const [inline] |
Get the local endpoint of the socket.
This function is used to obtain the locally bound endpoint of the socket.
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
Definition at line 903 of file basic_socket.hpp.
endpoint_type asio::basic_socket< Protocol, SocketService >::local_endpoint | ( | asio::error_code & | ec | ) | const [inline] |
Get the local endpoint of the socket.
This function is used to obtain the locally bound endpoint of the socket.
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); ... asio::error_code ec; asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec); if (ec) { // An error occurred. }
Definition at line 932 of file basic_socket.hpp.
lowest_layer_type& asio::basic_socket< Protocol, SocketService >::lowest_layer | ( | ) | [inline] |
Get a reference to the lowest layer.
This function returns a reference to the lowest layer in a stack of layers. Since a basic_socket cannot contain any further layers, it simply returns a reference to itself.
Definition at line 147 of file basic_socket.hpp.
native_type asio::basic_socket< Protocol, SocketService >::native | ( | ) | [inline] |
Get the native socket representation.
This function may be used to obtain the underlying representation of the socket. This is intended to allow access to native socket functionality that is not otherwise provided.
Definition at line 290 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::open | ( | const protocol_type & | protocol = protocol_type() | ) | [inline] |
Open the socket using the specified protocol.
This function opens the socket so that it will use the specified protocol.
protocol | An object specifying protocol parameters to be used. |
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); socket.open(asio::ip::tcp::v4());
Definition at line 166 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::open | ( | const protocol_type & | protocol, |
asio::error_code & | ec | ||
) | [inline] |
Open the socket using the specified protocol.
This function opens the socket so that it will use the specified protocol.
protocol | An object specifying which protocol is to be used. |
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); asio::error_code ec; socket.open(asio::ip::tcp::v4(), ec); if (ec) { // An error occurred. }
Definition at line 192 of file basic_socket.hpp.
endpoint_type asio::basic_socket< Protocol, SocketService >::remote_endpoint | ( | ) | const [inline] |
Get the remote endpoint of the socket.
This function is used to obtain the remote endpoint of the socket.
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
Definition at line 952 of file basic_socket.hpp.
endpoint_type asio::basic_socket< Protocol, SocketService >::remote_endpoint | ( | asio::error_code & | ec | ) | const [inline] |
Get the remote endpoint of the socket.
This function is used to obtain the remote endpoint of the socket.
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); ... asio::error_code ec; asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec); if (ec) { // An error occurred. }
Definition at line 981 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::set_option | ( | const SettableSocketOption & | option | ) | [inline] |
Set an option on the socket.
This function is used to set an option on the socket.
option | The new option value to be set on the socket. |
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::no_delay option(true); socket.set_option(option);
Definition at line 681 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::set_option | ( | const SettableSocketOption & | option, |
asio::error_code & | ec | ||
) | [inline] |
Set an option on the socket.
This function is used to set an option on the socket.
option | The new option value to be set on the socket. |
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); ... asio::ip::tcp::no_delay option(true); asio::error_code ec; socket.set_option(option, ec); if (ec) { // An error occurred. }
Definition at line 728 of file basic_socket.hpp.
void asio::basic_socket< Protocol, SocketService >::shutdown | ( | shutdown_type | what | ) | [inline] |
Disable sends or receives on the socket.
This function is used to disable send operations, receive operations, or both.
what | Determines what types of operation will no longer be allowed. |
asio::system_error | Thrown on failure. |
asio::ip::tcp::socket socket(io_service); ... socket.shutdown(asio::ip::tcp::socket::shutdown_send);
Definition at line 1003 of file basic_socket.hpp.
asio::error_code asio::basic_socket< Protocol, SocketService >::shutdown | ( | shutdown_type | what, |
asio::error_code & | ec | ||
) | [inline] |
Disable sends or receives on the socket.
This function is used to disable send operations, receive operations, or both.
what | Determines what types of operation will no longer be allowed. |
ec | Set to indicate what error occurred, if any. |
asio::ip::tcp::socket socket(io_service); ... asio::error_code ec; socket.shutdown(asio::ip::tcp::socket::shutdown_send, ec); if (ec) { // An error occurred. }
Definition at line 1032 of file basic_socket.hpp.