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.
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.