boost::corosio::tcp
Encapsulate the TCP protocol for socket creation.
Synopsis
Declared in <boost/corosio/tcp.hpp>
class tcp;
Description
This class identifies the TCP protocol and its address family (IPv4 or IPv6). It is used to parameterize socket and acceptor open() calls with a self‐documenting type.
The family(), type(), and protocol() members return the three integers passed to the operating system's socket() call. Their values are platform‐defined constants taken from the system socket headers. For an inline variant that includes those headers, use native_tcp.
Example
// set_option throws rather than returning an error code, unlike
// open/bind/listen. Precondition: acc is not already open -- open() is a
// no-op on an already-open acceptor, so an acceptor left over from a v4
// attempt would silently keep its v4 socket and fail later at bind().
std::error_code
open_an_ipv6_listener(corosio::io_context& ioc)
{
corosio::tcp_acceptor acc(ioc);
if (auto ec = acc.open(corosio::tcp::v6())) // IPv6 socket
return ec;
acc.set_option(corosio::socket_option::reuse_address(true));
if (auto ec =
acc.bind(corosio::endpoint(corosio::ipv6_address::any(), 8080)))
return ec;
if (auto ec = acc.listen())
return ec;
return {};
}
Type Aliases
Name |
Description |
The acceptor type to use with this protocol, |
|
The socket type to use with this protocol, |
Member Functions
Name |
Description |
Return the address family (AF_INET or AF_INET6). |
|
Return true if this is IPv6. |
See Also
native_tcp, tcp_socket, tcp_acceptor
Created with MrDocs