20 Socket algorithms [socket.algo]

20.1 Synchronous connect operations [socket.algo.connect]

template<class Protocol, class EndpointSequence> typename Protocol::endpoint connect(basic_socket<Protocol>& s, const EndpointSequence& endpoints); template<class Protocol, class EndpointSequence> typename Protocol::endpoint connect(basic_socket<Protocol>& s, const EndpointSequence& endpoints, error_code& ec);

Returns: connect(s, endpoints, [](auto, auto){ return true; }, ec).

template<class Protocol, class EndpointSequence, class ConnectCondition> typename Protocol::endpoint connect(basic_socket<Protocol>& s, const EndpointSequence& endpoints, ConnectCondition c); template<class Protocol, class EndpointSequence, class ConnectCondition> typename Protocol::endpoint connect(basic_socket<Protocol>& s, const EndpointSequence& endpoints, ConnectCondition c, error_code& ec);

Effects: Performs ec.clear(), then finds the first element ep in the sequence endpoints for which:

  • c(ec, ep) yields true;

  • s.close(ec) succeeds;

  • s.open(ep.protocol(), ec) succeeds; and

  • s.connect(ep, ec) succeeds.

Returns: typename Protocol::endpoint() if no such element is found, otherwise ep.

Error conditions:

  • socket_errc::not_found — if endpoints.empty() or if the function object c returned false for all elements in the sequence.

template<class Protocol, class InputIterator> InputIterator connect(basic_socket<Protocol>& s, InputIterator first, InputIterator last); template<class Protocol, class InputIterator> InputIterator connect(basic_socket<Protocol>& s, InputIterator first, InputIterator last, error_code& ec);

Returns: connect(s, first, last, [](auto, auto){ return true; }, ec).

template<class Protocol, class InputIterator, class ConnectCondition> InputIterator connect(basic_socket<Protocol>& s, InputIterator first, InputIterator last, ConnectCondition c); template<class Protocol, class InputIterator, class ConnectCondition> InputIterator connect(basic_socket<Protocol>& s, InputIterator first, InputIterator last, ConnectCondition c, error_code& ec);

Effects: Performs ec.clear(), then finds the first iterator i in the range [first, last) for which:

  • c(ec, *i) yields true;

  • s.close(ec) succeeds;

  • s.open(typename Protocol::endpoint(*i).protocol(), ec) succeeds; and

  • s.connect(*i, ec) succeeds.

Returns: last if no such iterator is found, otherwise i.

Error conditions:

  • socket_errc::not_found — if first == last or if the function object c returned false for all iterators in the range.

20.2 Asynchronous connect operations [socket.algo.async.connect]

template<class Protocol, class EndpointSequence, class CompletionToken> DEDUCED async_connect(basic_socket<Protocol>& s, const EndpointSequence& endpoints, CompletionToken&& token);

Returns:

async_connect(s, endpoints, [](auto, auto){ return true; }, forward<CompletionToken>(token))

template<class Protocol, class EndpointSequence, class ConnectCondition, class CompletionToken> DEDUCED async_connect(basic_socket<Protocol>& s, const EndpointSequence& endpoints, ConnectCondition c, CompletionToken&& token);

A composed asynchronous operation ([async.reqmts.async.composed]).

Completion signature: void(error_code ec, typename Protocol::endpoint ep).

Effects: Performs ec.clear(), then finds the first element ep in the sequence endpoints for which:

  • c(ec, ep) yields true;

  • s.close(ec) succeeds;

  • s.open(ep.protocol(), ec) succeeds; and

  • the asynchronous operation s.async_connect(ep, unspecified) succeeds.

ec is updated with the result of the s.async_connect(ep, unspecified) operation, if any. If no such element is found, or if the operation fails with one of the error conditions listed below, ep is set to typename Protocol::endpoint(). [ Note: The underlying close, open, and async_connect operations are performed sequentially.  — end note ]

Error conditions:

  • socket_errc::not_found — if endpoints.empty() or if the function object c returned false for all elements in the sequence.

  • errc::operation_canceled — if s.is_open() == false immediately following an async_connect operation on the underlying socket.

template<class Protocol, class InputIterator, class CompletionToken> DEDUCED async_connect(basic_socket<Protocol>& s, InputIterator first, InputIterator last, CompletionToken&& token);

Returns:

async_connect(s, first, last, [](auto, auto){ return true; },
              forward<CompletionToken>(token))

template<class Protocol, class InputIterator, class ConnectCondition, class CompletionToken> DEDUCED async_connect(basic_socket<Protocol>& s, InputIterator first, InputIterator last, ConnectCondition c, CompletionToken&& token);

A composed asynchronous operation ([async.reqmts.async.composed]).

Completion signature: void(error_code ec, InputIterator i).

Effects: Performs ec.clear(), then finds the first iterator i in the range [first, last) for which:

  • c(ec, *i) yields true;

  • s.close(ec) succeeds;

  • s.open(typename Protocol::endpoint(*i).protocol(), ec) succeeds; and

  • the asynchronous operation s.async_connect(*i, unspecified) succeeds.

ec is updated with the result of the s.async_connect(*i, unspecified) operation, if any. If no such iterator is found, or if the operation fails with one of the error conditions listed below, i is set to last. [ Note: The underlying close, open, and async_connect operations are performed sequentially.  — end note ]

Error conditions:

  • socket_errc::not_found — if first == last or if the function object c returned false for all iterators in the range.

  • errc::operation_canceled — if s.is_open() == false immediately following an async_connect operation on the underlying socket.