NetworkManager: Maximum hosts cannot exceed {16}

maximum hosts cannot exceed {16}
UnityEngine.Networking.NetworkManager:StartClient()
s_NetworkManager:JoinGame() (at Assets/Scripts/Custom_Network/s_NetworkManager.cs:54)
UnityEngine.EventSystems.EventSystem:Update()

Unity Version : 5.4.0b23

I’m using a custom network manager:

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Collections;
using System;

public class s_NetworkManager: NetworkManager {


    public s_StatusInfo status;

    /// <summary>
    /// Listener for the Start button.
    /// </summary>
    public void StartGame()
    {
        NetworkManager.singleton.StopHost();
        NetworkManager.singleton.networkPort = 7777;
        NetworkManager.singleton.StartHost();
    }


    /// <summary>
    /// Listener for the Join button.
    /// </summary>
    public void JoinGame()
    {
        if (s_NetworkManagerUI.singleton.ipAdress)
        {
            if (s_NetworkManagerUI.singleton.ipAdress.text.Length > 0)
            {
                string[] adress = s_NetworkManagerUI.singleton.ipAdress.text.Split(':');
                string ip = "";
                string port = "";

                if (adress.Length >= 2)
                {
                    ip = s_NetworkManagerUI.singleton.ipAdress.text.Split(':')[0];
                    port = s_NetworkManagerUI.singleton.ipAdress.text.Split(':')[1];
                }



                if (!string.IsNullOrEmpty(ip))
                    NetworkManager.singleton.networkAddress = ip;
                else
                    NetworkManager.singleton.networkAddress = "localhost";

                if (!string.IsNullOrEmpty(port))
                    NetworkManager.singleton.networkPort = int.Parse(port);
                else
                    NetworkManager.singleton.networkPort = 7777;

                NetworkManager.singleton.StartClient();
            }
            else
            {
                Debug.LogError("Put a valid IP Adress!");
            }
        }
    }

    /// <summary>
    /// Listener for the Disconnect button.
    /// </summary>
    public void LeaveGame()
    {
        NetworkManager.singleton.StopHost();
    }

    /// <summary>
    /// Called when the local client connects to a server.
    /// </summary>
    /// <param name="conn"></param>
    public override void OnClientConnect(NetworkConnection conn)
    {
        base.OnClientConnect(conn);
        status.UpdateStatus("connected to a room...");

        s_NetworkManagerUI.singleton.SetUsername();
        s_NetworkManagerUI.singleton.EnableConnectPanel(false);
        s_NetworkManagerUI.singleton.EnableDisconnectPanel(true);
        s_NetworkManagerUI.singleton.EnableGroupSelectPanel(true);
    }

    /// <summary>
    /// Called on the host when the server is stopped.
    /// </summary>
    public override void OnStopHost()
    {
        base.OnStopHost();
        status.UpdateStatus("menu...");

        s_NetworkManagerUI.singleton.EnableChatPanel(false);
        s_NetworkManagerUI.singleton.EnableDisconnectPanel(false);
        s_NetworkManagerUI.singleton.EnableGroupSelectPanel(false);
        s_NetworkManagerUI.singleton.EnableConnectPanel(true);
    }

    /// <summary>
    /// Called when the local client disconnects from a server.
    /// </summary>
    /// <param name="conn"></param>
    public override void OnClientDisconnect(NetworkConnection conn)
    {
        base.OnClientDisconnect(conn);
        status.UpdateStatus("menu...");

        s_NetworkManagerUI.singleton.EnableChatPanel(false);
        s_NetworkManagerUI.singleton.EnableDisconnectPanel(false);
        s_NetworkManagerUI.singleton.EnableGroupSelectPanel(false);
        s_NetworkManagerUI.singleton.EnableConnectPanel(true);
    }
}

How to reproduce:

  • Insert an ip to an offline or unreachable server.
  • Press join 17 times.

I’ve tried to remove the host manually and it doesn’t work.
I’m not sure if this is a bug or I’m doing something wrong.

Did a quick test with 5.4.0p1 and did not get a repro.

Could you download the latest version and see if it reproduces?
If it does, please file a bug report and send me the case number.

I just tried with the 5.5.0b2 version and I still get the error.
Submitted a bug report and this is the case number : 830700

I have the same problem. Connecting to the different combat servers after 17 times get the same error

Bug more than a year! I’m in amazement.

using UnityEngine;
using UnityEngine.Networking;

public class NetworkClientMy : NetworkClient
{
    public NetworkClientMy(string _host, int _port) {
        Connect(_host, _port);
    }

    public void ReConnect(string _host, int _port)
    {
        if(isConnected)
        {
            Disconnect();
//                   NetworkTransport.RemoveHost(connection.hostId);
        }
    
               Connect(_host, _port);
//                  ReconnectToNewHost(_host, _port);
    }
}

In 16th ReConnect error occurs: maximum hosts cannot exceed {16}. Can someone tell whether there is a solution how to bypass the error?

I take a look on NetworkManager source code from https://bitbucket.org/Unity-Technologies/networking/src.

They StopClient by Disconnect then Shutdown the old NetworkClient instance.

if (client != null)
            {
                // only shutdown this client, not ALL clients.
                client.Disconnect();
                client.Shutdown();
                client = null;
            }

I think we have to use new NetworkClient to reconnect. I create a Reconnect function in that way and I see no error occurs.

public void Reconnect()
        {
            if (client != null) // if comment this block, old client will be used to call Connect again
            {
                client.Disconnect();
                client.Shutdown();
                client = null;
            }

            if (client == null)
            {
                NetworkCRC.scriptCRCCheck = false;

                client = new NetworkClient();
                client.Configure(connConfig, 800);

                client.RegisterHandler(MsgType.Connect, OnConnect);
                client.RegisterHandler(MsgType.Disconnect, OnDisConnect);
                client.RegisterHandler(MsgType.Error, OnError);
                // ... more handler callback

                client.Connect(serverIP, serverPort); // test ok with 20 times Reconnect
            }
            else
            {
                // will have error after 16 times Reconnect
                client.Connect(serverIP, serverPort);
            }
        }

tungchengvn, where can I place this reconnect function, since my scene changes when player disconnects?

Thanks.

Use it with your NetworkClient in a Singleton or DontDestroyOnLoad GameObject. Make sure that there only one network instance at a time is the rule.