How can I find out if a port is available on linux - Linux

This is a discussion on How can I find out if a port is available on linux - Linux ; Hi, How can I find out if a port is available on linux? One thing i can do is to open() and then close() a port number. I wonder if there is a more efficient way? Thank you....

+ Reply to Thread
Results 1 to 3 of 3

Thread: How can I find out if a port is available on linux

  1. How can I find out if a port is available on linux

    Hi,

    How can I find out if a port is available on linux?
    One thing i can do is to open() and then close() a port number.
    I wonder if there is a more efficient way?

    Thank you.


  2. Re: How can I find out if a port is available on linux

    On Jul 5, 12:42 pm, "silverburgh.me...@gmail.com"
    wrote:
    > Hi,
    >
    > How can I find out if a port is available on linux?
    > One thing i can do is to open() and then close() a port number.
    > I wonder if there is a more efficient way?
    >
    > Thank you.


    Run: netstat -atnp|grep LISTEN

    It will show you the ports already in use. A quick way to tell if a
    tcp port is free is to just 'telnet localhost XXXX' where XXXX is the
    port. Connection refused means (usually) nothing is listening there.


  3. Re: How can I find out if a port is available on linux

    silverburgh.meryl@gmail.com wrote:
    > Hi,
    >
    > How can I find out if a port is available on linux?
    > One thing i can do is to open() and then close() a port number.
    > I wonder if there is a more efficient way?


    What "port"? Network port? You need to (try to) bind() to it, if that
    fails, the port is in use. There is no use checking netstat because you
    then have a classical race condition (netstat says: port is not in use,
    someone else grabs that port, your bind() attempt fails).
    Or do you mean serial port? You could look into /sys/class/tty for
    entries "ttyS[0-9]", but on my notebook, I have ttyS0 through ttyS3, yet
    no serial port is available on the outside ;-) Another way would be to
    check dmesg output for lines like these:
    ttyS00 at 0x03f8 (irq = 4) is a 16550A
    ttyS01 at 0x02f8 (irq = 3) is a 16550A
    (that's from my desktop which really has two serial ports).

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html


+ Reply to Thread