码迷,mamicode.com
首页 > 编程语言 > 详细

在unity 脚本中获取客户端的IP地址

时间:2015-01-05 00:16:26      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:

需要using System.Net.NetworkInformation;
原理就是获取网卡的信息。

//下面这段代码是我在百度贴吧找来的,经检验是正确的

string userIp = "";
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); ;
foreach (NetworkInterface adapter in adapters)
{
  if (adapter.Supports(NetworkInterfaceComponent.IPv4))
  {
    UnicastIPAddressInformationCollection uniCast = adapter.GetIPProperties().UnicastAddresses;
    if (uniCast.Count > 0)
    {
      foreach (UnicastIPAddressInformation uni in uniCast)
      {
        //得到IPv4的地址。 AddressFamily.InterNetwork指的是IPv4
        if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
        {
          userIp =uni.Address.ToString();
        }
      }
    }
  }
}

技术分享

在unity 脚本中获取客户端的IP地址

标签:

原文地址:http://www.cnblogs.com/hewencong/p/4202441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!