Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Dec 26 '12 at 4:45

This question came from our site for professional and enthusiast programmers.

    
I can't give you a very specific answer, but the router keeps track of where a request is going and who sent it. When the external source responds, the router simply checks to see which device attempted to contact the external source initially, and forwards the response to that device. If you need more information, look into NAT. –  Jack Humphries Dec 26 '12 at 3:17
    
This question belongs to Superuser.com –  TimothyP Dec 26 '12 at 3:18

3 Answers 3

up vote 7 down vote accepted

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).

share|improve this answer
    
this would explain things like TCP connections, but some applications are connectionless. What about UDP packets? What if you're hosting a server on 1 device, even if it uses TCP for the connection, it wont be able to initialize a connection without the router knowing which device the server is on. –  Dan Webster Dec 26 '12 at 3:23
    
For UDP connections you usually have to pre-define a port on your router that can be used to route to a specific device. This is the "port forwarding" feature of routers, and can be used either for incoming UDP packets (either in response to an outgoing message or a fresh incoming packet) as well as for incoming TCP packets (eg those initiated by a remote client instead of your own PC.) –  Michael Bray Dec 26 '12 at 3:26

NAT or Network address translation is what does this for home networks http://en.wikipedia.org/wiki/Network_address_translation

share|improve this answer
    
No - Network Address Translation is when the router has multiple network addresses available to use, which the OP clearly states that there is one external IP. He is describing Port Address Translation. –  Michael Bray Dec 26 '12 at 3:20

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.