1.代码示例
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using UnityEngine;
using static UnityEngine.EventSystems.EventTrigger;
public class IpTool : MonoBehaviour
{
void Start()
{
Debug.Log("获取本机IPv4:" + GetLocalIPv4());
Debug.Log("获取本机IPv6:" + GetLocalIPv6());
Debug.Log("获取外网Ip:" + GetExternalNetIp());
Debug.Log("获取指定域名www.baidu.com的Ip信息:");
GetOtherNetIP("www.baidu.com");
Debug.Log("获取设备名称:" + GetDeviceName());
Debug.Log("获取设备模型:" + GetDeviceModel());
Debug.Log("获取设备唯一标识码:" + GetDeviceUniqueIdentifier());
}
// 获取本机IPv4
public string GetLocalIPv4()
{
string ipAddress = "";
try
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
//Debug.Log("GetHostName:" + Dns.GetHostName());
//返回IP列表
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
break;
}
}
//获取主机别名列表
for (int i = 0; i < host.Aliases.Length; i++)
{
//Debug.Log("主机别名:" + host.Aliases[i]);
}
//获取DNS名称
//Debug.Log("DNS服务器名称" + host.HostName);
}
catch (Exception e)
{
Debug.LogError("IP 获取失败" +e.Message);
}
return ipAddress;
}
// 获取本机IPv6
public string GetLocalIPv6()
{
string ipAddress = "";
try
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetworkV6)
{
ipAddress = ip.ToString();
break;
}
}
}
catch (Exception e)
{
Debug.LogError("IP 获取失败" + e.Message);
}
return ipAddress;
}
//获取指定域名的Ip信息
public string GetOtherNetIP(string ip)
{
string ipAddress = "";
try
{
//获取域名的地址
IPHostEntry entry = Dns.GetHostEntry(ip);
for (int i = 0; i < entry.AddressList.Length; i++)
{
Debug.Log("IP地址:" + entry.AddressList[i]);
}
for (int i = 0; i < entry.Aliases.Length; i++)
{
Debug.Log("主机别名:" + entry.Aliases[i]);
}
Debug.Log("DNS服务器名称" + entry.HostName);
}
catch (Exception e)
{
Debug.LogError("IP 获取失败" + e.Message);
}
return ipAddress;
}
//获取外网Ip
public string GetExternalNetIp()
{
string IP = string.Empty;
try
{
//从网址中获取本机ip数据
WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.Default;
IP = client.DownloadString("http://checkip.amazonaws.com/");
client.Dispose();
IP = Regex.Replace(IP, @"[\r\n]", "");
}
catch (Exception e)
{
Debug.LogError("获取外网Ip" + e.Message);
}
return IP;
}
//获取设备名称
public string GetDeviceName()
{
return SystemInfo.deviceName;
}
//获取设备模型
public string GetDeviceModel()
{
return SystemInfo.deviceModel;
}
// 获取设备唯一标识码
public string GetDeviceUniqueIdentifier()
{
return SystemInfo.deviceUniqueIdentifier;
}
}
2.运行结果

这里是井队,天高任鸟飞,海阔凭鱼跃,点个关注不迷路,我们下期再见。
4534

被折叠的 条评论
为什么被折叠?



