Alex headshot

AlBlue’s Blog

Macs, Modularity and More

Finding open network sockets

Howto 2007

I had a problem recently where I was trying to figure out what file had a specific port open, since there was no obvious indication that anything was using that port. If I was running Linux, I'd have been able to go into the /proc filesystem, do a scan for the port in the /proc/net files, and take it from there.

However, I'm on a Mac, which doesn't have a /proc filesystem, and for some reason the netstat tool wasn't reporting the entry. (I normally do netstat -a -n | grep port to find out what's bound to a port.)

I discovered the erstwhile tool lsof (in /usr/sbin, along with netstat) not only tells you what files are open, but can also do the same for network connections, too. In fact, you can do lsof -i :80 to find any connections to port 80.

Sadly, I discovered that the problem lay with the application trying to register itself twice on the given port (and thus failing immediately on the 2nd attempt), so the reason why netstat wasn't giving me anything in the first place was because at that time, there wasn't anything using that address. Let that be a lesson to check the logs more carefully next time ...