Stop listening to a port

I have an application that accepts SSL connections on a specific port. When I stop the application from the Websphere Administration Console and then start it again, I get an exception that complains that the port is still in use. I need to completely disable and restart Websphere in order to start the application again.

What is the correct way to stop listening on a port in Java?

+4
source share
4 answers

Create a context listener and register it in web.xml. You can then close all of your open sockets in the contextDestroyed() call.

See this for details.

http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/cweb_sctxl.html

+2
source

I'm not sure how this is done in Java, but closing the socket (.close () on ServerSocket?) Will close the port. However, many operating systems will not allow you to reuse the same port for about 30 seconds after you close it, to prevent old messages from being read by a new connection.

+2
source

Just close the socket descriptor, something like yourSocket.close() . This example may help.

+1
source
 ServerSocket ss = new ServerSocket(); ss.setReuseAddress(true); ss.bind(port); 
0
source

Source: https://habr.com/ru/post/1314044/


All Articles