Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Network Discovery not working on LAN

Discussion in 'Multiplayer' started by tgaldi, Oct 18, 2017.

  1. tgaldi

    tgaldi

    Joined:
    Oct 28, 2015
    Posts:
    102
    In my project I have two prefabs - NetClient and NetServer.

    Code (CSharp):
    1.     [AddComponentMenu( "Networking/NetServer" )]
    2.     public class NetServer : NetworkDiscovery
    3.     {
    4.         void Start()
    5.         {
    6.             Application.runInBackground = true;
    7.             StartServer();
    8.         }
    9.  
    10.         //Call to create a server
    11.         public void StartServer()
    12.         {
    13.             int serverPort = CreateServer();
    14.             if( serverPort != -1 )
    15.             {
    16.                 Debug.Log( "Server created on port : " + serverPort );
    17.                 broadcastData = serverPort.ToString();
    18.                 Initialize();
    19.                 StartAsServer();
    20.             }
    21.             else
    22.             {
    23.                 Debug.Log( "Failed to create Server" );
    24.             }
    25.         }
    26.  
    27.         int minPort = 10000;
    28.         int maxPort = 10010;
    29.         int defaultPort = 10000;
    30.  
    31.         //Creates a server then returns the port the server is created with. Returns -1 if server is not created
    32.         private int CreateServer()
    33.         {
    34.             int serverPort = -1;
    35.             //Connect to default port
    36.             bool serverCreated = NetworkServer.Listen( defaultPort );
    37.             if( serverCreated )
    38.             {
    39.                 serverPort = defaultPort;
    40.                 Debug.Log( "Server Created with deafault port" );
    41.             }
    42.             else
    43.             {
    44.                 Debug.Log( "Failed to create with the default port" );
    45.                 //Try to create server with other port from min to max except the default port which we trid already
    46.                 for( int tempPort = minPort; tempPort <= maxPort; tempPort++ )
    47.                 {
    48.                     //Skip the default port since we have already tried it
    49.                     if( tempPort != defaultPort )
    50.                     {
    51.                         //Exit loop if successfully create a server
    52.                         if( NetworkServer.Listen( tempPort ) )
    53.                         {
    54.                             serverPort = tempPort;
    55.                             break;
    56.                         }
    57.  
    58.                         //If this is the max port and server is not still created, show, failed to create server error
    59.                         if( tempPort == maxPort )
    60.                         {
    61.                             Debug.LogError( "Failed to create server" );
    62.                         }
    63.                     }
    64.                 }
    65.             }
    66.             return serverPort;
    67.         }
    68.     }

    Code (CSharp):
    1.     [AddComponentMenu("Networking/NetClient")]
    2.     public class NetClient : NetworkDiscovery
    3.     {
    4.         void Start()
    5.         {
    6.             StartClient();
    7.         }
    8.  
    9.         public void StartClient()
    10.         {
    11.             Initialize();
    12.             StartAsClient();
    13.         }
    14.  
    15.         public override void OnReceivedBroadcast( string fromAddress, string data )
    16.         {
    17.             Debug.Log( "Server Found: " + fromAddress + "\n" + "Data: " + data );
    18.         }
    19.     }

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

    tgaldi

    Joined:
    Oct 28, 2015
    Posts:
    102
    I've implemented Unity's network discovery class in my project to step through the code:

    https://bitbucket.org/Unity-Technol...Simple.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()

    Code (CSharp):
    1. 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!
     
    Last edited: Oct 19, 2017
  3. tgaldi

    tgaldi

    Joined:
    Oct 28, 2015
    Posts:
    102
    It was a simple subnet mismatch.
     
  4. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    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 :O

    thanks in advance :O
     
  5. tgaldi

    tgaldi

    Joined:
    Oct 28, 2015
    Posts:
    102
    In Windows, make sure your subnet is setup for broadcast, I.E. 255.255.255.255
     
  6. albertkong

    albertkong

    Joined:
    Oct 7, 2018
    Posts:
    1
    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.
     
    kaksaveksaka likes this.
  7. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    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.
     
  8. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476