3

everyone!

I have a lot of devices in one local network. How can I get ip addresses of all these devices?

I want to do something like this:

using UnityEngine; using System.Collections; using System.Collections.Generic; public class Tools : MonoBehaviour { //get local ip address public string getIP() { string IP = ""; IP = Network.player.ipAddress; return IP; } //get all ip addresses in local network public List<string> getIPArray() { List<string> listIP = new List<string>(); return listIP; } } 
5
  • What type of devices and why do you want to do that? Is this for network game? For example, to connect to those devices with their IP's for multiplayer? Commented Jun 21, 2016 at 18:27
  • @Programmer I want to create a multiplayer game based on UNet Messages. I want to create a lobby, where clients can connect to a servers without hardcoded Ip address of a servers. I imagine it as a survey of all the IP in the local network and to receive information about what one is server. Sorry for english and explanations. Commented Jun 21, 2016 at 18:45
  • I don't understand the question/problem/objective of the question. If you have devices in a network you can't just request the IP. You need a server to connect the clients to and manage all the game flow. I recommend you reading online tutorials and Unity documentation. Commented Jun 21, 2016 at 19:15
  • @IlyaGutnikov I get what you are doing. Look at my answer. Commented Jun 21, 2016 at 19:38
  • @Cabrra I need something like this: stackoverflow.com/a/19945759/3884619 Commented Jun 21, 2016 at 19:48

1 Answer 1

5

There are two way to do this:

1.UDP

It's a long process but can be done with UDP.

1.First thing to do is to get your local IP Address. If your local ip is 192.168.1.13, you have to take out the last octet which is 13 and replace it with 255.

2.Start a UDP server in all games and listen to IPAddress.Any with any port you want. It is recommended to go with port >=9000. The ports below are usually used by some other Apps. If anything is received from client, send something back to the client to let the client know that you are available.

3.To search for game, create a UDP client and send something to that IP address from #1 which ends with 255, then listen to listen to IPAddress.Any to see if there is response. If response/message is received from any server, store the IP from which the message is received from to an array or List.

4.You can then use those stored IP from #3 to find all available devices on your network.

Here is a working example.

The bad side about this is that it won't work if you have the free version of Unity. There is a restriction that requires you to have Unity Pro in order to use any raw socket API from C#. No longer true with Unity 5 and above. This can now be used in Unity 5 and up without a pro license.


2.NetworkDiscovery

The good news is that Unity made this easier for you in version 5.3 or so. I can't remember what version but make sure to have the latest version. You don't need to use Unity Pro for this to work.

You can use NetworkDiscovery class to do this in new version of Unity. Simply call NetworkDiscovery.StartAsServer() to broadcasting messages to clients. On the client side, you can listen to the Broadcast by calling NetworkDiscovery.StartAsClient(). You can use the OnReceivedBroadcast(string fromAddress, string data); to see which device you received from. It couldn't get easier than this.

Sign up to request clarification or add additional context in comments.

2 Comments

@IlyaGutnikov Np. You can also broadcast to 255.255.255.255. I didn't mention that because sometimes, the OS will block that. The 192.octet.octet.25 is more reliable. Good luck.
Please note that setting the last octet to 255 will not always achieve the broadcast address. The broadcast address is the last address in the IP subnet and isn't assignable to a device. The example above will work with /24 (255.255.255.0) You can calculate the broadcast address with binary math, or you can look up a subnet calculator to calculate it easily. calculator.net/ip-subnet-calculator.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.