Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Networking - discovering local (not public) IP?

Discussion in 'iOS and tvOS' started by Axel-F, Apr 22, 2010.

  1. Axel-F

    Axel-F

    Joined:
    Mar 20, 2009
    Posts:
    223
    Hi,

    via Network.InitializeServer and MasterServer.RegisterHost I'm creating successfull a multiplayer server.
    On the client side I discover via MasterServer.RequestHostList running servers and poll in the list with MasterServer.PollHostList.

    Works like a charm..but...the HostData elements I'm getting back containing always (and only) the public IP of my network (the IP I got from my internet service provider). This is great for running servers used for MMORPG's and stuff like this. But what I need is a local gameplay and so I need the local (192.168.x.x e.g.) IP. How do I discover this?

    Sometimes (1 try out of 20) the element.ip array contains the local IP and the connection succeeds. But mostly the one and only IP in element.ip is the public one.

    Thanks!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if you need it for local gameplay, don't use the master server at all but use udp broadcasts for discovery for example.

    Also the master server, given your configuration allows it at all on the system - firewall will hand oyu back 3 ips:

    Internet ip, LAN ip and actually even 127.0.0.1

    if your firewall interfers (the connection tester will give you such informations) you potentially won't see the later two, just the one thats detectable by your sending computers packet signature
     
    jm91086 likes this.
  3. April

    April

    Joined:
    Dec 14, 2009
    Posts:
    62
    You may write

    var server : NetworkPlayer;
    var serverIP : String;

    function Awake() {

    if (Network.isServer) {

    serverIP = "" + server.ipAddress;

    }
    }

    then send serverIP to all clients via RPC.
     
  4. Axel-F

    Axel-F

    Joined:
    Mar 20, 2009
    Posts:
    223
    UDP broadcasts? Uh, I have no idea. I still need a running master server in that case, or how do I start a game server without?
    However, April's advice is a good one but I cant send RPC request's without a running server. I did it now in the following way: started the server and put the value of NetworkPlayer.ipAdress into the "comments" variable of the server. When the client connects it connects to element.comment...not to element.ip. A little bit dirty, but at least it works.
     
  5. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,864
  6. rdvt

    rdvt

    Joined:
    Aug 23, 2014
    Posts:
    23
    you can use this:

    System.Net.IPAddress[] a = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());

    for (int i = 0; i < a.Length; i++)
    {
    Debug.Log(a[ i ].ToString());
    }