Network Discovery not working on LAN

In my project I have two prefabs - NetClient and NetServer.

NetServer

    [AddComponentMenu( "Networking/NetServer" )]
    public class NetServer : NetworkDiscovery
    {
        void Start()
        {
            Application.runInBackground = true;
            StartServer();
        }

        //Call to create a server
        public void StartServer()
        {
            int serverPort = CreateServer();
            if( serverPort != -1 )
            {
                Debug.Log( "Server created on port : " + serverPort );
                broadcastData = serverPort.ToString();
                Initialize();
                StartAsServer();
            }
            else
            {
                Debug.Log( "Failed to create Server" );
            }
        }

        int minPort = 10000;
        int maxPort = 10010;
        int defaultPort = 10000;

        //Creates a server then returns the port the server is created with. Returns -1 if server is not created
        private int CreateServer()
        {
            int serverPort = -1;
            //Connect to default port
            bool serverCreated = NetworkServer.Listen( defaultPort );
            if( serverCreated )
            {
                serverPort = defaultPort;
                Debug.Log( "Server Created with deafault port" );
            }
            else
            {
                Debug.Log( "Failed to create with the default port" );
                //Try to create server with other port from min to max except the default port which we trid already
                for( int tempPort = minPort; tempPort <= maxPort; tempPort++ )
                {
                    //Skip the default port since we have already tried it
                    if( tempPort != defaultPort )
                    {
                        //Exit loop if successfully create a server
                        if( NetworkServer.Listen( tempPort ) )
                        {
                            serverPort = tempPort;
                            break;
                        }

                        //If this is the max port and server is not still created, show, failed to create server error
                        if( tempPort == maxPort )
                        {
                            Debug.LogError( "Failed to create server" );
                        }
                    }
                }
            }
            return serverPort;
        }
    }

NetClient

    [AddComponentMenu("Networking/NetClient")]
    public class NetClient : NetworkDiscovery
    {
        void Start()
        {
            StartClient();
        }

        public void StartClient()
        {
            Initialize();
            StartAsClient();
        }

        public override void OnReceivedBroadcast( string fromAddress, string data )
        {
            Debug.Log( "Server Found: " + fromAddress + "\n" + "Data: " + data );
        }
    }

I have a simple UI that allows the user to select whether they are the server or client, which then adds the correct prefab to the scene.

Running both on a machine works, but when running on two or more machines, the client never receives any broadcasts. Wireshark does show that the client machine receives packets.

I tried running the Broadcast example project and the client also did not receive broadcasts. I’m running unity 2017.1.0f3.

The computers are connected using ethernet cables and a Netgear router, all running Windows 10.

Is this a known problem?

I’ve implemented Unity’s network discovery class in my project to step through the code:

https://bitbucket.org/Unity-Technologies/networking/src/e998e3faafcbfb39f39c57e9bf17f25a5ed1d1fd/Runtime/NetworkServerSimple.cs?at=5.4&fileviewer=file-view-default

which really doesn’t buy me much but - my client never receives a network event in the Update()

networkEvent = NetworkTransport.ReceiveFromHost( m_HostId, out connectionId, out channelId,  m_MsgInBuffer, k_MaxBroadcastMsgSize, out receivedSize, out error );

No error, just networkEvent is NetworkEventType.Nothing

When running a client and server on the same machine everything works fine.

Wireshark shows the broadcast coming through:

Src: 10.1.10.125, Dst: 10.1.255.255
Src Port: 49220, Dst Port: 47777
Length: 67

Client is 10.1.10.13
Server is 10.1.10.125

EDIT:

I noticed that in the NetworkDiscovery code, in StartAsServer() when calling NetworkTransport.Addhost() - the hostId returned is 1.

In StartAsClient() AddHost returns hostId 0.

In the Update(), ReceiveFromHost() takes the hostId.

I tried just setting it to 1 on the client and get the error: “host id out of bound id {1} max id should be greater 0 and less than {1}”

Not sure if the id’s need to match or this is fine?

I also tried building a project in an older version of Unity (5.4) and it still did not work.

I’m open to any suggestions. Also if you have networking discovery working on multiple computers and are using Windows 10 let me know. If you are using a different version than 5.4/2017.1 let me know. If you had this issue and were able to fix it let me know!

It was a simple subnet mismatch.

hi @tgaldi !

If you happen to be there … how specifically did you resolve this?

Do you mean a subnet setting “in Windows” or something in Unet?

I’d really appreciate any tips on this bizarre problem :open_mouth:

thanks in advance :open_mouth:

In Windows, make sure your subnet is setup for broadcast, I.E. 255.255.255.255

I’ve been trying to get Network discovery to work but I can’t. I’m using hte default GUI and im trying to connect my android phone and the editor in Unity. I can host and then broadcast but the client never seems to receive anything? my phone and computer are connect to same Wi-Fi. Does anyone here have any ideas? I’m noob.

1 Like

Hi @albertkong

Your PC is a windows or Mac?

a HUGE pain in the ass is, you must totally turn off the firewall on your windows PC.

Hi @albertkong ,

Here is a HUGE post about some networking woes,

Note that “incidentally” at the bottom it explains exactly how to do the client and server side of discovery.

Hope it helps!