A router is a device that manages traffic flow around a network. It’s an intelligent computer that can be anything from a small home version with a few ports, to a massive enterprise scale version with hundreds of ports. There’s no point asking "how does a router work?" if you don’t know how a network operates. Don’t worry it isn’t as hard as it might seem.
Every networked machine in the world uses an IP, or Internet Protocol address. It’s like a house number and zip code in one. It looks something like 10.201.165.205, and is the primary way a router knows where to send traffic. Each router will have a routing table, which is like a phone book for it’s computers. It will know which computers and their IP addresses are connected to it, in order for it to send and receive traffic.
Networks of all kinds use packets to transmit data, these are little packages of information that comprise specific elements which we need to know a little about for now. Firstly there is the destination IP address, i.e. where that packet is headed. There is also the source address, which is the IP address of the computer that sent it. This and other information makes up the "packer header." The other main component of a packet is the payload. This is the actual information being transmitted and can be anything from a web page request to part of an email.
The router itself is built much like a computer. It has a version of a motherboard, a processor, RAM, and a flash disk instead of a hard drive. Rather than the single network port a computer has, it will either have a few, or many, depending on the size of the router. It also has software to control it and provide functions like the routing table, firewall and anything else the router offers. This is the centerpiece of the network, it is what makes a network function with any intelligence. It controls traffic, separates different networks and domains, it can provide firewall protections, and keep your home network safe from the outside world.
So, How does a router work?
The router connects a network and networked devices. In the case of a home or small business network, this will probably be an internet connection and some computers.
Every time the router receives traffic, it inspects the header to decide where to send it. If it’s an incoming packet, it looks at the destination address and checks to see if the computer with that address is connected to it. It then finds which port the computer is attached to and sends the packet through that port. If that address isn’t attached to the router, it will either send it to any other routers attached to it, or return the packet to the computer that sent it. If it has to return a packet to the sender, you might see an "address not found" message. This is from the destination router, which told your router or PC that it can’t find the IP address you wanted.
To decide if the address is connected to it, the router consults its routing table. This is a table in the software which tells the router which computer IP addresses are connected to which port. That’s how it knows where to send the packet.
If a computer on your network sends traffic to the router, it does exactly the same thing, except in the home network there are only two destinations to send the traffic. These will be the first and second choice enterprise routers of the ISP. They then do the routing on a much larger scale, but using exactly the same principles.
For example, say a packet arrives from the internet for IP address 192.168.101.1. The router holds the packet and looks in its routing table to see if it has a computer connected to it with that address. If a computer exists with that address, it checks the table to see which port it’s connected to, then forwards that packet to the destination through that port. On most modern routers, this whole process takes less than a second.
It does this for every single packet that it transmits or receives to ensure traffic goes to the correct destination.