3

I want to implement a service discovery module to fetch the IP address of a service broadcasting device, similar to the bonjour browser. Is it possible to implement it in Unity3D(Android/iOS). Can anyone guide me on how can I achieve it. A small example would be really helpful. Thanks

Edit 1: An IOT device is broadcasting a DNS service, and I want to fetch this service in the Unity application to find the IP address of the IOT device. I don't have any access to the IOT device software or source code.

Programmer
  • 121,791
  • 22
  • 236
  • 328

3 Answers3

2

You don't have to do this from scratch. There is a new Unity API called NetworkDiscovery which is designed to simplify this.

To find which IP Address to connect to, call NetworkDiscovery.StartAsServer() on the server side once in the Start() function.

On the client side, call NetworkDiscovery.StartAsClient() then implement the OnReceivedBroadcast(string fromAddress, string data); function. When server is found, the OnReceivedBroadcast(string fromAddress, string data);function will be called and you can then use the returned fromAddress value to connect to your server.

When client connects, you can stop broadcasting by calling NetworkDiscovery.StopBroadcast().

If you are just doing this for learning purposes, you just need to Broadcast with UDP protocol. For more information, you can read this and this.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    I have tried the NetworkDiscovery class, it works fine. But here the broadcasting 3rd party device is not my unity client. So this service is not visible in the OnReceivedBroadcast() function. I can see the service broadcast by the 3rd party device in Bonjour browser app on the same device, whereas I can not see the UDP broadcast from Unity client(editor/device) on the bonjour browser app. – TUSHAR KHAVALE Jan 26 '17 at 04:53
  • 1
    I also covered that in the second part of my question. *If you are just doing this for learning purposes, you just need to Broadcast with UDP protocol. For more information, you can read this and this.* Please check those two links on how to do it. The first link explains how it works. The second links shows how to broadcast on a network with UDP. That's what you want. – Programmer Jan 26 '17 at 04:59
  • I am not able to achieve it using NetworkDiscovery or UDPClient. It only shows the broadcast that I do using NetworkDiscovery - Start broadcasting and UDP broadcast. Where as in the Bonjour browser app I also see the printers connected to the network. I have successfully achived it in native android using ServiceListener class. Can you pls guide me further. Thanks – TUSHAR KHAVALE Jan 26 '17 at 12:17
  • My situation: The box that identifies itself with a .local is NOT a computer, and therefore I cannot program it. (It's a DSP with a proprietary application to control it.) I'm hoping to connect to it based on it's .local name. – Ubuntourist Nov 03 '17 at 21:08
  • @Ubuntourist It doesn't matter if it is a computer or not. as long as it goes through the network/wifi, it should work. Use Wireshark to see which protocol it is using to broadcast itself. This is usually UDP but verify with Wireshark then see what exactly it is sending. If UDP, read the second part of my answer which has a link to examplr of UDPClient. That can be used to detect that device. Note that no one will replicate a priopetry api for you for **free**. It is simply time consuming. You have to do it yourself by following my direction **or** for pay someone to do it for you. – Programmer Nov 03 '17 at 21:44
0

Here is an iOS plugin that does DNS Service discovery: https://docs.unity3d.com/Manual/PluginsForIOS.html

0

The new solution native to unity is the Netcode Network Discovery

It's not actually a supported solution anymore. There is just sample code.

Edit: They removed that discovery code. Which makes sense because it didn't seem to be functional. I still used UNet, but supposedly there's reference of a discovery system here

CrippledTable
  • 784
  • 5
  • 20