How to detect if there is a server running

Hi , i’m sorry ,it seems to be so simple but i 'm a beginner in networking and i didn’t find any answer for that :

i want my first app user to be automatically the server and then all others will be clients , i use MLAPI system
that’s the code i tried but it didn’t work :

  private void Awake()
    {
      
        MakeSingleton();
        SetupParamsFromPrefs();
        SetupConnection();
    }
private void SetupConnection()
    {

        if (NetworkManager.Singleton.ConnectedClientsList.Count > 0)
        {
            Debug.Log("there are alraedy clients ,we start as a client");
            StartNetworkClient();
        }
        else
        {
            if (NetworkManager.Singleton.NetworkConfig.ConnectionApproval)
            {
                Debug.Log("there s already a server , we start as client");
                StartNetworkClient();
            }
            else
            {
                Debug.Log("there is no client , we start as serverserveur");
                StartNetworkServer();
            }
        }
    }

    public void StartNetworkClient()
    {
        if (NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsClient)
        {
            return;
        }
        else
        {
            NetworkManager.Singleton.StartClient();
        }
    }
    public void StartNetworkServer()
    {
        if (NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsClient)
        {
            return;
        }
        else
        {
            NetworkManager.Singleton.StartServer();
        }
    }

Thanks in advance for your help , i guess i missed something …

What you are trying to do is not out of the box possible with MLAPI. You cannot just have one player run as a server and other clients will magically connect to it. NetworkManager.Singleton.ConnectedClientsList is the list of connected clients to the server when running as a server. You cannot access it before connecting.

I’m not sure whether this helps you to implement something but the system you are trying to achieve is usually done in the following way:

  • A relay transport which supports rooms by room name is used. MLAPI has a SteamP2P and a Photon Realtime transport which support that use case.
  • When starting a game the client will try to connect to an existing room (.StartClient) with the given name. If it works then everything is fine and the client joins the existing room as a client. If it fails the client will instead create a room with the name by calling .StartServer

Thank you for the answer , i followed this workflow to try understand how transport works :

https://docs-multiplayer.unity3d.com/docs/transport/minimal-workflow

But i’m not sure if i can work with both :MLAPI and this network / transport workflow ( i guess i can’t ) .
Otherwise i found SteamP2P and Photon Example that you mentionned here :

https://github.com/Unity-Technologies/mlapi-community-contributions/tree/master/Transports/com.mlapi.contrib.transport.photon-realtime
As i founb several others transports like ruffles , Enet …

in order to build for mobile (android version ) and access network via Room system ; which would you recommend ?
Thanks a lot for your help !

If you want to run on android and have it just work so that anyone can create a room and others can join it, I’d recommend starting with the Photon Realtime Transport.

1 Like