- Home /
Get IPv4 adress?
As the title says, How can i get the machines iPv4 adress in Unity? I prefer C# if possible :) And sorry if this question already exists but I can not search in Unity Answers, I just get a Time Out (504).
Answer by weenchehawk · Aug 18, 2013 at 08:50 PM
Depending on the state of your code, you may be able to use
Network.player.ipAddress
This will give you the server IP if running as a server, or the client IP if running as a client.
Answer by ArkaneX · Aug 18, 2013 at 08:46 PM
var firstIPv4Address = Dns.GetHostEntry("answers.unity3d.com").AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
If you need it for local machine, then replace host name with Dns.GetHostName(). And don't forget to handle exceptions, especially SocketException.
Like this? public string firstIPv4Address = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
If so i get an error: Assets/Scripts/Network$$anonymous$$anager.cs(12,42): error CS0103: The name `Dns' does not exist in the current context
You're missing required namespaces. Please add the following in the beginning of your script:
using System.Net;
using System.Net.Sockets;
using System.Linq;
First two namespaces are related to network, last one is required for FirstOrDefault extension method.
Followed the steps here, but now it shows this: 'Dns' does not contain a definition for 'GetHostEntry'
Your answer
Follow this Question
Related Questions
I need an alternative to Network.player.ipAdress 0 Answers
Streaming EEG data from Qstreamer to Unity 0 Answers
How to Sync a sprite change to other clients? 0 Answers
Can I add a network prefab at runtime? 1 Answer
Instantiate By ID? 0 Answers