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.
Use this code.
function CheckIP(){
myExtIPWWW = WWW("http://checkip.dyndns.org");
if(myExtIPWWW==null) return;
yield myExtIPWWW;
myExtIP=myExtIPWWW.data;
myExtIP=myExtIP.Substring(myExtIP.IndexOf(":")+1);
myExtIP=myExtIP.Substring(0,myExtIP.IndexOf("<"));
// print(myExtIP);
}
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.
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.
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:
<?PHP
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
?>
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.
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.
You can get the public ip address of your computer in this site Ip-details.com.
now unity have an internal code…
network.player.externalIP just for you know
yep, UNASSIGNED_SYSTEM_ADDRESS
Doesn’t seem to work – shows up as UNASSIGNED_SYSTEM_ADDRESS
If you’re using WWW, try https://www.ipify.org/ (for example new WWW(“https://api.ipify.org”)).
It has a well-documented API and works quite fast!
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;
}