Search Unity

maximum hosts cannot exceed {16}

Discussion in 'Multiplayer' started by Nokhal, Oct 6, 2015.

  1. Nokhal

    Nokhal

    Joined:
    Jun 4, 2014
    Posts:
    1
    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 :
    http://issuetracker.unity3d.com/iss...sconnect-does-not-remove-transport-layer-host

    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 ?
     
  2. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Did you actually file a bug report? Just curious.
     
  3. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    this is a known bug. will be fixed in a 5.2 patch release
     
  4. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    This is still not fixed in 5.2.1p4. Any idea when the fix will be released?
     
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    @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.
     
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    This bug was fixed and verified in 5.2.1p3
     
  7. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    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.
     
  8. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Here's a script that produces the error:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Networking.NetworkSystem;
    4. using System.Collections;
    5.  
    6. public class Connector : MonoBehaviour {
    7.  
    8.     int connectionAttemptCount;
    9.     NetworkClient client;
    10.     bool errorHappened;
    11.  
    12.     void Update () {
    13.  
    14.         if (errorHappened) {
    15.             errorHappened = false;
    16.             StopClient ();
    17.         }
    18.  
    19.         if (client == null )
    20.             StartClient ();
    21.     }
    22.  
    23.     void StartClient()
    24.     {
    25.         client = new NetworkClient ();
    26.         client.RegisterHandler (MsgType.Connect, OnConnected);
    27.         client.RegisterHandler (MsgType.Disconnect, OnDisconnected);
    28.         client.RegisterHandler (MsgType.Error, OnError);
    29.  
    30.         // short timeouts
    31.         var config = new ConnectionConfig();
    32.         config.ConnectTimeout = 100;
    33.         client.Configure (config, 1);
    34.         client.Connect ("2.2.2.2", 1234); // some non-connectable server to create a connection timeout
    35.  
    36.         connectionAttemptCount++;
    37.         Debug.LogFormat ("New connection attempt {0}...",connectionAttemptCount);
    38.     }
    39.  
    40.     void StopClient()
    41.     {
    42.         print ("Stopping client: hostid=" +client.connection.hostId);
    43.  
    44.         // can't do it here, would spam this error: host id {0} has been already deleted
    45.         //NetworkTransport.RemoveHost (client.connection.hostId);
    46.         //client.Disconnect ();
    47.  
    48.         client.UnregisterHandler (MsgType.Connect);
    49.         client.UnregisterHandler (MsgType.Disconnect);
    50.         client.UnregisterHandler (MsgType.Error);
    51.         client.Shutdown ();
    52.         client = null;
    53.     }
    54.  
    55.     void OnConnected(NetworkMessage netMsg)
    56.     {
    57.         print ("connected");
    58.     }
    59.  
    60.     void OnDisconnected(NetworkMessage netMsg)
    61.     {
    62.         print ("disconnected");
    63.     }
    64.  
    65.     void OnError(NetworkMessage netMsg)
    66.     {
    67.         // we just assume it's a timeout error
    68.         errorHappened = true;
    69.     }
    70. }
    71.  
    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.
     
  9. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Got a GIF where it shows how the error messages are generated? Just curious how it looks.
     
  10. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
  11. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Networking.NetworkSystem;
    4. using System.Collections;
    5. public class Connector : MonoBehaviour {
    6.     int connectionAttemptCount;
    7.     NetworkClient client;
    8.     bool errorHappened;
    9.     void Start () {
    10.         StartClient ();
    11.     }
    12.     void StartClient()
    13.     {
    14.         client = new NetworkClient ();
    15.         client.RegisterHandler (MsgType.Connect, OnConnected);
    16.         client.RegisterHandler (MsgType.Disconnect, OnDisconnected);
    17.         client.RegisterHandler (MsgType.Error, OnError);
    18.         // short timeouts
    19.         var config = new ConnectionConfig();
    20.         config.ConnectTimeout = 100;
    21.         client.Configure (config, 1);
    22.         client.Connect ("2.2.2.2", 1234); // some non-connectable server to create a connection timeout
    23.         Debug.LogFormat ("New connection attempt {0}...",connectionAttemptCount);
    24.     }
    25.     void StopClient()
    26.     {
    27.         print ("Stopping client: hostid=" + (connectionAttemptCount));
    28.         // can't do it here, would spam this error: host id {0} has been already deleted
    29.         NetworkTransport.RemoveHost (0);
    30.         //client.Disconnect ();
    31.         client.UnregisterHandler (MsgType.Connect);
    32.         client.UnregisterHandler (MsgType.Disconnect);
    33.         client.UnregisterHandler (MsgType.Error);
    34.         client.Shutdown ();
    35.         client = null;
    36.         connectionAttemptCount += 1;
    37.     }
    38.     void OnConnected(NetworkMessage netMsg)
    39.     {
    40.         print ("connected");
    41.     }
    42.  
    43.     private void OnDisconnected(NetworkMessage netMsg)
    44.     {
    45.         print("disconnected");
    46.         StartCoroutine(Reset());
    47.     }
    48.  
    49.     IEnumerator Reset()
    50.     {
    51.         StopClient();
    52.         yield return new WaitForSeconds(1);
    53.         StartClient();
    54.     }
    55.     void OnError(NetworkMessage netMsg)
    56.     {
    57.         // we just assume it's a timeout error
    58.         errorHappened = true;
    59.     }
    60. }
    61.  
     
    Oxygeniium likes this.
  12. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Thanks for the workaround @seanr , works great so far!
     
  13. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Hello,
    the bug is still there... Unity 5.3.1p1
    but
    Code (CSharp):
    1. public override void OnStopHost() {
    2.         myNetworkDiscovery.StopBroadcast();
    3.         NetworkTransport.RemoveHost (0);
    4. }
    made it work!
    the key is first stop broadcasting then remove the host otherwise you get some error msg...
    thx
    M.
     
    Last edited: Dec 29, 2015
  14. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    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?
     
  15. OlegGoncharenko

    OlegGoncharenko

    Joined:
    Nov 25, 2013
    Posts:
    3
    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?
    Code (CSharp):
    1. private void ClearHostIDs()
    2.     {
    3.         if(hostIDs.Count != 0)
    4.         {
    5.             var clearList = new List<int>();
    6.             foreach(int id in hostIDs)
    7.             {
    8.                 if(client.connection != null)
    9.                 {
    10.                     if(id != client.connection.hostId)
    11.                     {
    12.                         NetworkTransport.RemoveHost(id);
    13.                         clearList.Add(id);
    14.                     }
    15.                 } else
    16.                 {
    17.                     NetworkTransport.RemoveHost(id);
    18.                     clearList.Add(id);
    19.                 }
    20.             }
    21.  
    22.             foreach(int id in clearList)
    23.             {
    24.                 hostIDs.Remove(id);
    25.             }
    26.         }
    27.     }
    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:)