0

I am making a multiplayer game. One of the players plays as host and the other one connects as client. To connect you have to enter the IP and be on the same WIFI.

The problem is that while on PC it works fine on android doesn't. The problem is that you can join well if the server is made in PC but not if you do it on android, android can join to PC but not to other android. I think the problem is in the IP that I show.

Here is the code I use:

GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        throw new System.Exception("No network adapters with an IPv4 address in the system!");
    }
Pedro _F.M
  • 31
  • 2
  • would this be an option for you? https://learn.microsoft.com/en-us/answers/questions/345698/obtain-ip-address-from-host-name-on-android-using maybe `Dns.GetHostEntry` is generally not supported on Android – derHugo Jun 13 '23 at 08:03
  • alternatively try this one https://stackoverflow.com/a/24814027/7111561 .. in general devices in your local network might be connecting with you on a different IP than the one you use to access the internet .. depends a lot on your network setup of course – derHugo Jun 13 '23 at 08:08

2 Answers2

0

well your on the right track, with a few twists! :) this function returns the "first" local IP it sees, thing is, Android devices can be connected to multiple devices at the same time, for example you can have both Mobile Data and WiFi enabled at the same time! so you get a local IP from this function, it just might not be the one you needed! also keep in mind most of the times Android devices prefer using IPv6 for private(local) communications.

now in your case, you preferably want to show the user which local IPs are assigned to them, and let them select one of them, this way your Host and Client can compare they're subnet/address and choose the one they both have!

you might even ping those IPs for them so they can compare pings as well!

0

I alredy found the solutión, instead of the player having their IP in the screen for other players to connect to I used Network Discovery.

Is not the same thing but it did the trick.

If you want to chek how to use it here it is: https://mirror-networking.gitbook.io/docs/manual/components/network-discovery

Thanks for any help you give me.

Pedro _F.M
  • 31
  • 2