When you use a computer in a busy environment, you get your local IP address via DHCP. This is referred as a 'lease' on the use of that IP address. You can only use and hold the lease for its lease duration, and never beyond the lease period, or your traffic may collide with a different computer's data.
So when you Mac goes to sleep, it does not know whether it will still be sleeping when the DHCP lease on its local IP address expires. To avoid issues, it releases its IP address.
it is possible, with certain settings, that when your display goes dark, you Mac also sleeps.
System preferences > Battery > Power adapter...
Check :
[√] Prevent computer sleep when the display is OFF
it will warn you this setting uses more power.
--------
These settings seem to vary wildly between different versions of MacOS:
Set sleep and wake settings for your Mac - Apple Support