如何使用C#中的Unity UNET获取本地网络中所有设备的IP地址? [英] How to get IP addresses of all devices in local network with Unity UNET in C#?

查看:938
本文介绍了如何使用C#中的Unity UNET获取本地网络中所有设备的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有人!

我在一个局域网中有很多设备.如何获得所有这些设备的IP地址?

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

我想做这样的事情:

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;
    }
}

推荐答案

有两种方法可以做到这一点:

There are two way to do this:

1 .UDP

这是一个漫长的过程,但是可以使用UDP完成.

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

1 .首先要做的是获取您的本地IP地址.如果本地ip是192.168.1.13,则必须取出最后一个八位位组,即13,然后用255替换它.

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 .在所有游戏中启动UDP服务器,并使用所需的任何端口收听IPAddress.Any.建议使用端口> = 9000.下面的端口通常由其他一些Apps使用.如果从客户那里收到任何东西,请将一些东西发送回客户,让客户知道您有空.

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 .要搜索游戏,请创建UDP客户端,然后从#1 255结尾的IP地址发送一些内容,然后收听IPAddress.Any查看是否有响应.如果从任何服务器接收到响应/消息,请将接收消息的IP地址存储到阵列或List.

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 .然后,您可以使用#3 中存储的IP查找网络中所有可用的设备.

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

在这里可以正常工作例子.

不利的一面是,如果您拥有Unity的免费版本,它将无法正常工作.有一个限制要求您必须拥有Unity Pro才能使用C#中的任何原始套接字API. Unity 5 及更高版本不再适用.现在,无需专业许可即可在Unity 5及更高版本中使用它.

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

2.NetworkDiscovery

好消息是Unity在5.3版或更高版本中为您简化了这一过程.我不记得是哪个版本,但请确保拥有最新版本.您无需使用Unity Pro即可工作.

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.

您可以使用 NetworkDiscovery 类来进行

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.

这篇关于如何使用C#中的Unity UNET获取本地网络中所有设备的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆