- Home /
How to get local IP in Unity 2018.2.4
The Network class is abandoned. So how could I get my device local IP, I didn't find any new method about local IP. Please Help.
Answer by gvergidis · Aug 21, 2018 at 11:06 AM
Well, you did not search google I guess :)
Try this :
public static string LocalIPAddress()
{
IPHostEntry host;
string localIP = "0.0.0.0";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
this works in some cases, but not in many.
for example, when i run this code on my iPhone, host.AddressList contains well over a dozen IPAddresses which have the given AddressFamily, but only one of which is correct. You might get lucky and the correct one is the first one, but in general that's not the case.
"Correct" meaning:
1) it matches what i see in iOS preferences | WiFi | | IP Address
2) if i start listening on a socket, i can actually open a connection to that socket from another machine using that IP Address. Other IP Addresses fail to connect.
In fact, none of the many approaches proposed in this StackOverflow page have panned out for me. $$anonymous$$y current plan is to write a native (macos/ios/android) plugin to retrieve this information.
Your answer
Follow this Question
Related Questions
How can I check if a particular IP is a server? 1 Answer
Getting android device's IP 1 Answer
how do you get player external ip address 1 Answer
network address automatically change to localhost if i want to host 0 Answers
Erro in MultiPlayer 0 Answers