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.

public string GetLocalIPv4()
{
return Dns.GetHostEntry(Dns.GetHostName())
.AddressList.First(
f => f.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
.ToString();
}

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!");
    }

}

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?