Help!

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


Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> App Development RSS
Next:  How can I wait on a server socket for 10 seconds?  
Author Message
silverburgh.meryl
External


Since: Jul 05, 2007
Posts: 1



PostPosted: Thu Jul 05, 2007 7:42 pm    Post subject: How can I find out if a port is available on linux
Archived from groups: comp>os>linux>development>apps (more info?)

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.
Back to top
mike
External


Since: Jul 06, 2007
Posts: 5



PostPosted: Fri Jul 06, 2007 10:47 pm    Post subject: Re: How can I find out if a port is available on linux [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jul 5, 12:42 pm, "silverburgh.me...@gmail.com"
<silverburgh.me....RemoveThis@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.
Back to top
Josef Moellers
External


Since: Jul 07, 2005
Posts: 389



PostPosted: Mon Jul 09, 2007 10:32 am    Post subject: Re: How can I find out if a port is available on linux [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

silverburgh.meryl DeleteThis @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 Wink 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
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> App Development All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum