i'm using boost asio for some network communication and wondering why in the examples are sometimes using the
boost::asio::connect() is a higher level free function that will invoke
boost::asio::ip::tcp::socket::connect(). The documentation is fairly clear here
This function attempts to connect a socket to one of a sequence of endpoints. It does this by repeated calls to the socket's connect member function, once for each endpoint in the sequence, until a connection is successfully established.
Neither has better behavior, though that is largely a subjective term. It's up to you and your application how you want to handle connection establishment. If you need to provide some sort of feedback to the user, ex:
"attempting to connect to host 1 of 10" it might be preferable to use
socket.connect() in a loop rather than the
connect() free function.
connect() free function takes an endpoint iterator so it can connect to all possible endpoints that are resolved. The tcp echo client that you linked to does this
tcp::resolver resolver(io_service); tcp::resolver::query query(tcp::v4(), argv, argv); tcp::resolver::iterator iterator = resolver.resolve(query); tcp::socket s(io_service); boost::asio::connect(s, iterator);