Search Unity

Question Confused on how to bind to the correct port

Discussion in 'Game Server Hosting' started by Voliware, Jan 28, 2024.

  1. Voliware

    Voliware

    Joined:
    Feb 4, 2023
    Posts:
    15
    I am using the unity game server hosting (multiplay) stuff like dedicated server hosting and matchmaking. Or at least trying to. I have seen three ways to configure a dedicated server's port and I'm not sure which to use, other than the one that I'm using is not working.

    In short, I have a NetworkManager and for dedicated server builds I have a ServerManager. Here is my ServerManager code.

    Code (CSharp):
    1. using Unity.Netcode;
    2. using Unity.Netcode.Transports.UTP;
    3. using Unity.Services.Core;
    4. using UnityEngine;
    5.  
    6. public class ServerManager : MonoBehaviour
    7. {
    8.   private string ip = "0.0.0.0";
    9.   private ushort port = 7777;
    10.  
    11.   private void Start()
    12.   {
    13.     string[] args = System.Environment.GetCommandLineArgs();
    14.     for (int i = 0; i < args.Length; i++)
    15.     {
    16.       string arg = args[i];
    17.       if (arg == "-port")
    18.       {
    19.         if (i + 1 < args.Length)
    20.         {
    21.           this.port = arg[i + 1];
    22.         }
    23.       }
    24.     }
    25.  
    26.     this.StartServer();
    27.   }
    28.  
    29.   private async void StartServer()
    30.   {
    31.     Debug.Log($"Starting server at {this.ip}:{this.port}");
    32.  
    33.     await UnityServices.InitializeAsync();
    34.  
    35.     NetworkManager.Singleton
    36.       .GetComponent<UnityTransport>()
    37.       .SetConnectionData(this.ip, this.port);
    38.  
    39.     NetworkManager.Singleton.StartServer();
    40.   }
    41. }
    42.  
    When I create a test fleet/deployment, it says it's been created on port 9000, yet my logs show that it bound to port 111 (same port every time). Clearly something is wrong here. So another method I saw is this in the forums:

    Code (CSharp):
    1. public class BigRoxConnection : MonoBehaviour
    2. {
    3.     public ServerConfig ServerConfig;
    4.     public UnityTransport Transport;
    5.     [NonSerialized] public bool IsUnityServer;
    6.     async void Start()
    7.     {
    8.         try
    9.         {
    10.             await UnityServices.InitializeAsync();
    11.             Debug.Log("Initialized unity services");
    12.             ServerConfig = MultiplayService.Instance.ServerConfig;
    13.             Transport.ConnectionData.Address = "0.0.0.0";
    14.             Transport.ConnectionData.ServerListenAddress = "0.0.0.0";
    15.             Transport.ConnectionData.Port = ServerConfig.Port;
    16.             IsUnityServer = true;
    17.             Debug.Log("Unity server enabled.");
    18.         }
    19.         catch (InvalidOperationException)
    20.         {
    21.             Debug.Log("I am not a unity server.");
    22.         }
    23.     }
    24. }
    Is that the correct way to do it?

    Finally the third way I've seen is to not do anything, just leave it at default 7777 which seems even more wrong. For reference, this works fine locally, but not when I deploy it.
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,811
    you should be able to confirm if its working or not by looking at netstat.. when you say it doesnt work when deployed, what are you seeing?
     
  3. Voliware

    Voliware

    Joined:
    Feb 4, 2023
    Posts:
    15
    I get the error `Server is shutting down due to network transport start failure of Unity Transport!`

    The port is totally wrong.. it is giving me port `111` via command line where as it should be `9000` according to the table that lists all servers.

    This is in Unity's hosting platform
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,811
    well yeah 111 on a unix box requires root privilages to start - so you need to ask yourself why it would pick 111, what does debugging show you about the command line and the values it picked up?
     
  5. Voliware

    Voliware

    Joined:
    Feb 4, 2023
    Posts:
    15
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,811
    it would suggest its got the wrong command line yes?