I have a basic networking question. Two PCs are communicating on a WLAN. Both the PCs are behind routers or modems.
Both the PCs have the same public IP. That public IP is most of the time is either of the router or of the modem. There can be more then one PC behind those routers and modems. How are the PCs are communicating? I can understand that packets can reach those routers or modems and then the Internet, but what after that? In the packet, the destination IP is set as the public IP. When the packet returns, how does the router or modem decide where to send the packet? Can anybody explain me this please.
Most of the mechanics behind this mystery is NAT-masquerading:
for instance your PC is on a private network, with IP 10.1.2.3.
Your router remembers the PC IP/port, and that it initialized a connection to a remote IP/port (WAN). Thus, when there is a reply on the same channel, the router forwards back the packet to the PC, on its LAN side.
In this case, the ext. device initializes the connection, and thus the router doesn't know to which 10.x.x.x client it is supposed to forward the packet.
This is why the router needs a specific settings that says, for instance if your PC happens to be a Web server (http port)
that says, the packets coming on the HTTP port go to the internal LAN address 10.1.2.3 (your PC) on port 80.
In a basic setup a device always only knows the next hop. This one also just knows the next hop to the desired destination. if the destination is not local to the host/router it sends the packet to the default gateway and so on and so forth.
in more complex situations you can have static routes pointing to an explicit gateway for a certain network destination.
if it gets too complex to manage those routes manually, dynamic route distribution protocols are used like RIP
even more elaborate protocols know more about the global distribution of ip blocks (via AS Numbers) and try to make decisions on where to route packets on cost and distance. examples are BGP and OSPF
I'm a bit puzzled by your "most of the time": I think it's either always or never. If the public IP of a PC is always coinciding with its router/modem, then the local network is using NAT to give access to the PC from outside the local network. => see first answer
If the public IP of a PC doesn't coincide with its router/modem, then the local network is directly accessible from the WAN (if we ignore possible firewalls that can block the way). => see second answer
If you are just starting to understand how routing works, you should probably first concentrate on the latter scenario, and maybe also simplify it as:
(like there would not be anything else around)
Good question. There are actually a few strategies, but essentially the router keeps a table of all NAT'd connections (client port, client IP, destination IP, destination port). When a return packet comes back it matches which client to forward it to based on where the packet has come from, and from which port.
Most NAT implementations are also PAT (port address translation implementations), to prevent two clients picking the same source port.
Some routers, e.g. Cisco devices do sequence number randomization for security reasons - it can help hide how many and what types of clients are behind the NAT. It can also try to protect the clients against types attacks that target the sequence number, or guess the next sequence number for the next connection.