| Home | Libraries | People | FAQ | More

###### basic_stream::connect (7 of 10 overloads)

Establishes a connection by trying each endpoint in a sequence.

<h7> Synopsis </h7>

    template<
        class EndpointSequence,
        class ConnectCondition>
    Protocol::endpoint
    connect(
        EndpointSequence const& endpoints,
        ConnectCondition connect_condition);

<h7> Description </h7>

This function attempts to connect the stream to one of a sequence of endpoints by trying each endpoint until a connection is successfully established. The underlying socket is automatically opened if needed. An automatically opened socket is not returned to the closed state upon failure. The algorithm, known as a composed operation, is implemented in terms of calls to the underlying socket's `connect` function.

<h7> Parameters </h7>

Name | Description
--- | ---
`endpoints` | A sequence of endpoints.

`connect_condition`

A function object that is called prior to each connection attempt. The signature of the function object must be:

    bool connect_condition(
        error_code const& ec,
        typename Protocol::endpoint const& next);

The `ec` parameter contains the result from the most recent connect operation. Before the first connection attempt, `ec` is always set to indicate success. The `next` parameter is the next endpoint to be tried. The function object should return true if the next endpoint should be tried, and false if it should be skipped.

<h7> Return Value </h7>

The successfully connected endpoint.

<h7> Exceptions </h7>

Type | Thrown On
--- | ---
`boost::system::system_error` | Thrown on failure. If the sequence is empty, the associated error code is `net::error::not_found`. Otherwise, contains the error from the last connection attempt.