Two hosts in the same LAN
A will send an ARP broadcast in the LAN: My IP is xxx, mac is XXX. I want to know what is the mac of B. This broadcast will be received by all hosts in the LAN, but only B will respond and reply with an ARP to A. response.
Two hosts in different LANs
1. Since B's IP address is not in the same network segment as A, when A sends data to B, A will not directly send the data to B, but to its own gateway Router1.
2. After Router1 receives the frame, it checks the target IP of the frame, and searches its own routing table for how to reach the network segment. It finds that it can be reached, and the next hop address is the s0 port of routerB, so it re-encapsulates the data and The source address is changed to the s0 port MAC address, the destination MAC address is changed to the s0 port MAC address of router2, and sent to Router2
3. The principle of intermediate routing is the same. . . . . .
4. The last route (routerN) receives the frame and finds that the target IP is in its directly connected network segment, so it checks the ARP cache. If the MAC address of the IP is found, it encapsulates the data with the MAC address and sends it out. If the ARP cache is not found, an ARP broadcast is sent to request the MAC address of the IP. After obtaining the corresponding MAC address, it is sent to host B.