You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Daniel Becroft <dj...@gmail.com> on 2011/05/02 23:30:37 UTC

Finding an available port number

Hi,

Is there a task or script that can be used to generate a (random) port
number that is known to be available? I'm trying to setup some server
instances for testing purposes, and I'd prefer not to hard-code port
numbers.

I'm running on Windows, so I don't have awk/sed available by default,
either.

Cheers,
---
Daniel Becroft

Re: Finding an available port number

Posted by "Steele, Richard" <ri...@steelezone.net>.
This doesn't directly answer your question, but Hudson/Jenkins does have the
port allocator plugin which does much as you suggest, but of course only in
the context of running a job.  See
https://wiki.jenkins-ci.org/display/JENKINS/Port+Allocator+Plugin.

Rich

On Mon, May 2, 2011 at 8:12 PM, Daniel Becroft <dj...@gmail.com> wrote:

> THanks, I was aware that I could do it through a Java (or similar) command.
> What I've got is an Ant task that initializes there server instance with a
> specified port number (unfortunately there's no facility to pass in 0
> here).
> I was after another task so I could so something like:
>
> <target>
>  <getavailableport property="port.number" />
>
>  <createserver .... port="${port.number}" />
> </target>
>
> Cheers,
> Daniel B.
>
> On Tue, May 3, 2011 at 10:01 AM, Donald McLean <dm...@stsci.edu> wrote:
>
> > Depends on what you're trying to do. Most programming environments have a
> > way to create a socket on an available port number.
> >
> > For example (from JavaDoc for java.net.ServerSocket):
> >
> > public ServerSocket(int port)
> >             throws IOException
> >
> >    Creates a server socket, bound to the specified port. A port of 0
> > creates a socket on any free port.
> >
> >
> > On 5/2/11 5:30 PM, Daniel Becroft wrote:
> >
> >> Hi,
> >>
> >> Is there a task or script that can be used to generate a (random) port
> >> number that is known to be available? I'm trying to setup some server
> >> instances for testing purposes, and I'd prefer not to hard-code port
> >> numbers.
> >>
> >> I'm running on Windows, so I don't have awk/sed available by default,
> >> either.
> >>
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
> >
>

Re: Finding an available port number

Posted by Daniel Becroft <dj...@gmail.com>.
THanks, I was aware that I could do it through a Java (or similar) command.
What I've got is an Ant task that initializes there server instance with a
specified port number (unfortunately there's no facility to pass in 0 here).
I was after another task so I could so something like:

<target>
  <getavailableport property="port.number" />

  <createserver .... port="${port.number}" />
</target>

Cheers,
Daniel B.

On Tue, May 3, 2011 at 10:01 AM, Donald McLean <dm...@stsci.edu> wrote:

> Depends on what you're trying to do. Most programming environments have a
> way to create a socket on an available port number.
>
> For example (from JavaDoc for java.net.ServerSocket):
>
> public ServerSocket(int port)
>             throws IOException
>
>    Creates a server socket, bound to the specified port. A port of 0
> creates a socket on any free port.
>
>
> On 5/2/11 5:30 PM, Daniel Becroft wrote:
>
>> Hi,
>>
>> Is there a task or script that can be used to generate a (random) port
>> number that is known to be available? I'm trying to setup some server
>> instances for testing purposes, and I'd prefer not to hard-code port
>> numbers.
>>
>> I'm running on Windows, so I don't have awk/sed available by default,
>> either.
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>

Re: Finding an available port number

Posted by Donald McLean <dm...@stsci.edu>.
Depends on what you're trying to do. Most programming environments have 
a way to create a socket on an available port number.

For example (from JavaDoc for java.net.ServerSocket):

public ServerSocket(int port)
              throws IOException

     Creates a server socket, bound to the specified port. A port of 0 
creates a socket on any free port.

On 5/2/11 5:30 PM, Daniel Becroft wrote:
> Hi,
>
> Is there a task or script that can be used to generate a (random) port
> number that is known to be available? I'm trying to setup some server
> instances for testing purposes, and I'd prefer not to hard-code port
> numbers.
>
> I'm running on Windows, so I don't have awk/sed available by default,
> either.