Okay, so I understand how subnet-masks are used to divide a network
into sub-networks, but my question is, why does every computer in the
network need to know the subnet-mask and not just the router?
well the devices people refer to as routers are usually not just routers.. hence people sometimes use terms like NAPT Router, or Home Router, or Consumer Router, to suggest that it's not purely a router. To make the point they're not just routers, these devices can do NAPT(which isn't routing), and they have a switch built in(a switch does bridging, which isn't routing - distinctions between a switch and a bridge are a bit not so well defined - one may say a bridge would often have 2 ports and connect different network mediums(e.g. ethernet and non-ethernet), whereas a switch would have multiple ports and the same network medium). A switch does bridging.
If the switch were separated from the "router", then indeed, it'd be more clear.. When the IP address is on the same network, then the packet is directed to goes down the cable to what is next physically which is the switch, and is ultimately destined to some other computer on the network(unless it was a managed switch and you were connecting to the switch e.g. telnet or http and the switch had its own ip), and since the packet is not destined for another network, the packet won't reach the router. When it's destined for a computer on a different network, then it of course still goes to the switch, but then continues after that to the router, (the switch directed the packet to the router and the destination MAC address of the packet coming in to the switch would've been the MAC address of the router), and the route would route it out of the correct interface of the router.
With these things typically called routers, that have switches inside them (like, not the professional style cisco/juniper routers), then the switch is inside.. But that's just the location of the switch.. It's still that when the IP is on the same network then the packet is addressed to the switch not to the router. And it goes only to the switch inside the router and doesn't reach the router.
I could understand it, if each computer were physically connected to
each other with a wire,
what do you mean here.. If all computers on an entire network were physically connected with a wire.. then I guess you wouldn't need a switch or router. What you're describing sounds a bit like original ethernet.. and if they're all connected with a wire it would likely not be such a big network. And anyhow it won't be a wire like you are used to. It'd have computers connected along the way to it with "taps". So I don't know why you just threw that sentence in.
but all packets needs to go through the router anyway.
so you mean putting aside that idea of all computers connected with a wire with no router.
And no, even in your home set up, they don't go through the router every time. Even with your "home router" , call it an internet box. They go to the switch in it.
Lets say that I have a computer on a network
192.168.0.0/255.255.255.0, which have the IP 192.168.0.1.
If that computer try to reach a computer outside the sub-network, lets
say 192.168.1.1, it transmits the message to the router,
to the switch then from the switch it goes to the router
the router identyfies that the IP is outside the sub-network IP-range, and rather than transmitting it on the sub-network, it transmits it to the network it is connected to (perhaps another router).
well the router identifies what network interface it's for. It sends from one interface to another interface. One distinction - besides which interface, would be whether a network is directly connected or not. It could send on to a switch then to a computer. Or it could send to a computer. Or if the network isn't directly connected it'd go to another router.
and your last sentence was
it transmits the message to the router,
and then what, did you suddenly decide to stop writing?
the router identyfies that the IP is outside the sub-network
IP-range, and rather than transmitting it on the sub-network, it
transmits it to the network it is connected to (perhaps another
router).
well, I wouldn't put it like that. Each interface on the router has a different IP range.
But, with your consumer router or home router, what happens is it's like a router with two interfaces, one your side has a switch connected to it. And the multiple ports are ports of the switch.
So if you think in terms of the router part, it's not like oh this is inside the subnetwork and this is outside, 'cos there's potentially many networks. There's one on each interface. And the router is not going to be transmitting it back to the same network it came from. The reason why it reached the router in the first place is precisely because the switch (which it reached first), saw the MAC address and so saw that that's not the MAC address of the router.
The computer that sent the packet out, will check whether the destination IP of the packet is on its own network, or the same network, and will then based on that, choose the relevant MAC address. Either the MAC address of the computer it's destined to (that's if the computer is on the same network). Or (if the computer is on another interface of the router), then it'll be the MAC address of the router. I guess that might answer the title of your question quite directly, as to why the computer would need to know the subnet mask.. In the TCP/IP system as it is.. that's how it works, the computer picks the relevant layer 2 address e.g. for ethernet, the MAC address.
31Computers do not need to go through a router in order to form a network. – Overmind – 2017-03-22T12:26:26.140
1@Overmind: True, but they can. Netmasks are indeed not strictly necessary, they're an optimization to ease the work for the router . – MSalters – 2017-03-23T15:41:35.973
3
if each computer were physically connected to each other with a wire
- you have to remember that tcp/ip was invented at a time where this is indeed the case. Google10-base-2
. There are other protocols that work differently from tcp/ip but over the last 20 years IPv4 won the protocol battle. – slebetman – 2017-03-24T02:08:30.030You seem to have a misunderstanding of how computer networks work. I suggest you watch this video: https://www.youtube.com/watch?v=6i777lddg8s
– InterLinked – 2017-03-29T02:21:08.013@InterLinked with a narrator with such a creepy voice? never. – devoured elysium – 2019-06-10T01:34:32.770
You can look up a token ring where all computers are connected in a circle. Then, they would need to know the subnet mask/ CIDR. Also, networks can use switches instead and all network traffic does not go through the gateway. – jonnyjandles – 2020-02-10T23:56:18.957