I’ve been using this code to get the local IP of the device:
public static string[] GetLocalIPv4(NetworkInterfaceType interfaceType = NetworkInterfaceType.Ethernet)
{
var ipAddrList = new List<string>();
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (netInterface.NetworkInterfaceType == interfaceType && netInterface.OperationalStatus == OperationalStatus.Up)
{
foreach (var ip in netInterface.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
ipAddrList.Add(ip.Address.ToString());
}
}
}
return ipAddrList.ToArray();
}
And it works fine and dandy for PC, Mac, and iOS (both for hosting and for joining as a client in each device).
Now, when I built it for Android, I get no info for the local IP:
Also, I’m not able to join as a client to any available host’s local IP, which makes me think it might have something to do with networking permissions, but maybe not, I have no idea.
I tried this method showcased in this UnityAnswers:
which is adding this line of code (android:usesCleartextTraffic=“true”) to the app manifest
to
But to no avail, it doesn’t help. Also, after building it, the manifest apparently gets reset and that line disappears.
I also tried to find any permissions for the app regarding the network, but I don’t seem to find any (although I think it has to have permissions on.
Any help would be highly appreciate it.
Maybe the device is only using an IPv6 address? Try getting that instead.
Also add some logging in the foreach loop or step through the loop with the debugger to see what kind of interfaces are available and what their IPs are.
1 Like
Hi, thanks for replying. I’m coming back home tonight so I’ll give that a shot. Is there a way to make sure to detect whatever local IP they have? I feel like I could have IPv6 and IPv4, and then I may have some problems in the future if there’s a new one that I don’t cover in code. Also, I tried on a second iPhone, and that iPhone also didn’t ask for permissions to join the local network, which may be due to what you said (IPv6):
On the first iPhone and the iPad this message was triggered and therefore I was able to join the local network. From that on, the app appeared on the local network section of the settings so one would be able to deactivate it or activate as they please.
Anyway, I’ll try to put both one after another. If you can, let me know if this code is correct:
public static string[] GetLocalIPv6(NetworkInterfaceType interfaceType = NetworkInterfaceType.Ethernet)
{
var ipAddrList = new List<string>();
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (netInterface.NetworkInterfaceType == interfaceType && netInterface.OperationalStatus == OperationalStatus.Up)
{
foreach (var ip in netInterface.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
ipAddrList.Add(ip.Address.ToString());
}
}
}
return ipAddrList.ToArray();
}
Sorry, this is the image I was trying to upload.
Also, the whole piece of code is the following:
public static string GetPublicIPv4()
{
var ipString = new WebClient().DownloadString("http://icanhazip.com");
return ipString.Replace("\\r\\n", "").Replace("\\n", "").Trim();
}
public static string[] GetLocalIPv4(NetworkInterfaceType interfaceType = NetworkInterfaceType.Ethernet)
{
var ipAddrList = new List<string>();
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (netInterface.NetworkInterfaceType == interfaceType && netInterface.OperationalStatus == OperationalStatus.Up)
{
foreach (var ip in netInterface.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
ipAddrList.Add(ip.Address.ToString());
}
}
}
return ipAddrList.ToArray();
}
public static string GetFirstLocalIPv4(NetworkInterfaceType interfaceType = NetworkInterfaceType.Ethernet) =>
GetLocalIPv4(interfaceType)?.FirstOrDefault();
public static IPAddress[] ResolveHostname(string hostName, bool ip6Wanted = false)
{
// hostname may already be an IP address
if (IPAddress.TryParse(hostName, out var outIpAddress))
return new[] { outIpAddress };
try
{
var addresslist = Dns.GetHostAddresses(hostName);
if (addresslist == null || addresslist.Length == 0)
return new IPAddress[0];
if (ip6Wanted)
return addresslist.Where(o => o.AddressFamily == AddressFamily.InterNetworkV6).ToArray();
return addresslist.Where(o => o.AddressFamily == AddressFamily.InterNetwork).ToArray();
}
catch { }
return null;
}
public static string TryResolveHostname(string hostName, bool ip6Wanted = false) =>
ResolveHostname(hostName, ip6Wanted)?.FirstOrDefault()?.ToString();
I think it covers the IPv6, right? So it might be something else
Ok, so I even tried it with this method from this thread :
public class IPManager
{
public static string GetIP(ADDRESSFAM Addfam)
{
//Return null if ADDRESSFAM is Ipv6 but Os does not support it
if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
{
return null;
}
string output = "0.0.0.0";
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
#endif
{
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
{
//IPv4
if (Addfam == ADDRESSFAM.IPv4)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (ip.Address.ToString() != "127.0.0.1")
output = ip.Address.ToString();
}
}
//IPv6
else if (Addfam == ADDRESSFAM.IPv6)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
{
if (ip.Address.ToString() != "127.0.0.1")
output = ip.Address.ToString();
}
}
}
}
}
return output;
}
}
public enum ADDRESSFAM
{
IPv4, IPv6
}
But to no avail. With the previous code that I showed and the new code shown right here (using both IPv4 and IPv6), I got either an empty Local IP (and public) or I get these seemingly random numbers:
Little update:
I was using the iPhone’s 4G data as wifi connection for the android device so far. When I connected to a WiFi router, however, it did work. I don’t know why mobile data is not ok for using local IPs.
Now I tested again on the same iPhone, and, if I use data, I get this local IP format (of this kind xxx.xx.xx.xx, which might work), but unfortunately most of the time I get no Local IP at all.
What is weirder though is that yesterday I was on I trip and I used the same iPhone with data and an iPad connected to that iPhone’s 4G, and it worked without an issue… This is a mystery to me, I have no clue about what’s going on, so any clarification on the matter is highly appreciated.
PS: Ok, I found out something basic about how the local network works (so I apologize for making a mess out of this). All devices must be connected to the same connection (either to the same WiFi router, 4G, 5G, or whatever), or else it won’t work (again, sorry for bringing something this basic). Now, if I wanted different devices to connect to a host with their respective connection (maybe each client is connecting from their home), then I thought they could join with the Host’s public IP:
public static string GetPublicIPv4()
{
var ipString = new WebClient().DownloadString("http://icanhazip.com");
return ipString.Replace("\\r\\n", "").Replace("\\n", "").Trim();
}
But they don’t seem to be able to connect by using the Host’s public IP.
So, the current situation is: why sometimes the Host’s local IP is null? and how does the Public IP work? because it doesn’t seem to be useful for connecting clients to the host.