- Home /
Get the device IP address from Unity
Hi,
I'm developing a multiplayer game to run on Android and IOS. I'm using Mirror Networking Library to handle the network related work. The game can be played between players who are connected to the same local network.
In my game there is no dedicated server instance and a player who hosts to game will act as the server. In that case I want to show the IP address of the player who hosts the game in the players screen so that others can manually enter the server's IP address and join the game.
I still couldn't come up with a solution to get the IP address of the local device through Unity or Mirror Networking Library. Is it possible? If so how to get it.
This game is intended to run on Android and IOS. So the solution should support both the platforms.
Thanks.
Hello, Amodth Can you please explain how did you convert your project from localhost to LAN?
Answer by GetLitGames · May 18, 2020 at 08:41 AM
public string GetLocalIPv4()
{
return Dns.GetHostEntry(Dns.GetHostName())
.AddressList.First(
f => f.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
.ToString();
}
Note that just grabbing the first entry usually works well for PCs but does not necessarily work that well on mobile. Android usually favors IPv6. Also Android devices are often connected to several networks at the same time and could have several different addresses. However this should get him on the right track ^^.
Hi SubtleTechnology Yes this works. But the problem is as Bunny83 mentioned it returns the 1st IP address from the list. What I actually want is to get is the WiFi IP address of the device. Is there a way to specifically return the Wifi IP address instead of the 1st IP from the list?
There is nothing like a dedicated WiFi IP range. Just by looking at the addresses you can'r really tell what this address belongs to. You can rule out some addresses like the local loopback (127.x.x.x) and maybe some others. However WiFi could use pretty much any address range. Usually WiFi would use addresses from the private address range. Keep in $$anonymous$$d that IPv4 has different private ranges then IPv6.
There are other platform dependent solutions to get the actual WiFi information from the OS. Of course on mobile devices this most likely requires additional privileges and some native code plugin.
Answer by AleixT · Jul 16, 2020 at 02:47 PM
Hi Amodth, I'm trying to do the same, connect different devices in the same room by the IP address. Have you do it successfully?
Answer by Honorsoft · Oct 31, 2020 at 07:56 AM
I am also starting out with using Mirror, but I have some info that might help... you can get the IP's and see what players (servers or sever-clients) are available by adding the "Network Discovery" and "Network Discovery HUD" components included with Mirror. Any hosted games (servers/server-clients) should show up in the list.
I am also running some more tests on Mirror by adding some 3rd-party code for IP detection. I am not sure if the IPv4 address is the one needed to connect Mirror on mobile devices, but here's some Unity code to get the IPv4 (un-tested by me so far):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
public class TestLocationService : MonoBehaviour
{
public Text hintText;
private void Start()
{
GetLocalIPAddress();
}
public string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
hintText.text = ip.ToString();
return ip.ToString();
}
}
throw new System.Exception("No network adapters with an IPv4 address in the system!");
}
}
Your answer
Follow this Question
Related Questions
Mirror Matchmaking, Matchmaking System Using Mirror 3 Answers
Multiplayer for Android/iOS platforms 0 Answers
Smartphone multiplayer - NAT problem? 2 Answers
PC Android LAN Network Connection 1 Answer