You can determine the current open ports by reading text / proc pseudo-files, such as
/proc/net/tcp /proc/net/udp
This is basically what the netstat command does (where implemented) - you might want to find and study the source of a simple netstat implementation (maybe it can be ported to java)
However, when running as an unprivileged appid application, you can only close sockets belonging to your own process (or workarounds related to ptrace or process processing, other processes belonging to your user ID). Also note that closing a socket does not necessarily make this port available for immediate reuse.
Chris stratton
source share