boost::asio::connect
throws on error. So the try {} block and the catch { } block that are commented out would be useful for detecting the error rather than crashing.
resolver.resolve(query) returns .end() instead of a valid iterator if the port is not found, so you could test the iterator first before attempting to call connect
From boost documentation:
- @throws boost::system::system_error Thrown on failure. If the sequence is
- empty, the associated @c error_code is boost::asio::error::not_found.
- Otherwise, contains the error from the last connection attempt.