How to have NetworkDiscovery switch back and forth between client and server ?

Hello .

I am currently trying to understand how NetworkDiscovery works, because the documentation on it is a quite sparse. And I am a bit puzzled by the behaviour of that component.

I started using the default NetworkDiscovery component, everything works just fine, my two computers on LAN can broadcast to each other, no problem.

The next thing I tried was having just four buttons, respectively calling NetworkDiscovery’s Initialize(),StartAsServer(), StartAsClient(), StopBroadcast().

I manage to crash the editor by doing the following :

One computer is put on broadcast mode.

On the other computer I followed this sequence : Initialize>StartAsClient (Here I am receiving the broadcast) >StopBroadcast>StartAsClient> and then it crashes.

( I have reported that one )

So what I got from that is that it is needed to re Initialize after stopping, and before trying to re start as client or server. Which I find a bit weird, and am not sure if it’s a bug or the correct behaviour. I would have thought that you only need to initialize once.

The thing I am currently trying to do is using my own class, inheriting from NetworkDiscovery, to attempt to switch between client and server. The end goal is to make some sort of matchmaking over LAN, but I quickly got rid of any NetworkManager related code to focus on attempting to make NetworkDiscovery works reliably.

public class LANMatchmaker : NetworkDiscovery
{
    void Start()
    {
        if (this.Initialize())
        {
            this.StartAsClient();
        }
    }
    public void StartHosting()
    {
        this.StopBroadcast();
        if (!running)
        {
            if (this.Initialize())
            {
                this.StartAsServer();
                print("Starting as an Host on hostId : " + this.hostId);
            }
        }
    }

    public void BackToListening()
    {
        this.StopBroadcast();
        if (!running)
        {
            if (this.Initialize())
            {
                this.StartAsClient();
                print(" Back to listening , hostId : " + this.hostId);
            }
        }
    }
}

Two buttons in the scene, one for calling StartBroadcasting, the other for BackToListening. It properly start as client, receiving messages if the other computer is broadcasting. If I switch to broadcasting, it also works, and the other computer is receiving correctly. However if I switch back to client, it’s no longer receiving anything. The booleans “running”, “isServer” and “isClient” are displaying the correct value for each state. But once switch off it’s initial state as a client, my component inheriting from NetworkDiscovery doesn’t seem to be able to correctly switch back to it. Broadcasting is fine though.

Now, someone helped me “improve” on this problem, by changing my two methods to the following :

public void StartHosting()
{
    this.StopBroadcast();
    if (!running)
    {
        Debug.Log("Start hosting"); //magie! ne pas enlever
        if (this.Initialize())
        {
            this.StartAsServer();
            print("Starting as an Host on hostId : " + this.hostId);
        }
    }    
}

public void BackToListening()
{
    this.StopBroadcast();

    if (!running)
    {
        Debug.Log("Start listening"); //magie! ne pas enlever
        if (this.Initialize())
        {
            this.StartAsClient();
            print(" Back to listening , hostId : " + this.hostId);
        }
    }
}

Spot the differences.

Now this doesn’t actually solve the problem. It just inconsistently allows to switch back and forth between server and client a few times, before the client definitely stops receiving any messages at all.

So I guess this all boils down to the following questions :

What am I doing wrong ?

What is the correct way of using NetworkDiscovery ?

What is exactly going on inside Initialize(), StartAsServer(), StartAsClient(), StopBroadcast() ? Are those methods asynchronous in some way ?

Edit > Corrected the code in the first part, it was badly copy pasted

I have the very identical problem.
My current solution is adding

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

before calling StartAsServer() and StartAsClient().

It’s not a perfect solution but it works at least for me. Hope it helps.
Refs http://forum-old.unity3d.com/threads/broadcast-discovery-already-running-startbroadcast-failed-err-8-startserver-listen-failed.357852/