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

需要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();
        }
      }
    }
  }
}

转载于:https://www.cnblogs.com/hewencong/p/4202441.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部