Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

get public IP address

Discussion in 'Multiplayer' started by benblo, Mar 26, 2008.

  1. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    How can I get the public IP address of the computer?
    Please not that I'm not in a networking context, I just need the IP to send to a server.
    I've tried Network.player.ipAddress, but it only returns the LAN IP if I don't do the network connection tests first.
     
  2. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    Use this code.

    Code (csharp):
    1. function CheckIP(){
    2.     myExtIPWWW = WWW("http://checkip.dyndns.org");
    3.     if(myExtIPWWW==null) return;
    4.     yield myExtIPWWW;
    5.     myExtIP=myExtIPWWW.data;
    6.     myExtIP=myExtIP.Substring(myExtIP.IndexOf(":")+1);
    7.     myExtIP=myExtIP.Substring(0,myExtIP.IndexOf("<"));
    8.     // print(myExtIP);
    9. }
     
  3. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Thanks Jeff!
    I was hoping for a more internal solution, that wouldn't involve querying an IP server... but it may not be possible.
    I don't like hard-coding an url like this, if the checkip.dyndns.org goes down, I'm in the dark.
     
    Jriles likes this.
  4. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I don't believe it's possible to accurately determine your external IP address without asking an external site to tell you what it is. In particular, if you're behind several routers, it can be very difficult to know where your intranet stops and the internet starts.

    There are several sites which provide similar services to checkip.dyndns.org. This is another one, and I'm sure you can find some others by Googling. If you have a few URLs on hand, you can try another if your first choice is unavailable.
     
  5. dirkk0

    dirkk0

    Joined:
    Nov 29, 2007
    Posts:
    16
    If you don't want to rely on an external provider you could supply your own service, with a simple php script for example.

    Something like:
    Code (csharp):
    1.  
    2. <?PHP
    3. $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
    4. ?>
    5.  
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    The server you are sending the ip address to should also be able to determine your IP address from the incomming connection. That's how whatismyip.com and even the php script above work.

    So unless the server is on the same intranet as the client, there's no need for the client to determine the address and send it explicitly.
     
  7. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    To all: thanks, I'll probably setup a rotating thing to check against several servers if some are unresponsive. Is there a "standard" protocol for this (like NTS for time servers)?

    To freyr: I'm with, I just don't understand why the service needs me to send the IP to begin with... but unfortunately it's out of my hands.
     
  8. reth

    reth

    Joined:
    Jan 31, 2013
    Posts:
    2
    You can get the public ip address of your computer in this site Ip-details.com.
     
  9. metallizard

    metallizard

    Joined:
    Apr 10, 2013
    Posts:
    3
    now unity have an internal code...

    network.player.externalIP just for you know :D
     
  10. forcepusher

    forcepusher

    Joined:
    Jun 25, 2012
    Posts:
    227
    yep, UNASSIGNED_SYSTEM_ADDRESS
     
  11. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    962
    Doesn't seem to work -- shows up as UNASSIGNED_SYSTEM_ADDRESS
     
  12. Chuptys

    Chuptys

    Joined:
    Mar 19, 2014
    Posts:
    18
  13. DmitryMelkov

    DmitryMelkov

    Joined:
    Nov 2, 2022
    Posts:
    1
    DONE:

    private string GetGlobalIPAddress()
    {
    var url = "https://api.ipify.org/";

    WebRequest request = WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    Stream dataStream = response.GetResponseStream();

    using StreamReader reader = new StreamReader(dataStream);

    var ip = reader.ReadToEnd();
    reader.Close();

    return ip;
    }
     
    Joy-less and Magasenakwa like this.