maximum hosts cannot exceed {16}

maximum hosts cannot exceed {16}

As of 06/10/2015, Unity version 5.2.1f1 :

How to reproduce :
Create network manager, attach a HUD, click 17 time on Lan Client (C)

Maybe linked issue :

Also, I tried to fix it by myself :

public NetworkManager ntm;

[…]
=> when disconnecting :
NetworkTransport.RemoveHost(ntm.client.connection.hostId);
ntm.client.Disconnect();

Even when removing the Host manually, doesn’t work.

Is there a way to reset the Host count in NetworkTransport ?

Did you actually file a bug report? Just curious.

this is a known bug. will be fixed in a 5.2 patch release

This is still not fixed in 5.2.1p4. Any idea when the fix will be released?

@seanr didn’t say it’s going to be in 5.2.1p4. For all I know, it could be 5.2.1p10 if they want to.

This bug was fixed and verified in 5.2.1p3

Ok, that’s strange, I have it still in 5.2.1p4.
If I understand right it always happens after trying to connect a few times to a server which is not available. In my project the game tries to connect to the (unet-) master server. If the server is not reachable or offline, this fails with a timeout error. Then the game tries to connect again after a short break. If I leave the game running while the server is offline, after a few (probably 16) times this error occurs and I can’t join or start a server or host a game anymore.

I’ll try to create a smaller repro and will report this as a new issue.

Here’s a script that produces the error:

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using System.Collections;

public class Connector : MonoBehaviour {

    int connectionAttemptCount;
    NetworkClient client;
    bool errorHappened;

    void Update () {

        if (errorHappened) {
            errorHappened = false;
            StopClient ();
        }

        if (client == null )
            StartClient ();
    }

    void StartClient()
    {
        client = new NetworkClient ();
        client.RegisterHandler (MsgType.Connect, OnConnected);
        client.RegisterHandler (MsgType.Disconnect, OnDisconnected);
        client.RegisterHandler (MsgType.Error, OnError);

        // short timeouts
        var config = new ConnectionConfig();
        config.ConnectTimeout = 100;
        client.Configure (config, 1);
        client.Connect ("2.2.2.2", 1234); // some non-connectable server to create a connection timeout

        connectionAttemptCount++;
        Debug.LogFormat ("New connection attempt {0}...",connectionAttemptCount);
    }

    void StopClient()
    {
        print ("Stopping client: hostid=" +client.connection.hostId);

        // can't do it here, would spam this error: host id {0} has been already deleted
        //NetworkTransport.RemoveHost (client.connection.hostId);
        //client.Disconnect ();

        client.UnregisterHandler (MsgType.Connect);
        client.UnregisterHandler (MsgType.Disconnect);
        client.UnregisterHandler (MsgType.Error);
        client.Shutdown ();
        client = null;
    }

    void OnConnected(NetworkMessage netMsg)
    {
        print ("connected");
    }

    void OnDisconnected(NetworkMessage netMsg)
    {
        print ("disconnected");
    }

    void OnError(NetworkMessage netMsg)
    {
        // we just assume it's a timeout error
        errorHappened = true;
    }
}

This tries to connect to a server that doesn’t exist and fails with a timeout. After failing, it repeats the same thing over and over again. After 16 times it fails with said error.

Maybe I’m doing something wrong when cleaning up the NetworkClient instance, but I have no idea what it is. I also tried to call NetworkClient.Disconnect() or NetworkTransport.RemoveHost() before shutting down, but that just spammed the console with “host id {0} has been already deleted” errors and breaks the loop in this example. The host id of the client keeps increasing until it reaches 15, which causes the “maximum hosts cannot exceed {16}” error.

Got a GIF where it shows how the error messages are generated? Just curious how it looks.

2345355--158694--Untitled.gif

This appears to be a slightly different issue. A NetworkClient that never connects does not remove it’s hostId.

Filed bug 737538

Here is a workaround:

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using System.Collections;
public class Connector : MonoBehaviour {
    int connectionAttemptCount;
    NetworkClient client;
    bool errorHappened;
    void Start () {
        StartClient ();
    }
    void StartClient()
    {
        client = new NetworkClient ();
        client.RegisterHandler (MsgType.Connect, OnConnected);
        client.RegisterHandler (MsgType.Disconnect, OnDisconnected);
        client.RegisterHandler (MsgType.Error, OnError);
        // short timeouts
        var config = new ConnectionConfig();
        config.ConnectTimeout = 100;
        client.Configure (config, 1);
        client.Connect ("2.2.2.2", 1234); // some non-connectable server to create a connection timeout
        Debug.LogFormat ("New connection attempt {0}...",connectionAttemptCount);
    }
    void StopClient()
    {
        print ("Stopping client: hostid=" + (connectionAttemptCount));
        // can't do it here, would spam this error: host id {0} has been already deleted
        NetworkTransport.RemoveHost (0);
        //client.Disconnect ();
        client.UnregisterHandler (MsgType.Connect);
        client.UnregisterHandler (MsgType.Disconnect);
        client.UnregisterHandler (MsgType.Error);
        client.Shutdown ();
        client = null;
        connectionAttemptCount += 1;
    }
    void OnConnected(NetworkMessage netMsg)
    {
        print ("connected");
    }

    private void OnDisconnected(NetworkMessage netMsg)
    {
        print("disconnected");
        StartCoroutine(Reset());
    }

    IEnumerator Reset()
    {
        StopClient();
        yield return new WaitForSeconds(1);
        StartClient();
    }
    void OnError(NetworkMessage netMsg)
    {
        // we just assume it's a timeout error
        errorHappened = true;
    }
}
1 Like

Thanks for the workaround @seanr , works great so far!

Hello,
the bug is still there… Unity 5.3.1p1
but

public override void OnStopHost() {
        myNetworkDiscovery.StopBroadcast();
        NetworkTransport.RemoveHost (0);
}

made it work!
the key is first stop broadcasting then remove the host otherwise you get some error msg…
thx
M.

I think the original bug is more of how it is designed, rather than letting the developers use this workaround. Either these workarounds should be listed in the documentation, or it needs to be revised, I feel.

Also, are you sure your bug is related to clicking 17 times on the LAN client in the NetworkManagerHUD?

I’ve stumbled across this bug also. I have a server supervisor that checks if server is alive. If it hangs for some reason - it should stop it, restart and connect to new instance of server. All worked well until I’ve introduced reconnect to new instance. After 16 retries of connecting I get same error as above.

How I’ve solved it?

private void ClearHostIDs()
    {
        if(hostIDs.Count != 0)
        {
            var clearList = new List<int>();
            foreach(int id in hostIDs)
            {
                if(client.connection != null)
                {
                    if(id != client.connection.hostId)
                    {
                        NetworkTransport.RemoveHost(id);
                        clearList.Add(id);
                    }
                } else
                {
                    NetworkTransport.RemoveHost(id);
                    clearList.Add(id);
                }
            }

            foreach(int id in clearList)
            {
                hostIDs.Remove(id);
            }
        }
    }

Where I add hostIDs to list after I TRY to connect (not on OnConnect network event). I run this method every time I try to reconnect.

Hope this helps someone:)