Make sure that all settings under the Privacy tab and Xbox One/Windows 10 Online Safety are set to "Allow" or "Everyone". It should be like the screenshot below. Also, make sure to set these settings to "Allow" or "Everyone".
Check for Antivirus or Firewall Interference. The lost connection Terraria error can occur when your Windows Firewall or antivirus program is blocking you from accessing the server. Under this situation, you can allow the game through your Firewall or antivirus software.