Search Unity

Android Build - Can't Get IP Address

Discussion in 'Android' started by ryanchen26, Mar 11, 2020.

  1. ryanchen26

    ryanchen26

    Joined:
    Jul 26, 2014
    Posts:
    6
    All I want to do is get the IPv4 address of my Android Unity build displayed on the android phone. The code listed here by "Programmer" works on iOS devices, windows, and mac laptops: https://stackoverflow.com/questions...ess-of-device-in-unity-2018/51977237#51977237 - it is not working for me on the Android phones (running Pie 9 or Nougat 7). I've tried multiple wi-fi networks, I've tried different Unity builds (2019.2.2, 2019.2.6, 2019.3.4, 2020 alpha, tried setting internet access to required, nothing. What is it about Android that is causing a problem? Keep in mind that my android project does show an ip address when mobile data is turned on, but only shows 127.0.0.1 when I'm trying to do a simple local area wi-fi communication thing. Again, it works fine on the iOS phone on the same wi-fi network - I see the ip address phone receives data using NetMQ from a laptop on the same network. It's got to be some setting in the phone right?
     
  2. ryanchen26

    ryanchen26

    Joined:
    Jul 26, 2014
    Posts:
    6
  3. ryanchen26

    ryanchen26

    Joined:
    Jul 26, 2014
    Posts:
    6
    For what it's worth, I tried adding the network_security_config.xml idea with the following in Plugins>Android>res>xml :
    Code (Boo):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <network-security-config>
    3.  
    4.     <base-config
    5.         cleartextTrafficPermitted="true">
    6.  
    7.         <trust-anchors>
    8.  
    9.             <certificates
    10.                 src="system" />
    11.         </trust-anchors>
    12.     </base-config>
    13. </network-security-config>
    And I added the much talked about android:usesCleartextTraffic = "true" in the <applications> tag of the AndroidManifest.xml. Both options failed in my case. See what I'm trying to do, like many others, is just have a TCP socket to a local network ipaddress and port to send and receive data for a game using NetMQ. Simple. Works fine with iOS.
     
  4. e140games

    e140games

    Joined:
    Aug 2, 2012
    Posts:
    4
    This code returns the IP address of the device. But 127.0.0.1 is returned last in the address list. I added conditions that exclude this address and it worked for me.

    Code (CSharp):
    1. public class IPManager
    2. {
    3.     public static string GetIP(ADDRESSFAM Addfam)
    4.     {
    5.         //Return null if ADDRESSFAM is Ipv6 but Os does not support it
    6.         if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
    7.         {
    8.             return null;
    9.         }
    10.  
    11.         string output = "0.0.0.0";
    12.  
    13.         foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
    14.         {
    15. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
    16.             NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
    17.             NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
    18.  
    19.             if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
    20. #endif
    21.             {
    22.                 foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
    23.                 {
    24.                     //IPv4
    25.                     if (Addfam == ADDRESSFAM.IPv4)
    26.                     {
    27.                         if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
    28.                         {
    29.                             if (ip.Address.ToString() != "127.0.0.1")
    30.                                 output = ip.Address.ToString();
    31.                         }
    32.                     }
    33.  
    34.                     //IPv6
    35.                     else if (Addfam == ADDRESSFAM.IPv6)
    36.                     {
    37.                         if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
    38.                         {
    39.                             if (ip.Address.ToString() != "127.0.0.1")
    40.                                 output = ip.Address.ToString();
    41.                         }
    42.                     }
    43.                 }
    44.             }
    45.         }
    46.         return output;
    47.     }
    48. }
    49.  
    50. public enum ADDRESSFAM
    51. {
    52.     IPv4, IPv6
    53. }
    54.  
     
    GuirieSanchez and achimmihca like this.
  5. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    How to decide whether to ask for IPv4 or IPv6?
    I'm calling this function with:
    _localIpLabel.text = IPManager.GetIP(ADDRESSFAM.IPv4);

    But it seems that I have to choose between IPv4 or IPv6
     
  6. axelstewart

    axelstewart

    Joined:
    May 17, 2018
    Posts:
    16
    Hello - this is what I have been using - it works on Windows and Android:
    Code (CSharp):
    1.     // "Mr. Wang from Next Door" on StackOverflow: https://stackoverflow.com/questions/6803073/get-local-ip-address
    2.     public static IPAddress GetLocalIP()
    3.     {
    4.         using(Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
    5.         {
    6.             try
    7.             {
    8.                 socket.Connect("8.8.8.8", 65530);
    9.                 IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
    10.                 return endPoint.Address;
    11.             }
    12.             catch (SocketException)
    13.             {
    14.                 return IPAddress.Loopback;
    15.     }   }   }