Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Get IP Address

Discussion in 'Scripting' started by tecra134, Aug 10, 2011.

  1. tecra134

    tecra134

    Joined:
    Mar 19, 2011
    Posts:
    94
    Is it possible to pull the computers current IP address. I'm trying to figure out some code that will pull the computers IP Address and automatically place it in a text field before it starts a server.

    I'm only looking in JS because that's all I know. But help would be greatly appreciated.
     
    Last edited: Aug 10, 2011
  2. hipcodder

    hipcodder

    Joined:
    Sep 3, 2009
    Posts:
    221
    Try this

    Code (csharp):
    1. import System.Net;
    2.  
    3. function GetIP():String
    4. {
    5. var strHostName:String = "";
    6. strHostName = System.Net.Dns.GetHostName();
    7.  
    8.  var ipEntry:IPHostEntry = System.Net.Dns.GetHostEntry(strHostName);
    9.  
    10. var addr:IPAddress[] = ipEntry.AddressList;
    11.  
    12. return addr[addr.Length-1].ToString();
    13.  
    14. }
     
    maedron likes this.
  3. atrowbri

    atrowbri

    Joined:
    Sep 3, 2011
    Posts:
    4
  4. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Look at the code in the Multiplayer Network / Star Trooper tutorial. I did that just last week on the iPhone iTouch so I know it works.
     
  5. atrowbri

    atrowbri

    Joined:
    Sep 3, 2011
    Posts:
    4
    juan-jo and princePoorna like this.
  6. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Sorry I was a bit too lazy to look it up myself and I figured the tutorial might be useful for other things to you.
     
  7. atrowbri

    atrowbri

    Joined:
    Sep 3, 2011
    Posts:
    4
    Oh no problem. I appreciate it, A LOT. I put the info there for future people who happen across this (and also if that tutorial disappears for some reason or something)
     
    matEhickey likes this.
  8. Ozelo

    Ozelo

    Joined:
    Sep 5, 2012
    Posts:
    5
    I did the code above and I am NOT getting any IPv6 address. I wonder what I am missing. My OS has IPv6 connectivity and Unity just don't show anything. Could someone help me with IPv6 and Unity?
     
  9. karthick2323

    karthick2323

    Joined:
    Apr 13, 2017
    Posts:
    1

    hi !!!!!!!! dns.gethostentry not working in uwp.. can you help me with that? im using unity 2017.4.2f2. Need to retrieve the local ip address from the hololens using c#.
     
    IdeasAV and VincentRoumier like this.
unityunity