Boost logo

Boost Users :

Subject: [Boost-users] [Asio] How do I dynamically select a port?
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2009-07-18 16:44:28


Hi All

I'm new to Asio, and not a tcp/ip expert either, so if I'm approaching this
all wrong
then please tell me.

I have two process on one machine, one of which streams a file to the other.
In fact
there may be several 'others', but one other will suffice to phrase the
question.

I need to dynamically select port numbers on which to stream the files to
these other
processes, based on whether that port number is available, and once the
viability of
using a particular port is established that port number can be communicated
to the
other process(es) by other means.

So, the code sequence I have is this. At what point can I detect that use of
this
particular port number is ok? Is it appropriate to use
from_string("127.0.0.1"), or
should I use some other form for local sockets?

Comments appreciated.

using namespace boost;
using asio :: ip :: tcp;

unsigned short port = 2001;

asio :: io_service io;
tcp :: endpoint endpoint( asio :: ip :: address :: from_string( "127.0.0.1"
), port );
tcp :: acceptor acceptor( io, endpoint );
tcp :: iostream socket_stream;
acceptor.accept( * socket_stream.rdbuf( ) );

Thanks,

- Rob.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net