Search Unity

NetworkDiscovery always broadcasts "localhost" - why?

Discussion in 'Multiplayer' started by eshan-mathur, Jan 8, 2016.

  1. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    Hi there,

    I wasn't able to get NetworkDiscovery working between two separate devices, but it worked on the same machine. After some digging I realized the broadcast data always contains "localhost" as the address for some reason.

    To fix it, I had to change the given sample of OnReceivedBroadcast to this. The change is using the "fromAddress" parameter instead of the data in the broadcast.

    Why does the broadcast data always contain localhost instead of the actual network address?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using System;
    5.  
    6. public class CustomNetworkDiscovery : NetworkDiscovery {
    7.  
    8.     public override void OnReceivedBroadcast(string fromAddress, string data)
    9.     {
    10.         Debug.Log("Client discovery received broadcast " + data + " from " + fromAddress);
    11.  
    12.         var items = data.Split(':');
    13.         if (items.Length == 3 && items[0] == "NetworkManager")
    14.         {
    15.             if (NetworkManager.singleton != null && NetworkManager.singleton.client == null)
    16.             {
    17.                 NetworkManager.singleton.networkAddress = fromAddress; //items[1];
    18.                 NetworkManager.singleton.networkPort = Convert.ToInt32(items[2]);
    19.                 NetworkManager.singleton.StartClient();
    20.             }
    21.         }
    22.     }
    23.  
    24. }
    25.  
     
    Last edited: Jan 8, 2016
    apha likes this.
  2. faneric

    faneric

    Joined:
    May 15, 2013
    Posts:
    10
    I am not very sure about your code, but the broadcast data cannot be changed while the broadcaster is running.
     
  3. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    Sure, but why does the broadcaster ever broadcast localhost as its address? It seemingly prevents any device other than the broadcasting device from using that data to connect.
     
    apha and Hodgson_SDAS like this.
  4. luuchowl

    luuchowl

    Joined:
    May 17, 2016
    Posts:
    2
    Thanks a lot dude, it really really helped me
     
    eshan-mathur likes this.
  5. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    Network Discovery broadcasts the server's NetworkManager.networkAddress which is set to localhost by default. I haven't been able to find out the server's own ip to use instead of localhost, so I've been using the fromAddress too.
     
  6. Wuschli

    Wuschli

    Joined:
    Mar 4, 2015
    Posts:
    1
    Hi, I've got the same problem with the NetworkDiscovery always broadcasting localhost when "use Network Manager" is selected. This occured on multiple devices and basically breaks the NetworkDiscovery completely. Are there any News on this Issue?
     
  7. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    You'll need to use the fromAddress like the solution above.
     
  8. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    @seanr, your script, right?
     
  9. SanjayBhambhu

    SanjayBhambhu

    Joined:
    Aug 22, 2012
    Posts:
    13
    I think the post with the solution has been deleted from this thread. Does anyone have a solution to this?
     
  10. JesusChrist17

    JesusChrist17

    Joined:
    Sep 17, 2016
    Posts:
    12
    hey bro, when i detect a broadcast using this function, i can see the port and the addresss. do you know how to put a name to, because the people dont like to see a ip ( normal people, not programmers )
     
  11. bsozay

    bsozay

    Joined:
    May 18, 2015
    Posts:
    1
    I am using network discovery and I also can not change the message while broadcast go on. If you want to change message you should shutdown the network and then restart it.

    Code (CSharp):
    1.         /// <summary>
    2.         /// Refreshs broadcast packet data with stop and restart broadcast
    3.         /// </summary>
    4.         public void RefreshBroadcasting(ActionItem action)
    5.         {
    6.             // Update package values
    7.             InitializePacketInfo();
    8.  
    9.            BroadcastDiscovery.InstanceSender.MsgOutBuffer = BroadcastDiscovery.InstanceSender.Packet.ToString().ToBytes();
    10.             BroadcastDiscovery.InstanceSender.BroadcastData = BroadcastDiscovery.InstanceSender.MsgOutBuffer.ToString();
    11.  
    12.             // If broadcast state is running, stop broadcasting
    13.             if (BroadcastDiscovery.InstanceSender.Running)
    14.             {
    15.                 BroadcastDiscovery.InstanceSender.StopBroadcast();
    16.                 BroadcastDiscovery.InstanceReceiver.StopBroadcast();
    17.  
    18.                 BroadcastDiscovery.InstanceSender.ShoutdownBroadcast();
    19.             }
    20.                
    21.             // If not just start the broadcast          
    22.             BroadcastDiscovery.InstanceSender.InitBroadcasting();
    23.             BroadcastDiscovery.InstanceSender.StartAsServer();
    24.  
    25.             BroadcastDiscovery.InstanceReceiver.InitBroadcasting();
    26.             BroadcastDiscovery.InstanceReceiver.StartAsClient();
    27.  
    28.         }
    29.  
     
    Last edited: Mar 15, 2017