java - Check port usage at runtime? -


in application, during runtime, able identify if particular port being used (pretty easy this)

public static boolean portavailable(int port) {     try (socket ignored = new socket("localhost", port)) {         return false;     } catch (ioexception ignored) {         return true;     } } 

but information possible using port. stuff process id, name etc. how go doing this?

java not provide information looking for. have use os-specific apis instead.

i don't know *nix systems, on windows can owning process id tcp socket using gettcptable2()/gettcp6table2() or getextendedtcptable() enumerate tcp sockets until find ip/port interested in. udp, there getextendedudptable() function (and there getudptable() , getudp6table() functions, not provide owner process ids).

once have process id, can filename either:

  1. using createtoolhelp32snapshot()/process32first()/process32next() enumerate running processes until find matching process id.

  2. using openprocess() directly open process of given process id, , query process's filename using getmodulefilenameex(), getprocessimagefilename(), or queryfullprocessimagename().


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -