Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

unity3d - How to get IP addresses of all devices in local network with Unity UNET in C#?

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...