How does the router know which device to send packets to? [duplicate]
my question does not concern how it moves through the internet, but how it moves through the router to a certain device. All devices connected to a router in a home network have the same external IP. Say device A is loading a page and packets are sent from an external source to the router because the packets know the external IP of device A and they are able to get to the router. But now, how does it get to device A? How does the router know to send it to device A instead of device B?
I think this involves the NAT, but i'm just looking for a logical explanation of what NAT does to accomplish this.
When you initiate a connection out to an external site, the router is using a different source port number each time... it then uses that source port number (which is where the data returns to) as the key to look up which machine on your internal network the request came from. This is called PORT Address Translation (as opposed to Network Address Translation, which is when you have multiple network addresses available to your router).
Normally, All device from Home network have different internal address (private address, always 192.168.0.1~192.168.0.255).
When a packet is send from Home network (let's say Device A: 192.168.0.123), when it pass through router, the router will translate the private address to router's external address (assume is 140.191.2.5), and the router also record this mapping in it's NAT.
When the response is back, the router mapping the NAT, and find the original source is Device A.
so the device A eventually receive the packet.
This is just a concept, for more detail, please refer wiki: NAT.