Before problem happens, are you able to ping the DNS server?
when problem happens, are you still able to ping the DNS server?
when problem happens, are you still able to ping 8.8.8.8?
you can try if there is improvement by disabling edrx feature by AT+CEDRXS=0
Will the DNS work fine after disconnecting “cm data” and connecting “cm data”?
BTW, does it work with gethostname() by using real IP address instead of host name resolution?