I understand the differences between a switch and a router, but I don't understand the supposedly common practice of using a switch to extend/expand the number of ports provided by a router and where each device connected to the switch gets its own LAN IP address.
The switch and router operate on different layers (assuming the switch is not multi-layered) and hence I don't understand how it works.
Picture this scenario:
Router connects LAN to the WAN (public internet). Switch is connected to one of the router LAN ports. Devices (e.g. computers, laptops, etc.) are connected to the switch. Say there is device A connected to the switch and it wants to talk to device B (also connected to the switch). I understand that a switch allows the two devices to communicate without involving the router, but that is via MAC addresses (data-link layer). What if I want them to communicate using their LAN IP addresses, i.e. so I can use simple TCP socket programming?
Device A could send some data to Device B via a TCP socket, but how does the data get from Device A to the router and back to Device B, via the switch in both cases? The switch only knows MAC addresses, not IP addresses.