It's an interesting situation when you find that the semantic differences between Switches, Hubs, and Routers actually have very explicit implications for your network.
In other words, the way a hub works is this: Any traffic sent to any port on the hub is echoed to every port on the hub. This is useful when you have to go long distances because the echo-feature allows you to boost or maintain the signal strength as it hops from hub to hub.Becaus the signal is replicated to all ports, hubs are also good for broadcast transmissions of data.
Switches are similar to hubs in so much as they provide universal access to data from all ports on all ports. Switches, like hubs, do not care what data is being transferred through them and therefore they have no problem switching packets to to disparate, but connected networks. Newer switches, of course, offer enhanced mechanisms that actually look at the packet header to determine the best switching path, but that is almost stepping on the toes of the router.
Routers disect the data packets, stripping off the header to determine which port to send the stream. If the destination of the stream is within the scope of the router's list of ports, it transfers it directly. If not, the packets are forwarded to another router or a gateway depending on it's predifined route plans.
Gateways, though very similar to routers, are specifically designed to route data outside of the scope of your lan. A gateway is usually connected to modem or some means of transferring your packets to the Wan or to a separate physical network. For the most part, gateways are like routers with the added ability to send packets upstream to the next wan segment.
-----------------------------
Now, you want to know how two pcs on the same network SWITCH can ping each other....
Simple all traffic on a switch is sent to all nodes on the network without knowing anything about the data. It's an electrical thing for the most part and that's it.
If you mean how do two pcs on a the same ROUTER ping each other on separate subnets,
It's because your router, in the name of efficiency, keeps track of the addresses that come into each port and it inspects every packet of data being sent through it to determine how to most efficiently get the packets where they need to go. So, if it knows that PC1 has an ip address of 10.0.0.1 and PC2 has an address of 192.168.1.1, it can choose to send the data back and forth directly in the name of expedience.
That is why they made VLANS. Sometimes you want to have two unique networks that work independently of each other yet share the same wiring. Routers use the concept of VLANS to ensure that packets between VLAN 1 and VLAN 2 do not skip any steps. In other words, even if the router knows that PC1 is on port 1 and pc2 is on port 2, if they are on separate VLANS, it will forward the data to the next router/gateway on its routing table as though it didn't know.
This can be useful for strong security typing where you have PC1 on a secure channel with SERVER 1 and PC2 on a separate secure channel with SERVER 1. Although SERVER 1 can forward packets back and forth between the two PCS (encrypting each portion of the conversation with a different secure key), you can see that if a router tried to skip SERVER1 and let PC1 talk directly to PC2 (because they are both physically connected to the router), they wouldn't understand each other because they didn't share the same key.
WIth a VLAN, the router won't try to skip SERVER 1 and PC1 won't be able to see PC2 without the help of SERVER 1.
Am I making any sense at all? Or am I talking in circles? Let me know if this helps or confuses the matter.
Best wishes,
jase