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.
source share