How can i close open ports.

 
SOLVED
Go to solution
shashi kiran_3
Occasional Advisor

How can i close open ports.

Hi All,

Our application will open some ports and start some communication. My problem is the application is crashing frequently. If I start the application again.It is giving Following
The ports are already being used.
Choose other free ports.
How can I release the ports before starting the application.?
The application will start if I start after timeout period of the port.


Thanks & Regards
Shashi
5 REPLIES 5
Sridhar Bhaskarla
Honored Contributor
Solution

Re: How can i close open ports.

Hi Shashi,

I would check if the processes that are using those ports are actually dying. Use 'lsof' to find that information. For ex.,

lsof -i tcp:2500 will show all the processes that are using the tcp port 2500. Killing those processes as your application is already down should release the ports.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Dave Olker
HPE Pro

Re: How can i close open ports.

Hi Shashi,

If the ports in question are TCP ports, and if the lsof output shows that no running process is using the port but the port remains used, you can use ndd to forcibly shut down the connections holding the ports open.

If you search the ITRC forums for tcp_discon or tcp_discon_by_addr you should find examples of how to use ndd to forcibly disconnect TCP connections.

This thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=647225

contains details on how to do this, and it contains the warning text of why it is so important to be careful when using ndd to do this operation.

Regards,

Dave
I work for HPE
rick jones
Honored Contributor

Re: How can i close open ports.

I am afraid that in addition to whatever bugs your application has that leads to its crashing, it is also missing some code - a call to setsockop() to set SO_REUSEADDR before trying to bind() to its well-known port number(s).

If you do that, I suspect you will be able to restart your application without difficulty.
there is no rest for the wicked yet the virtuous have no pillows
Muthukumar_5
Honored Contributor

Re: How can i close open ports.

If you want to make port selection is based on system related ( free ports will be allocated for operation when port to the socket is set to 0 )

IF you want to make the ports to be reused / and address then try to use setsockopt() with SO_REUSEADDR (rick suggestion) and SO_REUSEPORT too before bind operation there.

These options make application to reuse the allocated port with address there.

And you can idenity port's usage of application with lsof tool there.
Easy to suggest when don't know about the problem!
rick jones
Honored Contributor

Re: How can i close open ports.

NEVER EVER USE TCP_DISCON! Unless and until you have demonstrated _absolutely_ no other recourse... ALWAYS insist on proper application behaviour first.
there is no rest for the wicked yet the virtuous have no pillows