How does the internet know where to send data?

"The internet" as an entity is a very large collection of devices called routers which are connected together to form a big "web", and individual computers which are connected (indirectly) to one such router, owned by your ISP (Internet Service Provider). By 'hopping' from one router to the next, it is possible to reach any other computer on the internet: the trick is knowing where to hop.

Every 'node' on the internet, such as your computer or the computer hosting this website, has what is called an IP Address. The routers that form the 'net' (middle bit) itself can have multiple IP Addresses - typically one for each connection to another router or computer.

IP Addresses are numbers of the form a.b.c.d, such as 192.168.0.1. The parts A through D are numbers in the range of 0-255, so the smallest IP address is 0.0.0.0 and the highest is 255.255.255.255 (in practice, these example addresses and many others are reserved for special usage). An analogy can be drawn between IP addresses and house addresses, with A being the least specific part (analogous to 'Country') and D being the most specific (analogous to 'House number') - however IP addresses generally bear absolutely no relation to street address, and it is not possible to work out one given the other.

The internet routers operates on a principle which is called packet switching. This works similarly to the postal service. If I post a letter to a friend in another country, the postman does not immediately know where to send the letter and take it there his/herself. Instead, he will take it to the local sorting office, who will send it to a bigger (international) sorting office, who will put it on a boat and send it to the destination country, and it will then filter down through more and more specific postal destinations until it is delivered to the most specific of all: the exact recipient.

Each router forming the internet knows how many other routers it is connected to, and it is also aware of the IP address of each of these connections. By asking all of its neighbours "What else are you connected to?" it is possible to build up what is called a Routing Table. This is a generalised set of rules which basically equate to "Data going to 134.101.x.x should be sent this way".

When your computer sends data to another, it is in the form of a stream of packets, which is essentially your data inside an 'envelope' of sorts. The envelope holds source and destination information, as well as the number of hops left before this packet counts as 'lost' and can be discarded (called the Time To Live or TTL). Each router along the way that receives your packet forwards it based on its routing table. At each step, the TTL is also reduced by one. If a router sees a packet with TTL=0, it discards it - this is to prevent situations where the same packet ends up going in circles, and similar.

Going back to our international letter example, an analogy using a computer-to-computer data exchange such as a file transfer works like this:

1. The data is sent by your computer to your Internet Service Provider's router(s). This equates to the postman picking up the letter and taking it to the sorting office.

2. The ISP inserts it in to the 'net' of other routers by sending it to the most appropriate of its neighbours. This equates to the letter being sorted locally and sent to a higher level sorting office.

3. The routers forward it ever closer using their routing rules. This equates to intermediate sorting offices sending the letter to international offices, who send it to a port, who put it on a boat, and so on.

4. It arrives at the destination computer's ISP. This equates to the letter arriving at the local sorting office it the destination town.

5. The ISP's router sends it to the target computer. Finally, the postman delivers the letter to your friend's house.

To summarise:

The internet (or the devices that make it up) does not so much 'know' where to send data, but rather can figure out a route based on the source and destination IP Address, having discovered who their neighbours are and built up a set of rules that allow them to route traffic closer to the destination one hop at a time.