Stop listening to a port
I have a application that accepts SSL connections on a particular port. When I stop the application from the Websphere Administrative Console and then start it again, I get an exception that complains that the port is still in use. I need to shutdown and restart Websphere entirely to get the appli开发者_Python百科cation working again.
What is the correct way to stop listening to a port in Java?
Create a context listener and register it in web.xml. Then you can close all your open socket 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
I am not sure as to how it is done in Java, but closing the socket (.close() on the ServerSocket?) will close the port. Still, many operating systems will not let you reuse the same port for about 30 seconds after you have closed it to prevent old messages being read by the new connection.
Just close the socket handle, something like yourSocket.close()
. This example might help.
ServerSocket ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(port);
精彩评论