开发者

Get IP Address with Adobe Air 2

I'm building an app in Adobe Air 2 with AS3 and need to get the users ip address. From what I understand this code shoul开发者_如何学运维d work but is tracing: "::1" not an ip. Thank you.

var netInterfaces = NetworkInfo.networkInfo.findInterfaces();

var addresses = netInterfaces[0].addresses;

var userIp = addresses[0].address;

trace(userIp);


If you want the real IP and not your localhost, use this:

private function getIp():String{
    var netInterfaces:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces();
    var addresses:Vector.<InterfaceAddress> = netInterfaces[1].addresses;
    return addresses[0].address;    
}

If you're on Android, don't forget to set permissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


The code you wrote actually returns the ip adress of the first network Interface it finds. This is in your case the so called loopback device which is used for local connections

with IP4 the ip adress would be 127.0.0.1

with IP6 the ip-adress is ::1


Try this

var netInterfaces:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces();
if (netInterfaces && netInterfaces.length > 0) {    
  for each (var i:NetworkInterface in netInterfaces) {
    if (i.active) {
      var addresses:Vector.<InterfaceAddress> =i.addresses;
      for each (var j:InterfaceAddress in addresses) {
        trace("- Host : " + j.address);           
      }
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜