Switch – How to Communicate Between Devices on a Switch via IP

ipmac addressrouterswitch

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.

Best Answer

This is a simplified version of what happens:

The switch (assuming not a layer-3 switch) only involves itself with MAC addresses. This is where ARP comes into play. The sending PC will ARP, using the layer-3 address, and the receiving PC with that layer-3 address will respond with its layer-2 address. The sending PC then encapsulates the layer-3 packets into layer-2 frames, and the switch will switch the layer-2 frames.