java - Check which process is using a given port -
I'm creating a simple client-server application in Java, this way I'm starting to hear the socket:
Private Zero initSock () {try {sock = new ServerSocket (port); } Hold (IOException e) {System.err.println ("Could not listen to the port:" + port + "what port is not already in use?"); System.exit (1); }} This approach gives a potentially useful error message when the port is used - however: it is possible to expand the information such as:
Port xx could not be heard, it is being used by process blahblah by pid xxx . What Java (and Windows, and finally other systems) will allow me to get that information?
I can find this information using the command line, but in reality it is not what I want at this time.
I can not fulfill your requirement, but the code below can give you some more information. The public square netstat throws IOException {the public static zero major (string [] args) {buffrebrader br = new buffed reader (new input striper reader (system.)); System.out.println ("Enter port number:"); String port = br.readLine (); Stringbuffer output = new stringbuffer (); Process p = runtime.gettime (). Exec ("netstat -an"); BufferedReader Reader = New BufferedReader (New InputStreamReader (p.getInputStream ())); String line = ""; While ((line = reader.readline ())! = Null () (if (line canteen (port)) {output.append (line + "\ n");}} System.out.println (output.toString ( ));}} Input: 50773 Output: TCP 1xx.xxx.xxx.xxx:50773 xxx.xxx.xxx.xxx:8080Established
Comments
Post a Comment