Operating System - HP-UX
1833875 Members
1842 Online
110063 Solutions
New Discussion

Re: Question about Unix Socket !

 
SOLVED
Go to solution
Chris Fung
Frequent Advisor

Question about Unix Socket !

Hi there,

I am just wondering whether a client program can occupy a port no. e.g. 10000 that already defined in the /etc/services file for another services ?

What is the difference for -
1. the defined services is up and running therefore listening to the port 10000

2. the defined services is down / not running.

Thanks for your advice.

Best Regards,

Chris,
9 REPLIES 9
Simon Hargrave
Honored Contributor

Re: Question about Unix Socket !

The services in /etc/services are just a name/service number lookup. Just because a service is listed in /etc/services, does not mean that a daemon is running on that port.

Easiest way to see if anything is running on a TCP port is: -

telnet localhost 10000

If you get

telnet: Unable to connect to remote host: Connection refused

then there is no service running.

The /etc/services file will simply let you achieve the same with: -

telnet localhost console (since in /etc/services, port 10000 is defined as service called console, so it just looks up port 10000).
Bharat Katkar
Honored Contributor

Re: Question about Unix Socket !

Hi Chris,

# netstat -a

This command will show you if any service is listening at any particular port, whether the connection is established and so on.
Just have a look at the output.
See man netstat.

Regards,
You need to know a lot to actually know how little you know
Shaikh Imran
Honored Contributor

Re: Question about Unix Socket !

Hi,
Just to add something :
If the defined service is up and running you will get connection listening at that port.
If the client has established a connection via that port then you will get the status as established at that port.
You can view this by
#netstat -na |grep 10000

Regards,

I'll sleep when i am dead.
Chris Fung
Frequent Advisor

Re: Question about Unix Socket !

Hi there,

Thanks for the input. I would like to know is there a way to restrict the usage of the network port in some way ??

E.g. reserve 10000 to particular program so that other programs cannot acquire this port no matter the particular program is running / stopped.

Or the restriction can be lying on the user level, so that particular user will only be able to user a range of ports.

Any tools (open source / commercial / built-in) available to achieve this task.

Thanks in advance.

Cheers,

Chris,
Shaikh Imran
Honored Contributor
Solution

Re: Question about Unix Socket !

Hi,

There doesn't appear to be an way of reserving network ports, unless you
make your application a 'client' of inetd, like ftpd/telnetd/etc.:

Adding to /etc/services does not reserve the ports, although I would suggest
if you use particular ports consistently add them in there as a 'flag' to
let other users know you are using those ports. Better than nothing!

"The /etc/services file is documentation. It does not reserve a port. Some
programs will refer to the port by service name so if it is not listed, the program
will abort."

The way to go with this issue is to watch what ports you use:

"look at a netstat output to see what ports are being used while the machine
is busy, and pick a set of ports that are far away from the others. Also,
have a look at this:
http://www.iana.org/assignments/port-numbers

This explains the well-known, registered, and dynamic/private ports concept.
If you stay between 49152 and 65535, chances are, you're safe. Most of the
registered ports are not likely to be used on your system, so if you want a
lower number for an internal app, you shouldn't have a problem if you just
avoid the ones on your particular systems."

Regards,









I'll sleep when i am dead.
Bharat Katkar
Honored Contributor

Re: Question about Unix Socket !

Chris,
You can define the service in /etc/service as follows:
10000/tcp
and then define the same in /etc/inetd.conf.

Restart you inetd daemon.
# /usr/sbin/inetd -c

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: Question about Unix Socket !

Hai,

By defaul 1,1023 ports called as privillaged ports. Ports and the service using them are defined in /etc/services files. If we want to use the port < 1023,we have to use bindservport() call.

To check the weather the service is running or not,we can use netstat -na | grep "port-number". Some service will create process ID to indicate that it is running. We can get with ps.

If you want to startup the service by default and down while normal down,we have to use the /etc/inetd.conf with inetd(eamon). All the service starup success and failure are stored in syslog.log file.

If one service is using one port,other service can not use that port. Service up and down can be done on the service which are included in the inetd(eomon). If a Service running means, it is executed as a standalone one on shell and not running means,it is process stopped.

We can give the restriction to the ports for the specific service by adding that in inetd.conf file and /etc/service files. No other service cannot use that port. see the /etc/services file,and add an entry for your new service. Make an entry in inetd.conf and start the inetd with -c option to reconfigure.

Make a script, to check which service is using your needed port and service setup in /etc/inetd.conf file. comment the entries and allocate them for.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Mark Grant
Honored Contributor

Re: Question about Unix Socket !

In reply to your second question, the only way to reserve a port is to use it. Using "inetd", in effect, uses it. This may well be good enough for you. However, the disadvantage here is that your application now has to be run by inetd which is incredibly inefficient if you have multiple and/or many connections because the application gets re-started each time.

The best way is to get your application to sit on the port doing nothing except servicing requests. If it doesn't exit, nothing else can use the port.
Never preceed any demonstration with anything more predictive than "watch this"
Chris Fung
Frequent Advisor

Re: Question about Unix Socket !

Hi All,

Thanks for your inputs and advice. I think the problem now will be on our operation side that they need to stop the service before running a batch and restart it after the batch. The downtime will be more than 10 hrs.

Therefore, within this period, the registered port will be free from using by other process. Problem happened like winning the lottry that we have another process occupied the port and hanged for no reason one day.........and that raised everybody's attention in finding the root cause as well as the permanent solution.

Thanks you very much again.

Cheers,

Chris,