Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

Issue with NetworkManager.StartServer() when compiled and ran as a Windows Dedicated Server platform. #3066

Closed Jayson-Furr closed 2 months ago

Jayson-Furr commented 2 months ago

Description

A clear and concise description of what the bug is when I use the following code and compile it to a Windows Dedicated Server platform build the default address and port are used instead of the set address and port. This does not happen when running in the editor, only when I build and run the binaries. If I use the code and compile it to a Windows platform build, everything works fine.

` NetworkManager.Singleton.GetComponent().SetConnectionData("127.0.0.1", 57111);

Debug.Log($"Server Address Endpoint: {NetworkManager.Singleton.GetComponent().ConnectionData.ServerEndPoint.ToString()}"); Debug.Log($"Server Listen Endpoint: {NetworkManager.Singleton.GetComponent().ConnectionData.ListenEndPoint.ToString()}");

Debug.Log("Server Manager Starting Server."); NetworkManager.Singleton.StartServer();

Debug.Log($"Server Address Endpoint: {NetworkManager.Singleton.GetComponent().ConnectionData.ServerEndPoint.ToString()}"); Debug.Log($"Server Listen Endpoint: {NetworkManager.Singleton.GetComponent().ConnectionData.ListenEndPoint.ToString()}"); `

Reproduce Steps

  1. Add a NetworkManager with UnityTransport to the scene.
  2. Add the above code to a script's Start method and add it to an object in the scene.
  3. Compile the project as a Windows Dedicated Server build and run.
  4. View the debug logged results.
  5. Compile the project as a Windows build and run.
  6. View the debug logged results.

Actual Outcome

The default address, 127.0.0.1, and the default port, 7777, are used.

Expected Outcome

The set address, 127.0.0.1, and the set port, 57111, should be used.

Environment

NoelStephensUnity commented 2 months ago

Hi @Jayson-Furr, I am having a hard time replicating this issue. In order to help determine what the issue could be, I have attached a small project that has a dedicated server profile and scripts that follow the same pattern you listed above. DSListenConfig.zip

What I am logging is:

When you open the sample scene and select the NetworkManager in-scene placed GameObject you will see both a NetworkManagerHelper component and the UnityTransport component. Within the NetworkManagerHelper component there are two dedicated server specific properties, ListenAddress and ListenPort, that will be used to override the UnityTransport's settings. image

Within the NetworkManagerHelper script file, you will find the StartDedicatedServer method that contains the following script:

            Debug.Log($"[Pre-Init] Server Address Endpoint: {NetworkManager.Singleton.GetComponent<UnityTransport>().ConnectionData.ServerEndPoint}");
            Debug.Log($"[Pre-Init] Server Listen Endpoint: {NetworkManager.Singleton.GetComponent<UnityTransport>().ConnectionData.ListenEndPoint}");

            // Setup your IP and port sepcific to your DGS
            unityTransport.SetConnectionData(ListenAddress, ListenPort, ListenAddress);

            Debug.Log($"[Post-Init] Server Address Endpoint: {NetworkManager.Singleton.GetComponent<UnityTransport>().ConnectionData.ServerEndPoint}");
            Debug.Log($"[Post-Init] Server Listen Endpoint: {NetworkManager.Singleton.GetComponent<UnityTransport>().ConnectionData.ListenEndPoint}");

Then you will find within the OnServerStarted method (below the StartDedicatedServer method) the following script that logs the endpoint settings once the dedicated server is started:

        Debug.Log("Dedicated Server Started!");
        Debug.Log($"[Started] Server Address Endpoint: {NetworkManager.Singleton.GetComponent<UnityTransport>().ConnectionData.ServerEndPoint}");
        Debug.Log($"[Started] Server Listen Endpoint: {NetworkManager.Singleton.GetComponent<UnityTransport>().ConnectionData.ListenEndPoint}");

Could verify this works as expected for you and then see if there might be any differences between your project and the one included? If you do find any large differences between the two, could you modify the included project to replicate the issue you are experiencing so I can then try to replicate the issue on my end?

Jayson-Furr commented 2 months ago

Noel, I located this just earlier and realized that this package was overwriting my values and now see it is working as intended to. I was not immediately aware that the dedicated server package already did this and was attempting to implement it own my own.

You can close this as it is not an issue.

Thanks again!