Operating System - HP-UX
1820098 Members
3671 Online
109608 Solutions
New Discussion юеВ

How to reserve a TCP port

 
SOLVED
Go to solution
Tom Fellowes
Advisor

How to reserve a TCP port

We have a custom client-server application running on our b2600 workstation under HPUX 11.11. For this application, our workstation acts as the server and the server side always needs to listen on tcp port 55651. The client is at a remote location and they connect to port 55651.

Recently, we ran into a problem where our workstation was already connected to an Xterminal on port 55651. As you may already know, each display (in this case the Xterminal) runs an Xserver (on tcp port 6000). The system to which the Xterminal is connected connects to the Xterminal's port 6000 on a random port >1024 (as expected with client-server). Our workstation had connected to the Xterminal using port 55651. Our custom software errored out because it couldn't open port 55651 for listening.

Is there any way to reserve a port > 1024 so that other applications (in this case the random port selection of X connecting to the Xserver on the Xterminal) cannot use the port? To be clear, I want the workstation to NOT use a certain port when selecting a random port number > 1024 for a client application.
9 REPLIES 9
David Child_1
Honored Contributor

Re: How to reserve a TCP port

I'm not sure on this, but perhaps adding an entry to /etc/services might do the trick. I would think this file would be checked before opening a random port.

David
RAC_1
Honored Contributor

Re: How to reserve a TCP port

Start your application before X terminal damon. i.e in run level 3 (assuming that your application will be started at run level three, at boot time), start your application before X damon starts. i.e you will have to give lower start number than x daemon.

That is if X damon startup script is S230xxx, your startup script should be S200yyy

Anil
There is no substitute to HARDWORK
Biswajit Tripathy
Honored Contributor

Re: How to reserve a TCP port

Adding an entry to /etc/services will NOT
help.

If you take a look at your /etc/services
file, it's clearly documented that X uses
many of the ports you are using.

Take a look at your /etc/services file and
pick a port >1024 AND not used by another
service. Don't use ports with higher values
as they are dynamically allocated by the
system.

As long as the server binds to your newly
choosen port and client connects to that,
it does not matter if you have an
/etc/services entry.

- Biswajit
:-)
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to reserve a TCP port

Your developers didn't quite play by the rules. They should have chosen a port in the range 1024-49150 that is not already registered by another vendor at www.iana.org.
-- and then registered this port.

The port you are using is in the dynamically assigned range and thus is a free-for-all. The first server to attach wins.

The best approach is to have your developers allow a command-line port assignment or bind to an entry in /etc/services like "superduper" that you can set.
If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: How to reserve a TCP port

As Clay suggested, for complete list of assigned ports,
go to www.iana.org. Here is a HP released doc
containing the port numbers of HP-UX services and
applications:

http://docs.hp.com/en/5990-7252/ch02s01.html

- Biswajit
:-)
Tom Fellowes
Advisor

Re: How to reserve a TCP port

Thanks for all the help. Sorry to be stupid but I have a couple more questions.

I see from the HP document and the IANA port listing that the ports 49152-65535 are listed as dynamic ports. Does this mean that they are the only ports that will be used as dynamic ports by HP-UX? I might sound stupid, but if that's true, I didn't know it.

It sounds like if I was able to change the listening port to something less than 49152, that was registered to a service I'm sure we don't use (like one of the Nimbus ones at the end of the IANA listing), I'd probably be OK and HPUX wouldn't dynamically select that port when connecting to a remote server. Does that sound right?

Thanks again and I'll be sure to give points.
A. Clay Stephenson
Acclaimed Contributor

Re: How to reserve a TCP port

Yes, although if you don't register this port with the IANA then other vendors are also free to use this port and conflicts can still occur. Even with a registered port, you really should map to a service name rather than directly to a port number so that it much more easily user configurable.
If it ain't broke, I can fix that.
rick jones
Honored Contributor

Re: How to reserve a TCP port

The _default_ dynamic (aka anonymous, aka ephemeral) port range for _HP-UX_ 11 and later is 49152 to 65535. However, there are ndd parameters that could change either the lower or upper bound of that range. (tcp_smallest_anon_port and tcp_largest_annon_port IIRC)

While other OSes may have the same defaults, you cannot *really* count on that. And indeed, applications could try to bind to specific port numbers outside of that range.

As already stated, the only way to make sure that you get any *specific* port number is to be the first to request it.

Another option would be to use something like portmapper. It is an additional level of indirection and complexity, but it would allow you to have your server listen at any old port number and have clients find that portnumber.


BTW, make sure your server sets SO_REUSEADDR before trying to bind() to its port. That will preclude what may be the next step in the evolution of your server - trying to restart while there are still TCP endpoints with that port number. (Say in TIME_WAIT, or any state other than LISTEN)

PPS - don't select 12865 - that one is in use by "my" server - netserver of the netperf benchmark :) :) :)
there is no rest for the wicked yet the virtuous have no pillows
Tom Fellowes
Advisor

Re: How to reserve a TCP port

Info from others was helpful