[asio] available port

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[asio] available port

Jeff Flinn
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



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [asio] available port

Caleb Epstein-3
On 3/4/06, Jeff Flinn <[hidden email]> 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
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [asio] available port

Jeff Flinn
Caleb Epstein wrote:

> On 3/4/06, Jeff Flinn <[hidden email]> 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



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [asio] available port

Jeff Flinn
In reply to this post by Caleb Epstein-3
Caleb Epstein wrote:
> On 3/4/06, Jeff Flinn <[hidden email]> 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






_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [asio] available port

Arvid Norberg

On Mar 6, 2006, at 15:29, Jeff Flinn wrote:

> Caleb Epstein wrote:
>> On 3/4/06, Jeff Flinn <[hidden email]> 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


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [asio] available port

Jeff Flinn
Arvid Norberg wrote:
> On Mar 6, 2006, at 15:29, Jeff Flinn wrote:
>
>> Caleb Epstein wrote:
>>> On 3/4/06, Jeff Flinn <[hidden email]> 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



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [asio] available port

Christopher Kohlhoff
In reply to this post by Jeff Flinn
Hi Jeff,

--- Jeff Flinn <[hidden email]> 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



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost