how to get port number - TCP-IP

This is a discussion on how to get port number - TCP-IP ; Hi there, my problem is the following: I'd like to get a free port number from the operating system (Linux or other Unix). Of course I could use bind till I get a valid one, but that seems not to ...

+ Reply to Thread
Results 1 to 12 of 12

Thread: how to get port number

  1. how to get port number

    Hi there,

    my problem is the following: I'd like to get a free port number from
    the operating system (Linux or other Unix). Of course I could use bind
    till I get a valid one, but that seems not to be elegant. Anything
    better?

    - Gergely


  2. Re: how to get port number

    gbuday@gmail.com wrote:
    > Hi there,
    >
    > my problem is the following: I'd like to get a free port number from
    > the operating system (Linux or other Unix). Of course I could use bind
    > till I get a valid one, but that seems not to be elegant. Anything
    > better?
    >
    > - Gergely
    >

    A list of opened ports and connected sockets can be displayed using the
    netstat -a command

    Greetz Geert

  3. Re: how to get port number

    gbuday@gmail.com wrote:
    > Hi there,
    >
    > my problem is the following: I'd like to get a free port number from
    > the operating system (Linux or other Unix). Of course I could use bind
    > till I get a valid one, but that seems not to be elegant. Anything
    > better?
    >
    > - Gergely


    For most implementations of the TCP/IP stack you can try to use port 0
    (that's port ZERO). The TCP/IP stack or OS will automagically assign
    the next available port number. This is universally true on almost all
    Unix-like platforms and is certainly true for Linux. This technique is
    sometimes called port zero networking.

    This also works on my WinXP machine but I've heard people say that
    Windows don't universally support this feature.

    http://wiki.tcl.tk/2230


  4. Re: how to get port number

    slebetman@yahoo.com wrote:
    > This also works on my WinXP machine but I've heard people say that
    > Windows don't universally support this feature.
    >
    > http://wiki.tcl.tk/2230


    I've never seen a version of Windows where port number of zero didn't
    work, and I've used them all.

    The TCL reference you've given is about something else entirely.

  5. Re: how to get port number

    EJP wrote:
    > slebetman@yahoo.com wrote:
    > > This also works on my WinXP machine but I've heard people say that
    > > Windows don't universally support this feature.
    > >
    > > http://wiki.tcl.tk/2230

    >
    > I've never seen a version of Windows where port number of zero didn't
    > work, and I've used them all.


    That's what I thought. I even wrote a small test program on my XP to
    test it out and it worked. Last night I compiled it on my home Win2k
    machine and it worked. But googling around I found this message:

    http://www.derkeiler.com/Mailing-Lis...5-07/0021.html

    over and over again on various mailing lists saying that the port zero
    trick doesn't work the same way on Windows. Maybe an issue with older
    versions of winsock perhaps? I don't have a Win95/98 or Win3.1 machine
    to test it out on so I wasn't sure. Plus, my windows boxen has lots of
    third party libraries and dlls laying around and wasn't sure if it was
    them that caused my test program to work.

    > The TCL reference you've given is about something else entirely.


    It IS about port zero networking (albeit from a Tcl perspective). I
    couldn't find any other good reference on the net about port zero
    networking.


  6. Re: how to get port number

    In article <1154665178.172931.291000@m73g2000cwd.googlegroups. com>,
    "slebetman@yahoo.com" wrote:

    > EJP wrote:
    > > slebetman@yahoo.com wrote:
    > > > This also works on my WinXP machine but I've heard people say that
    > > > Windows don't universally support this feature.
    > > >
    > > > http://wiki.tcl.tk/2230

    > >
    > > I've never seen a version of Windows where port number of zero didn't
    > > work, and I've used them all.

    >
    > That's what I thought. I even wrote a small test program on my XP to
    > test it out and it worked. Last night I compiled it on my home Win2k
    > machine and it worked. But googling around I found this message:
    >
    > http://www.derkeiler.com/Mailing-Lis...005-07/0021.ht
    > ml
    >
    > over and over again on various mailing lists saying that the port zero
    > trick doesn't work the same way on Windows.


    The problem with the Internet is that you can't tell whether they've all
    run into the problem, or all these web sites are just repeating what
    they read on each other's web sites. So one guy runs into a problem,
    which may or may not have been real (perhaps it was actually a bug in
    his code), he posts about it, and it gets accepted as fact and published
    all over the place. Then you come along as say "See, all these web
    sites say it doesn't work."

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  7. Re: how to get port number

    slebetman@yahoo.com wrote:

    > EJP wrote:
    >>
    >>I've never seen a version of Windows where port number of zero didn't
    >>work, and I've used them all.

    >
    > That's what I thought. I even wrote a small test program on my XP to
    > test it out and it worked. Last night I compiled it on my home Win2k
    > machine and it worked. But googling around I found this message:
    >
    > http://www.derkeiler.com/Mailing-Lis...5-07/0021.html


    This message just relates to someone being baffled by a netstat output
    showing no foreign IP address/port, which is the normal output for a
    listening socket. (In theory in RFC 793 when listening it should also be
    possible to specify a foreign IP/port, being the only one you will
    accept() from, but I've never seen an implementation. The zero here
    means accept from any foreign port.)

    > over and over again on various mailing lists saying that the port zero
    > trick doesn't work the same way on Windows. Maybe an issue with older
    > versions of winsock perhaps? I don't have a Win95/98 or Win3.1 machine


    I've done sockets programming on every version of Windows since 3.0, and
    several vendor's implementations of WINSOCK, and I've never seen it not
    work as expected.

    >>The TCL reference you've given is about something else entirely.

    >
    > It IS about port zero networking (albeit from a Tcl perspective).


    Nope. It says 'The problem has to do with the way some winsock
    implementations do name resolution, which fconfigure -sockname triggers,
    and not the creation of the socket itself.' It seems to be about some
    kind of optimization to prevent modem dialups happening untowardly.

    > I couldn't find any other good reference on the net about port zero
    > networking.


    I've been doing sockets since about 1987 and I don't think I've never
    heard the term before.

  8. Re: how to get port number

    Barry Margolin wrote:
    > In article <1154665178.172931.291000@m73g2000cwd.googlegroups. com>,
    > "slebetman@yahoo.com" wrote:
    >
    > > EJP wrote:
    > > > slebetman@yahoo.com wrote:
    > > > > This also works on my WinXP machine but I've heard people say that
    > > > > Windows don't universally support this feature.
    > > > >
    > > > > http://wiki.tcl.tk/2230
    > > >
    > > > I've never seen a version of Windows where port number of zero didn't
    > > > work, and I've used them all.

    > >
    > > That's what I thought. I even wrote a small test program on my XP to
    > > test it out and it worked. Last night I compiled it on my home Win2k
    > > machine and it worked. But googling around I found this message:
    > >
    > > http://www.derkeiler.com/Mailing-Lis...005-07/0021.ht
    > > ml
    > >
    > > over and over again on various mailing lists saying that the port zero
    > > trick doesn't work the same way on Windows.

    >
    >
    > his code), he posts about it, and it gets accepted as fact and published
    > all over the place. Then you come along as say "See, all these web
    > sites say it doesn't work."
    >


    That's not what I said. I indeed said "it works on Windows" XP when I
    tried it. I even later said it works on Win2k when I tried it. But I
    don't have evidence that it works on all versions of Windows so I was
    merely excercising caution by pointing out that "other people" say that
    it doesn't always work. This is in case my claim that it works on
    Windows accidentaly "gets accepted as fact" without me actually knowing
    it for a fact.

    Note that with EJP's testimony of testing it on "multiple
    implementations of winsock on every version of Windows from 3.0" we can
    indeed accept it for a fact that the port zero trick works on all
    version of windows. But I couldn't make such claim because unlike EJP,
    I have not programmed all versions of Windows. Also note, that despite
    the fact that I did not claim that it does not work on Windows all this
    argument of weather it works on Windows is moot since the OP was asking
    for a mechanism that works on Unix/Linux.


  9. Re: how to get port number

    > > over and over again on various mailing lists saying that the port zero
    > > trick doesn't work the same way on Windows. Maybe an issue with older
    > > versions of winsock perhaps? I don't have a Win95/98 or Win3.1 machine

    >
    > I've done sockets programming on every version of Windows since 3.0, and
    > several vendor's implementations of WINSOCK, and I've never seen it not
    > work as expected.


    Excellent. So now we know it works on Windows as expected.

    > >>The TCL reference you've given is about something else entirely.

    > >
    > > It IS about port zero networking (albeit from a Tcl perspective).

    >
    > Nope. It says 'The problem has to do with the way some winsock
    > implementations do name resolution, which fconfigure -sockname triggers,
    > and not the creation of the socket itself.' It seems to be about some
    > kind of optimization to prevent modem dialups happening untowardly.


    "fconfigure -sockname" is a thin wrapper around getsockname() which you
    need to call to find out what port you've recieved from the TCP/IP
    stack when you passed port 0 to bind. So it looks like on some
    implementations of Winsock windows will trigger dial-up when you call
    getsockname() which may not be what you expect when merely starting up
    a server process. Though like the poster noted on that page, I haven't
    seen this behavior in my lifetime and can't simulate it. Note that
    getsockname() is normally not necessary in normal socket programming
    but is necessary when you use port 0 to find out what port number have
    actually been assigned. So, that statement IS about port 0 networking.
    And the page I referenced is the only place on the net so far defines
    what the port 0 trick does.


  10. Re: how to get port number

    Then, I am trying to get n continual unused ports (i.e.
    2001,2002...200n) on an embedded system (no netstat). So I can only use
    bind to check usability of a port number, is that right?

    Charlie Zhu

    slebetman@yahoo.com wrote:
    > gbuday@gmail.com wrote:
    > > Hi there,
    > >
    > > my problem is the following: I'd like to get a free port number from
    > > the operating system (Linux or other Unix). Of course I could use bind
    > > till I get a valid one, but that seems not to be elegant. Anything
    > > better?
    > >
    > > - Gergely

    >
    > For most implementations of the TCP/IP stack you can try to use port 0
    > (that's port ZERO). The TCP/IP stack or OS will automagically assign
    > the next available port number. This is universally true on almost all
    > Unix-like platforms and is certainly true for Linux. This technique is
    > sometimes called port zero networking.
    >
    > This also works on my WinXP machine but I've heard people say that
    > Windows don't universally support this feature.
    >
    > http://wiki.tcl.tk/2230



  11. Re: how to get port number

    In article <1154744164.175089.294630@i3g2000cwc.googlegroups.c om>,
    zh.charlie@gmail.com wrote:
    >Then, I am trying to get n continual unused ports (i.e.
    >2001,2002...200n) on an embedded system (no netstat). So I can only use
    >bind to check usability of a port number, is that right?


    embedded systems often have system-specific extensions,
    or permit monkeying around with operating system internals
    (instead of protecting them from you.)

  12. Re: how to get port number

    slebetman@yahoo.com wrote:
    >
    > Excellent. So now we know it works on Windows as expected.


    It *has to* work on Windows (well, WINSOCK) as expected, because that's
    the specified behaviour of bind().

    As slebetman confirms, the urban myth being bandied around concerns an
    unwanted side-effect of *finding out* what bind() did, not the behaviour
    of bind() itself.

+ Reply to Thread