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

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.

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.

Related

How to set up a client/server connection using port forwarding

I created a multi-threaded client/server application that can send messages to each other at real time. Everything works perfectly, but I want to be able to send messages over the Internet. From what I understand, I need to do port forwarding to be able to make my server reachable for the clients. I then set up my port forwarding options by providing a port (9991) and then my Macbook Air's IP Address (192.168.0.1).
I then tried to connect to my server using my public server IP (let's say 197.132.20.222) and it didn't work. I then tried to see if the port forwarding worked by using this website: https://www.yougetsignal.com/tools/open-ports/ and I realized that the connection was closed. I also tried the command nc -vz 197.132.20.222 9991 while running my application and the connection is refused.
I'm using a JavaFX application, and for my server side I use a ServerSocket with port 9991. For the client side, I use a Socket and set the IP Address to my public router IP Address, and I tried to connect with another PC using mobile data to use a different network.
My firewall settings are turn off, so I really don't know what is blocking my application to connect to that port. Could it be my ISP is blocking connections? I just don't understand why my ports are blocked even with no firewalls enabled.

PostgreSQL connection refused. Client presents with public IP address

I have .NET Core application which uses PostgreSQL. When I installed application on a machine which is presented to internet via IP address (Mikrotik via dst-nat), PostgreSQL server refuses connection with information that public IP address can't connect to PostgreSQL server.
But on Mikrotik router, I see that client tries to connect via internal IP address on the server port 5432, but (somehow) presents via public address. PostgreSQL server and client are in different subnets.
Thanks.

How to connect a small web server program to a web browser which is running in another device in the internet?

I had made a small web server program that can handle HTTP requests, it worked fine on loopback ip address. I Had connected the host computer to a wifi using a router, i can access that program using another device connected to the same router using a web browser. but the problem is when i tried to open the open the command from the device connected to another router/internet, it didnt connect.
I had used the address like "10.0.0.4:8080" to connect with the device on the same router. later i tried with the public IP address to connect to my web server program but it didnt work!
what am i missing in giving the correct ip address and port number such that it could be connected with the devices that arent connected with my router.
You have to add port forwarding on the router from outside to the specific IP of the device. Then use public IP from outside and public or private from LAN. Probably also set it to static internal IP from device or router.
DMZ to the device is also an option but is overkill.
That way lot of bots will scan your server so carefull with the security and what you expose.

Access server from anywhere without public IP and/or Port forwarding

Currently we have a home Internet connection with a public static IP, 100up10down for 60$/m, since we got a really good offer 500down100up for 12$/m we are going to switch internet provider. The catch, the new provider does not offer public IP and/or Port forwarding.
I have one home server and one server in a Datacenter.
Is there a solution for me to route traffic through my "public" server to my home server?
I have root access (obviously) to both server, is there something that I could install on my datacenter server that routes traffic to my private server?
I thought maybe there is something (software) where my private server connects to port 1234 on my datacenter server and if the datacenter server receives data on port 1234 it forwards it to my private server.
Google for port forwarding services.
https://portmap.io/
There are other ones that I can’t find right now.

How Can I ping or create a scoket connection with my friend?

When we ping to any Public IP of a router it give a reply,
we dont get reply, if we ping a local computer with its private IP of a particular network,
So is there a way so I can make direct connection or socket with a particular PC in a network.
I have heard that, I have to make connection with public IP of a particular network and the port number will decide, on which PC in that network , my request will go.
But this method is not working..
If the computer is behind a router that uses Network Address Translation (NAT) - that is, the computer has a private IP that is not visible to the internet - then you cannot ping or establish a connection to the computer from the outside. This is because the router does not know which computer you want since all the computers behind the router share the same public IP address.
The way to establish a connection in this case is to have the computer which is behind the router establish the connection to the outside computer. For example, when you go to a web site, you can do it from a computer behind a router since your computer is initiating the connection. However, the web server you are going to must have a public IP.
When a computer behind the router initiates a connection, the router does select a port number to associate with that connection so packets from the outside for that connection will go to the right computer. However, this only works for traffic on that particular connection; the port number is not a general purpose mapping to that computer.
Two computers behind the same router can establish connections with each other using the private IPs as they don't have to go through the router to do it.
You can decide with your friend who is going to be the client (the one initiation the TCP connection) and who is the server (the one receiving the TCP connection) and which port should be used (let's say X). Let's say that your friend is the server. Then he/she has to configure his/her router to redirect all the traffic to the router to port X to his/her private IP to port X (this is what Warren mentioned as NAT).
If you use a port above 1024, the server (running on your friend's PC) doesn't need root/administrator privileges.
Your program (the client) would then connect to the public IP address of your friend. He/she can check his/her public IP with: What is my IP