Can't connect UNet server, it just timed out

I made a simple multiplayer game by some tutorial in Unity 5.6.1.f1. It works fine in localhost, but when I started server in the remote(as headless on Amazon Linux) and try to connect from this computer, it’s just keep waiting until it’s timed out.

This is the code what am I using for starting server in headless mode:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class AutoServer : MonoBehaviour {
	void Awake() {
		// Runs on batch/headless mode
		if(SystemInfo.graphicsDeviceID == 0) {
			Debug.Log("START SERVER");
			GetComponent<NetworkManager>().StartServer();
		}
		else {
			Debug.Log("NOT RUN SERVER");
		}
	}
}

It’s just simple, and this is the log when I started Unity server(Amazon Linux):

[ec2-user@ip-172-31-22-2 unity-server]$ ./headless.x86
Set current directory to /home/ec2-user/unity-server
Found path: /home/ec2-user/unity-server/headless.x86
Mono path[0] = '/home/ec2-user/unity-server/headless_Data/Managed'
Mono path[1] = '/home/ec2-user/unity-server/headless_Data/Mono'
Mono config path = '/home/ec2-user/unity-server/headless_Data/Mono/etc'
Player data archive not found at `/home/ec2-user/unity-server/headless_Data/data.unity3d`, using local filesystem

And this is debug log which located at ~/.config/unity3d/Player.log:

[ec2-user@ip-172-31-22-2 mp_test]$ cat Player.log 
Initialize engine version: 5.6.1f1 (2860b30f0b54)
Forcing GfxDevice: Null
NullGfxDevice:
    Version:  NULL 1.0 [1.0]
    Renderer: Null Device
    Vendor:   Unity Technologies
AudioManager: Using Unknown: NoSound Driver
Begin MonoManager ReloadAssembly
Platform assembly: /home/ec2-user/unity-server/headless_Data/Managed/UnityEngine.dll (this message is harmless)
Loading /home/ec2-user/unity-server/headless_Data/Managed/UnityEngine.dll into Unity Child Domain

...

UnloadTime: 0.706000 ms
START SERVER
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

NetworkServerSimple initialize.
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

NetworkServerSimple listen: :7777
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:35 handler:OnClientReadyMessage
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:5 handler:OnCommandMessage
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:6 handler:HandleTransform
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:16 handler:HandleChildTransform
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:38 handler:OnRemovePlayerMessage
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:40 handler:OnAnimationServerMessage
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:41 handler:OnAnimationParametersServerMessage
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:42 handler:OnAnimationTriggerServerMessage
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandlerSafe id:17 handler:OnFragment
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler id:32 handler:OnServerConnectInternal
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler id:33 handler:OnServerDisconnectInternal
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler replacing 35
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler id:35 handler:OnServerReadyMessageInternal
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler id:37 handler:OnServerAddPlayerMessageInternal
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler replacing 38
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler id:38 handler:OnServerRemovePlayerMessageInternal
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterHandler id:34 handler:OnServerErrorInternal
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

NetworkManager StartServer port:7777
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

SpawnObjects sceneId:1 name:Enemy Spawner
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

SpawnObject instance ID 2 asset ID c218989b46640ff4db5f77aeab7c8a3a
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

SpawnObject instance ID 3 asset ID c218989b46640ff4db5f77aeab7c8a3a
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

SpawnObject instance ID 4 asset ID c218989b46640ff4db5f77aeab7c8a3a
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

SpawnObject instance ID 5 asset ID c218989b46640ff4db5f77aeab7c8a3a
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

SpawnObject instance ID 1 asset ID 00000000000000000000000000000000
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterStartPosition:Spawn Position 1 (UnityEngine.Transform)
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

RegisterStartPosition:Spawn Position 2 (UnityEngine.Transform)
 
(Filename: /home/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

Setting up 1 worker threads for Enlighten.
  Thread -> id: e8b4ab40 -> priority: 1 

It seems server is working well, but I couldn’t connect. I already set my server accessible anywhere with port 7777(which is default port number of unity server).

I also tried with disable firewalls in this computer but it still won’t worked. Also I tried with IP Address and Domain name both, but both won’t worked.

In the client(my computer), it fails to connect with this message:

ClientDisconnected due to error: Timeout
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

My server using Amazon Linux(4.4.51-40.58.amzn1.x86_64) and my computer using Windows 10. Is there a something that I missed? I don’t get it why it doesn’t work.

If there is something that I wasn’t wrote here, please tell me and I’ll be add more details. Any advice will very appreciate it, thanks!

You must open port 7777 (or whichever port your game is using) for both TCP and UDP traffic.

I was having a similar problem running my UNET-based multi-player headless server in a Docker Linux container. Only after opening both 7777/TCP and 7777/UDP, my client was able to communicate with my game server. For others trying to get their headless server working in docker, the Docker command is:

docker run -p 7777:7777/tcp -p 7777:7777/udp <docker_image_name>