Search code examples
mongodbserveripportforwardinglan

MongoDB connection refused if my public IP matches the one of the server


I created a public MongoDB server which is hosted on one of my computers. To test it I connected to another network on my laptop and connected to the server using my public IP and the port that I forwarded for MongoDB.

This worked fine.

However, if I connect my laptop to my LAN, the same one in which the server is connected to, I get a error:

[Errno 111] Connection Refused

Is it possible for me to connect to my own public server if my public IP matches the public IP of the server? It seems like some infinite feedback loop is happening where my public network is trying to connect to my public network.

I could always test if the clients public IP matches the server IP, and if it does to connect directly using the host computers IP instead, but first I want to see if there's a better way to handle this.


Solution

  • You cannot have two machines on the same network segment with the same address. This will cause issues with all kinds of applications because both machines will try to respond to traffic directed to either one.