How to Identify which process keeps port busy on android / Linux ?

Lets say you are trying to do something with UPNP which reserves port 1900 for all of its applications.. Now if you have some other application / process running already which is using this port and when you developed / installed your application.. there are higher chances that your application will not work as expected and will either return with some random errors or complain about being the respective port as busy.

Now, in this scenario the first thing we have to identify is which is the another application / process is using the same port which we intend use.

For the same, since we intend to identify an application using port, we will use a Linux/Android tool “lsof” along with -i option which enables us to filter the results related to internet/ports.

$ lsof -i
COMMAND    PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java      6104 devlab   14u  IPv6 487938      0t0  UDP *:1900 

As we can see above, the result of “lsof -i” informed us that there is a command “java” whose process id (pid) is 6104 is using UDP port 1900.

Now, lets get some more information about this process to identify which is the exact command or application is using this port. For the same, we will use standard process identification command “ps -ax” for ubuntu / Linux.

$ ps -ax | grep 6104
 6104 pts/8    Sl+    0:01 java DatagramReceiverServer

On ubuntu, you can also use “netstat” command as, below

$ sudo netstat -nap | grep 1900
udp6       0      0 :::1900                 :::*                              6104/java

NOTE: for our demonstration, we had modified DatagramReceiverServer.java from “JAVA DatagramSocket Client and Server Example” to run the server which can listen on port 1900.

For Android, use below command for process identification

$ ps -A | grep 6104

As another example, lets listen on same port 1900 as a TCP socket using netcat “nc” command as,

$ nc -l 1900

Now, if we check which are the apps using 1900 port using lsof, it returns as below,

$ lsof -i | grep 1900
java      6104 devlab   14u  IPv6 487938      0t0  UDP *:1900 
nc        6794 devlab    3u  IPv4 688692      0t0  TCP *:1900 (LISTEN)
$ ps -ax | grep 6794
 6794 pts/11   S+     0:00 nc -l 1900

here, as we can see, using process id, 6794 we could identify exact command / application details.

1 thought on “How to Identify which process keeps port busy on android / Linux ?”

Leave a Comment