'how to find which app is listening on port Android

I'm trying to write a port scanner, I managed to get the open ports using sockets.
My problem is how to know which apps are listening on open ports.



Solution 1:[1]

Android is based on a Linux kernel, therefore you can do this using the same approach that works under Linux. See https://stackoverflow.com/a/2359643/441899 for a description of how to do that. Additionally you would need to determine from a Linux process what the app running in that process is (see Android - How to get the processName or packageName by using PID? for this). Note that your app would have to be running as root to access the files in /proc that it would need to in order to find this information.

Solution 2:[2]

  1. cat /proc/net/tcp

this will give you a list about the android opening ports.e.g.

  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode                                                     
   0: 0100007F:13AD 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 3336108 1 0000000000000000 100 0 0 10 0                   
   1: 0100007F:1F90 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10252        0 3579923 1 0000000000000000 100 0 0 10 0                   
   2: 6400A8C0:A90E 6800A8C0:1F90 04 00000001:00000000 00:00000000 00000005     0        0 0 1 0000000000000000 326 4 29 1 5                         
   3: 6400A8C0:A91E 6800A8C0:1F90 04 00000001:00000000 00:00000000 00000005     0        0 0 1 0000000000000000 326 4 29 1 5                         
   4: 6400A8C0:84F2 66DFC2DC:01BB 09 00000001:00000001 00:00000000 00000005     0        0 0 1 0000000000000000 665 4 24 1 5                         

so we know uid =10252 is the APP listening the port 1F90( which is 8080)

  1. cat /data/system/packages.list | grep 10252 (the pid you found )
com.target.app 10252 0 /data/user/0/com.target.app default:targetSdkVersion=29 3002,3003 0 1

refer to : https://stackoverflow.com/a/38793457/445908

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Siwei