I'm using boost::asio for network communication and I'm wondering why in the examples sometimes the socket.connect(endpoint)
and some other times the boost::asio::connect(socket, endpoint)
is used. According to the code boost::asio::connect
is calling the socket.connect
in a loop for endpoint iterators. So my question is:
Which is better behaviour? Using boost::asio::connect or socket.connect? Personaly I prefer the socket.connect, because I have just one endpoint. Or may I'm wrong and misunderstood the asio libs.
Also my second question is, why the endpoint is an iterator? How can it be possible, more then one connections when 1 ip and 1 port is given?
Also there is a boost::asio::write and socket.write...
The examples are:
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.
The 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[1], argv[2]);
tcp::resolver::iterator iterator = resolver.resolve(query);
tcp::socket s(io_service);
boost::asio::connect(s, iterator);