'Broadcast discovery already running', 'StartBroadcast failed err: 8' & 'StartServer listen failed'

Excuse me for the gigantic title! :wink:

I’m continuously hitting the same wall with the NetworkManager in combination with NetworkDiscovery and I can’t figure out how to get around this. I hypothesise that this is a bug in UNET, unless someone here knows what I’m doing wrong. Other posts on the forum talking about the ‘StartServer listen failed’ error suggest that this issue has been fixed, but, well, I’m still experiencing it in 5.2.1f1.

Example project attached
I’ve attached an example test project which has a nice interface to quickly create / connect and disconnect with a LAN game. But mind you that I’ve found that the speed at which you connect / disconnect seems to be irrelevant.

The problem
After two or three connects / disconnects creating a local networked game, the game will stop working completely due to these three errors:

Broadcast discovery has been already running. Stop discovery first before repeat this call
UnityEngine.Networking.NetworkDiscovery:StartAsServer()
and
NetworkDiscovery StartBroadcast failed err: 8
UnityEngine.Networking.NetworkDiscovery:StartAsServer()
and
StartServer listen failed.
UnityEngine.Networking.NetworkManager:StartHost()

Even using the ‘HARD RESET’ button in my example (which destroys the NetworkManager and NetworkDiscovery) doesn’t fix the problem that I’m stuck in this state. As a matter of fact - nothing in-game that I’ve tried gets me out of this state. All I can do is quit the game and start over.

Considering connections sometimes fail on mobile networks, being able to restart / rejoin a game even when the connection failed previously is a MUST. Any help on this is greatly appreciated!!

2315761–156087–ListenError.zip (52.6 KB)

I had pretty much an identical problem. I found that the following made it work if called just before the StartAsServer and StartAsClient calls. Just make sure any existing network sessions and broadcasts have been stopped before doing it.

NetworkTransport.Shutdown();
NetworkTransport.Init();

I’m not sure how expensive it is to shutdown and init the whole network transport but until a better fix is put in place this could be a workaround for now :slight_smile:

1 Like

Hello,
had the same issue… maybe a Unity bug?
well i fixed it on the client side with these:

hope it helps someone…
M.

This bug still appears in Unity 5.5.1f1.

The client shutdown workaround seems to work in Unity editor, but not when running as build (Windows x64),
The only way I could fix it was by shutting down the whole NetworkTransport.

namespace BIMeVR
{
    public class NetDiscovery : NetworkDiscovery
    {
        public static NetDiscovery singleton;

        void Awake()
        {
            if (singleton != null && singleton != this) {
                Debug.Log("Multiple instances of " + typeof(NetDiscovery).Name + " detected. Disabling this one.", gameObject);
                this.enabled = false;
            } else {
                singleton = this;
            }
        }

        public new void StopBroadcast()
        {
            if (running)
                base.StopBroadcast();
            try {
                if (NetworkTransport.IsBroadcastDiscoveryRunning()) {
                    Debug.LogWarning("Broadcast discovery still running despite NetDiscovery having stopped - stopping again.", gameObject);
                    NetworkTransport.StopBroadcastDiscovery();
                }
            } catch (NullReferenceException) {
                // Ignore when NetworkTransport is not initialized
            }
        }
    }
}
namespace BIMeVR
{
    [RequireComponent(typeof(NetDiscovery))]
    public class NetManager : NetworkManager
    {
        public override void OnStopClient()
        {
            ClientShutdownAndReset();
        }

        // Workaround for broadcast not working after host has been started/stopped multiple times
        // https://issuetracker.unity3d.com/issues/broadcast-discovery-not-stopping-after-multiple-calls
        // https://forum.unity3d.com/threads/broadcast-discovery-already-running-startbroadcast-failed-err-8-startserver-listen-failed.357852/
        private void ClientShutdownAndReset()
        {
            NetDiscovery.singleton.StopBroadcast();
            if (client != null) {
                client.Disconnect();
                client.Shutdown();
                client = null;
            }
            if (isNetworkActive) {
                Debug.LogError("Network is still active even though the client has reportedly stopped.", gameObject);
                return;
            }
            Debug.Log("Resetting network transport layer");
            NetworkTransport.Shutdown();
            NetworkTransport.Init();
        }
    }
}

StopBroadcast() is not synchronous function. It will send command to stop broadcast only, and it will require time for system to execute this command.
Move NetworkTransport.IsBroadcastDiscoveryRunning() to Update() function and wait before it will return false

hope it helps

1 Like

Such kind of information should be provided within the docs.

This would save people from running into issues and finding themselves in the need to study the network discovery source code to find out what’s going on. So far, there already has been a lot of confusion about proper usage of Network Discovery, for example also see this thread.

1 Like

Thanks for your reply and the added clarification.
It does indeed work if I wait for broadcasting to shutdown cleanly.

I have done some more testing - here is some additional information.

After NetworkManager.StartClient(), it works as expected:
StopBroadcast() works immediately - NetworkTransport.IsBroadcastDiscoveryRunning() returns false. I don’t need to wait, reset NetworkTransport or do anything besides calling StopClient() right after.

After NetworkManager.StartHost(), things start to break in builds (but not always in the editor)
After I have used StartHost(), the StopBroadcast() command is asynchronous, like you said.
If I call StopHost() before broadcasting has stopped completely, eventually NetworkDiscovery will stop working and StartHost() will fail to bind on the port - this happens consistently in Windows x64 builds on the third Start/Stop cycle, but only occasionally in the Unity Editor.
As I said, the third StartHost() command will fail to bind on the same port, indicating that it is still in use by the transport API despite having called NetworkManager.singleton.StopHost().

The clean fix:
I have deleted the previous workaround and instead implemented new static Stop-methods in NetManager that defer the actual command until broadcasting has completely stopped.
Simplified example code below.

using System;
using UnityEngine;
using UnityEngine.Networking;

namespace BIMeVR
{
    public class NetDiscovery : NetworkDiscovery
    {
        public static NetDiscovery singleton;

        public static bool stopConfirmed = false;

        void Awake()
        {
            if (singleton != null && singleton != this)
                this.enabled = false;
            else
                singleton = this;
        }

        public new void StopBroadcast()
        {
            if (running)
                base.StopBroadcast();
            ConfirmStopped();
        }

        void LateUpdate()
        {
            if (!running && !stopConfirmed)
                ConfirmStopped();
        }

        void ConfirmStopped()
        {
            try {
                stopConfirmed = !NetworkTransport.IsBroadcastDiscoveryRunning();
            } catch (Exception) {
                stopConfirmed = true;
            }
        }
    }
}
using System;
using UnityEngine;
using UnityEngine.Networking;

namespace BIMeVR
{
    [RequireComponent(typeof(NetDiscovery))]
    public class NetManager : NetworkManager
    {
        public static void StopClientAndBroadcast()
        {
            NetDiscovery.singleton.StopBroadcast();
            onBroadcastStopped += singleton.StopClient;
        }

        public static void StopServerAndBroadcast()
        {
            NetDiscovery.singleton.StopBroadcast();
            onBroadcastStopped += singleton.StopServer;
        }

        public static void StopHostAndBroadcast()
        {
            NetDiscovery.singleton.StopBroadcast();
            onBroadcastStopped += singleton.StopHost;
        }

        private static event Action onBroadcastStopped;

        void Update()
        {
            if (onBroadcastStopped != null) {
                if (!NetDiscovery.singleton.running && NetDiscovery.singleton.stopConfirmed) {
                    onBroadcastStopped.Invoke();
                    onBroadcastStopped = null;
                } else {
                    if (LogFilter.logDebug)
                        Debug.Log("Waiting for broadcasting to stop completely", gameObject);
                    NetDiscovery.singleton.StopBroadcast();
                }
            }
        }
    }
}
1 Like

@helgewt thanks a lot for your feedback. Actually it looks like a small bug, could you think a bit should it be fixed, and if yes could you open the bug about this. I understood, that you fix is perfect, but the library possible should send some notification about this? Or documentation should be added? Please, make decision :slight_smile:

Came by this post which has helped me work around the network discovery issue. I’ve submitted a bug: Unity Issue Tracker - Network Discovery always fails if Unity started with no Network Connection. Please help boost the priority of this bug as it’s critical to our project and this work-around seems suspicious and fragile.

1 Like