
I'm moving an MFC net app to asio. In that app CAsyncSocket/CSocket are created using port=0, which MFC docs states: 'Windows Sockets to select a port'. Is there a similar facility in asio? If not any suggestions on a portable technique to get an available port? Thanks, Jeff

On 3/4/06, Jeff Flinn <TriumphSprint2000@hotmail.com> wrote: I'm moving an MFC net app to asio. In that app CAsyncSocket/CSocket are
created using port=0, which MFC docs states: 'Windows Sockets to select a port'. Is there a similar facility in asio? If not any suggestions on a portable technique to get an available port?
The default constructor for ipv4::tcp::endpoint initializes the port to 0. Just pass a default-constructed endpoint to your socket_acceptor's ctor or to its bind method. You can then use the get_local_endpoint method to get the value of the port that was allocated by the OS. -- Caleb Epstein caleb dot epstein at gmail dot com

Caleb Epstein wrote:
On 3/4/06, Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
I'm moving an MFC net app to asio. In that app CAsyncSocket/CSocket are
created using port=0, which MFC docs states: 'Windows Sockets to select a port'. Is there a similar facility in asio? If not any suggestions on a portable technique to get an available port?
The default constructor for ipv4::tcp::endpoint initializes the port to 0. Just pass a default-constructed endpoint to your socket_acceptor's ctor or to its bind method.
You can then use the get_local_endpoint method to get the value of the port that was allocated by the OS.
Thanks again Caleb. Jeff Flinn

Caleb Epstein wrote:
On 3/4/06, Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
...
You can then use the get_local_endpoint method to get the value of the port that was allocated by the OS.
This brings up one of the bothersome aspects of asio interface, IMHO. Functions like: template<typename Endpoint> void get_local_endpoint (Endpoint &endpoint) make it difficult to use in the context of an initializer list. typedef boost::asio::ipv4::tcp::endpoint tEndpoint; some_class( ... ) : mSocketPtr( ... ) ... , mPort( mSocketPtr->get_local_endpoint().port() ) {} obviously fails to compile. Rather than , mPort( mSocketPtr->local_endpoint_ref<tEndpoint>().port() ) Any reason this was not considered? The same goes for host and host resolver's get_host_by_name. Thanks, Jeff

On Mar 6, 2006, at 15:29, Jeff Flinn wrote:
Caleb Epstein wrote:
On 3/4/06, Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
...
You can then use the get_local_endpoint method to get the value of the port that was allocated by the OS.
This brings up one of the bothersome aspects of asio interface, IMHO. Functions like:
template<typename Endpoint> void get_local_endpoint (Endpoint &endpoint)
make it difficult to use in the context of an initializer list.
typedef boost::asio::ipv4::tcp::endpoint tEndpoint;
some_class( ... ) : mSocketPtr( ... ) ... , mPort( mSocketPtr->get_local_endpoint().port() ) {}
On the asio cvs there's a new member function, local_endpoint(), which returns the endpoint.
obviously fails to compile. Rather than
, mPort( mSocketPtr->local_endpoint_ref<tEndpoint>().port() )
Any reason this was not considered? The same goes for host and host resolver's get_host_by_name.
-- Arvid Norberg

Arvid Norberg wrote:
On Mar 6, 2006, at 15:29, Jeff Flinn wrote:
Caleb Epstein wrote:
On 3/4/06, Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
...
On the asio cvs there's a new member function, local_endpoint(), which returns the endpoint.
Aah, thank you. Does anyone know what the eta is on the asio review results? Or did I miss the posting? Thanks, Jeff

Hi Jeff, --- Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
This brings up one of the bothersome aspects of asio interface, IMHO.
Functions like:
template<typename Endpoint> void get_local_endpoint (Endpoint &endpoint)
make it difficult to use in the context of an initializer list.
As Arvid mentioned, this has already changed in asio's cvs repository. It's part of a change suggested during the review to add stronger typing for the socket classes. For example, you can now write: ip::tcp::socket socket(io_service); ... ip::tcp::endpoint ep = socket.local_endpoint();
Any reason this was not considered? The same goes for host and host resolver's get_host_by_name.
I intend to replace the host_resolver class with a new interface based on getaddrinfo/getnameinfo that can support both IPv4 and IPv6. This will probably return an iterator that can be used to enumerate all endpoints that match some criteria. E.g. something like: ip::tcp::resolver resolver(io_service); ip::tcp::resolver::query query("some.host", "daytime"); ip::tcp::resolver::iterator iter = resolver.resolve(query); ip::tcp::resolver::iterator end; while (iter != end) { ip::tcp::endpoint endpoint = iter->endpoint(); ... ++iter; } Cheers, Chris
participants (4)
-
Arvid Norberg
-
Caleb Epstein
-
Christopher Kohlhoff
-
Jeff Flinn