Search code examples
macosspring-bootsocketstcp

Mac OS: socket bind failed while port is not occupied


The background is: I have a SpringBoot project in development, which listens to port 55555. The project worked correctly on my Mac OS before. But recently it cannot start because it cannot bind to the given address. While another port like 55556 will be fine.

I tried to bind to it with a simple python script, but it also doesn't works:

>>> import socket
>>> s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # this won't help
>>> s.bind(('0.0.0.0',55555))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 48] Address already in use
>>>

As we know, the well-known way to solve this is to find out which process is occupying the port and kill it. However, I cannot find any process that occupies this port. I used lsof and netstat. Here is what I entered and what it responses (nothing, exactly):

cosmo:~$ sudo netstat -an -p tcp | grep 55555
cosmo:~$ sudo lsof -i:55555
cosmo:~$ 

Also, trying to connect to that port fails:

cosmo:~$ telnet localhost 55555
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
cosmo:~$

Lastly, I restarted my computer, but it was no use, too.

Any ideas how this problem happens and how to handle with it? Thanks so much!


Solution

  • Apparently, you cannot use port 55555 on Macs. It's not clear whether this is a bug or intentional behavior, but it is widely reported with various explanations, none official. The solution is to use some other port.

    The closest to an official statement I can find is from someone at Apple's support saying that the port is "used by the system for TCP" and that "... fixing that would be more challenging".