To fully understand how computer networks work, you need to look at the OSI model (or in practice, the TCP / IP or DoD model). For your question, you first need to look at three levels: physical, data channel and network.
The physical connection is self-evident and is a direct connection through some medium (copper, glass, air).
When host A tries to send a packet to host B, the first thing it will do is look at the recipient’s IP address and, based on its own IP configuration, determine if host B is on the same subnet as host A. This has already been explained to you Eugene Reik: the subnet mask bit is used to mask the bits of the IP address (logical AND operation). Now we have two cases:
- Host A and B are on the same subnet.
- Host A and B are not on the same subnet.
It should be noted that at level 2, which network adapters use to send and receive frames, there are no IP addresses (which are present at level 3), but instead, communication between devices is performed using MAC addresses. Because of this, a host can only communicate directly with hosts on its subnet (1st scenario). To send the frame host, the MAC address of host B is required. Thus, host A first looks for the MAC address mapped to the IP address of host B in its ARP table. If he cannot find it, he sends a broadcast ARP request with the request of the entire host on the subnet that has a specific IP address. If he receives a response, he adds the MAC address of the host to which he received a response and creates a packet with the destination MAC address of this host and the IP address of this host.
If both hosts are not on the same subnet (second scenario), the packet is sent to the default gateway, which is responsible for finding the route to the destination. The key point here is that even if the destination MAC address in this case is the MAC address of the router (default gateway), the destination IP address is still the IP address of host B, as in the first scenario. As the packet moves from the router to the router, the source and destination MAC addresses will change because they are locally significant, but the source and destination IP addresses will remain unchanged. Thus, each layer provides a service (so to speak) to the upper levels, and the upper layers use it transparently, without requiring to know what happens below.
So you have:
1st scenario.
Second scenario:
To summarize (@Eugen Rieck's answer already gave you):
Two hosts that are not on the same subnet as in your example (192.168.0.1/24 and 192.168.1.1/24) will not be able to communicate at level 2, and for this you will need a device with L3 support, such as a router, act as the default gateway and route traffic between two networks (broadcast domains) for a layer 3 connection.