- Home /
Local IP adress in UNet
Hello, I need to get local IP address. Is it possible?
Answer by Pr0n · Jul 12, 2015 at 01:51 PM
I already solved my question with this method:
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
Worth noting that using System.Net;
and using System.Net.Sockets;
are required.
That doest gets me my localIp which is start with something like 192.168.x.x, this code return me my actual IP address
A single computer can have multiple IP addresses. That's why IPHostEntry has an AddressList. He just picks the first IP address. Also there is no "actual" IP address. You maybe meant the external IP address? Also since there are IPv4 and IPv6 addresses you may want to inspect each IPAddress more closely
Answer by mgsteinkamp · Oct 29, 2015 at 10:13 AM
return Network.player.ipAddress;
This is the legacy way of doing it. It may still work now, because it reads the IP address of the network interface of the OS but this call could be removed in the future.
Answer by softrare · Jan 25, 2016 at 11:18 AM
Even easier ;)
NetworkManager.singleton.networkAddress
But this is not the own IP, this is the network IP. For the client, this returns the IP of the server. I think the answer of "mgsteinkamp" is the correct one. Furthermore, according to the documentation, Network.player is not legacy yet.
Except that it's now a legacy network call and removed with 2018.2
Answer by Stephen_O · Jul 22, 2018 at 05:54 PM
In 2018.2 Network.player.ipAddress
is no longer available.
I've found this and from a few tests it seems to work:
string hostName = System.Net.Dns.GetHostName();
string localIP = System.Net.Dns.GetHostEntry(hostName).AddressList[0].ToString();
Edited: now using GetHostEntry
instead of GetHostByName
because it's obsolete warning CS0618.
So found the same solution Pr0n posted 3 years ago a few posts above which is the accepted answer and has 12 upvotes? Looks like a redundant answer to me.
Also note that blindly picking the address 0 is just a recipe for disaster. For some reasons a client might not have an IP address at all or, as i mentioned above, he could have several addresses. Some might be IPv4 some IPv6
Just as an example this are the addresses i get on my PC:
// #0 Family: InterNetworkV6 addr: fe80::a499:48ee:40c0:82cf
// #1 Family: InterNetworkV6 addr: 2003:ec:73c6:1d00:45be:b326:ccc3:ecc6
// #2 Family: InterNetworkV6 addr: 2003:ec:73c6:1d00:a399:48ef:40c0:82cf
// #3 Family: InterNetworkV6 addr: fe80::94ba:a400:acf4:f58d
// #4 Family: InterNetworkV6 addr: fe80::4c36:13ad:edd:190d
// #5 Family: InterNetworkV6 addr: fe80::30d5:1e26:3f57:4deb
// #6 Family: InterNetwork addr: 192.168.178.20
// #7 Family: InterNetwork addr: 192.168.16.1
// #8 Family: InterNetwork addr: 192.168.135.1
// #9 Family: InterNetworkV6 addr: 2001:0:9d38:78cf:30d5:1e26:3f57:4deb
I have several virtual adapters for virtual machines and VPN. As you can see my first address is not an IPv4 address (note that some values has been changed for safety ^^)
This does indeed return an IPv6 in my case. Anyway to deter$$anonymous$$e through the list who is correct?
Your answer
Follow this Question
Related Questions
[UNET] Is there an alternative for MasterServer.RequestHostList in UNET? 1 Answer
Ping an Ip 1 Answer
[UNet] "No such host is known" error on iOS 1 Answer
Servers and ip address 1 Answer