netstat will show the most useful statistic data of networking.
netstat -ltnp | grep -w ':8080'
The above command will display the information related to the networking port of 8080.
These are the detailed meaning of options:
l – let netstat to only show listening sockets. t – let netstat to display TCP connections. n – show numerical addresses. p – enables showing of the process ID and the process name. grep -w – shows matching of exact string (:80).
Note: On MacOs, we’re stuck with the BSD netstat which will not show you the process ID that is attached to a given port.
lsof will be a choice for Mac.
lsof will display all opened files of a process. We could also use it to find which process used a networking port, since TCP/IP sockets use file descriptors.
lsof -i will show which process communicating over the internet.