0

I was surprised to discover that two servers I'm maintaining are both able to listen on port 3000 in development at the same time.

With the first server running, netstat shows

  ▶ sudo netstat -nap tcp | grep 3000
  tcp6       0      0  ::1.3000               *.*                    LISTEN     
  tcp4       0      0  127.0.0.1.3000         *.*                    LISTEN     

And with both running:

  ▶ sudo netstat -nap tcp | grep 3000
  tcp4       0      0  *.3000                 *.*                    LISTEN     
  tcp6       0      0  ::1.3000               *.*                    LISTEN     
  tcp4       0      0  127.0.0.1.3000         *.*                    LISTEN     

My interpretation of this is that the first server has bound port 3000 only for localhost (127.0.0.1), and the second server has bound port 3000 for 'any' (0.0.0.0) address. Is that right?

The behaviour seems to be that the first server supersedes the other for traffic specifically to http://localhost:3000, which makes sense I suppose. I just wanted to confirm my understanding for this slightly surprising scenario, I would have thought that trying to listen to 'any' address would fail if any address with that port was already bound.

4
  • Do you talk about two daemons, run on one server? Mar 9 at 19:20
  • 1
    Two separate server processes running on the same system, yes. @RomeoNinov
    – Pathogen
    Mar 9 at 19:44
  • Two separate server processes running on the same system, yes.
    – Pathogen
    Mar 9 at 19:45
  • 1
    Normally bind will fail for a conflict like this, but not if the program sets an option, originally SO_REUSEADDR but may depend on OS; see stackoverflow.com/questions/14388706/… . Some programs do this automatically to allow restarting after a crash while 'orphaned' connections are still in TIME_WAIT. Mar 10 at 4:17

1 Answer 1

1

Your assumption is correct for the first service. It listen on localhost and locahost6. About the second it seems it listen on the IPs of the host, different from above. But there is probability second also try to listen on localhost. You can check it by stop both and start only second one.

And if you permit me humble recommendation: separate the ports for service one and service two, make them for example 3001 and 3002

1
  • 1
    Yep, I've already split out the servers to listen on different ports. It was my huge surprise to discover that they weren't already, and I really wanted to understand what the heck was going on. The two server apps could be started in either order.
    – Pathogen
    Mar 9 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.