Routers are the traffic cops of the Internet. They ensure that all data gets sent to where it's supposed to go via the most efficient route. When you sit down at your computer and connect to the Internet and send or receive data, generally that information first must go through at least one routerand often more than one routerbefore it reaches its final destination.
Routers open the IP packets of data to read the destination address, calculate the best route, and then send the packet toward its final destination. If the destination is on the same network as the sending computer, such as within a corporation, the router sends the packet directly to the destination computer. If the packet is going to a destination outside the local network, the router instead sends the packet to another router closer to the destination. That router in turn sends the packet to a yet-closer router, and so on, until the packet reaches its final destination.
When routers determine the next router to receive packets, factors such as traffic congestion and the number of hops (routers or gateways on any given path) come into play. The IP packet carries a segment that holds the maximum hop counts it can travel, and a router will not use a path that would exceed that predefined number of hops.
Routers have two or more physical ports: receiving (input) ports and sending (output) ports. When an input port receives a packet, a software routine called a routing process is run. This process looks inside the header information in the IP packet and finds the address to which the data is being sent. It then compares this address against an internal database called a routing table. The routing table has detailed information about the ports to which packets with various IP addresses should be sent. Based on what it finds in the routing table, the router sends the packet to a specific output port. This output port then sends the data to the next router or to the destination itself.
There are two kinds of routing tables: static routing tables and dynamic routing tables. The static table is simpler, and specifies specific paths for packets to use to get to their final destination. The dynamic table allows a packet to have multiple routes to get to its destination.
At times, packets are sent to a router's input port faster than the port can process them. When this happens, the packets are sent to a special holding area called an input queue, which is an area of RAM on the router. That specific input queue is associated with a specific input port. A router can have more than one input queue. Each input port processes packets from the queue in the order in which they were received, so the first packets sent in are the first to get processed and sent out.
If the number of packets received exceeds the capacity of the queue (called the length of the queue), packets might be lost. When this occurs, the TCP protocol on the sending and receiving computers have the packets re-sent.
Note that the router in the following illustration is of the type that routes data throughout the Internet, and isn't the same as a home router that forms the basis of a home network. For information about how a home router works, see Chapter 10, "How Home Networks Work."