I have already looked at some of the answers on SO but to no avail. How can I check if a port is open already, close it if open already and then open a socket on that port from Java code?
Details: I want to open port number 5000 and bind it to a udp socket. However, every time I open the socket on that port, I get an exception "Address already in use". How can I make sure the port is available first? Overridind onDestroy() to close the socket when terminating the application does not seem to do it.
I have already looked at some of the answers on SO but to no avail.
Hard to believe.
How can I check if a port is open already
Try to bind to it as below. If you get a BindException, it's in use.
close it if open already
You can't.
and then open a socket on that port from Java code?
DatagramSocket ds = new DatagramSocket(port);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.