Search Unity

How to correctly use the NetworkDiscovery broadcastData?

Discussion in 'Scripting' started by mrboembastic, Jan 20, 2016.

  1. mrboembastic

    mrboembastic

    Joined:
    Oct 31, 2014
    Posts:
    14
    Hello,

    I'm using Unity's NetworkDiscovery for clients to find the host on the same network.
    The only problem I'm having is with receiving the broadcastData properly.

    I set the broadcastData like this:

    Code (csharp):
    1.  
    2.     public void NDstart()
    3.     {
    4.         GameObject.Find("broadcaster").GetComponent<NetworkDiscovery>().broadcastData = "the data i want to send";
    5.         GameObject.Find("broadcaster").GetComponent<NetworkDiscovery>().StartAsServer();
    6.     }
    7.  
    8.  
    But I don't know to use the byte array I'm getting returned at the client with this method:

    Code (csharp):
    1.  
    2.  
    3.  foreach (KeyValuePair<string, NetworkBroadcastResult> s in GameObject.Find("broadcaster").GetComponent<NetworkDiscovery>().broadcastsReceived)
    4.             {
    5.                 if (s.Value.serverAddress != null)
    6.                 {
    7.                     ipAndName toadd; //ipAndName is a struct with an ip and name
    8.                     toadd.ip = s.Value.serverAddress.Replace("::ffff:", ""); // serverAddress gives em the proper IP.
    9.  
    10.                     toadd.name =  s.Value.broadcastData.ToString();  
    11.                    
    12.                     hosts.Add(toadd);
    13.                 }
    14.             }
    15.  
    16.  
    This piece return a byte array and I don't know how to convert it to the original string: "the data i want to send"

    Code (csharp):
    1.  
    2. s.Value.broadcastData
    3.  
    Any help would be greatly appreciated.
     
  2. Forrest-Trepte

    Forrest-Trepte

    Joined:
    Jan 22, 2013
    Posts:
    8
    I was surprised that this is a string on one end and a byte array on the other. I'm not sure how this is intended to be used, but the following seems to work for converting it back to a string:
    Code (CSharp):
    1. string broadcastData = System.Text.Encoding.Unicode.GetString(broadcastReceived.Value.broadcastData);
    Disclaimer: Posting is my personal opinion, not that of my employer (Microsoft), provided "AS IS" with no warranties.
     
    DoritoDog likes this.
  3. bhermer

    bhermer

    Joined:
    Jun 24, 2013
    Posts:
    28
    I came across a similar problem. The broadcast data when received by overloading OnReceivedBroadcast comes through with 512 spaces in a byte array. (my language is perhaps wrong here, but basically if you run ?Encoding.ASCII.GetBytes(data);

    You will see a Byte[512], with 0 after the last ASCII chr

    System.Byte[512]
    [0]: 66
    [1]: 65
    [2]: 84
    [3]: 114
    [4]: 97
    [5]: 105
    [6]: 110
    [7]: 105
    [8]: 110
    [9]: 103
    [10]: 0
    [11]: 0



    This is my crude work around, but if anyone knows a better way I would love to know:

    Code (csharp):
    1.  
    2.  
    3.   public override void OnReceivedBroadcast(string fromAddress, string data)
    4.     {
    5.  
    6.         byte[] asciiBytes = Encoding.ASCII.GetBytes(data);
    7.         // look for the 1st zero
    8.         int endOfByte = 0;
    9.         foreach (var item in asciiBytes)
    10.         {
    11.             if (item == 0)
    12.             {
    13.                 break;
    14.             }
    15.             endOfByte++;
    16.         }
    17.         string broadcastData = System.Text.Encoding.ASCII.GetString(asciiBytes,0, endOfByte);
    18.         if (broadcastData == "StringYouExpect")
    19.         {
    20. // action a connection - you will also need some logic around this to stop it being called multiple times
    21.         }
    22.  
    23.  
    24. }
    25.  
    26.