as3.0 获取本地IP

package {
    import flash.display.Sprite;
    import flash.net.InterfaceAddress;
    import flash.net.NetworkInfo;
    import flash.net.NetworkInterface;

    public class GetAddress extends Sprite {
        public function GetAddress() {
            var networkInfo: NetworkInfo = NetworkInfo.networkInfo;
            var interfaces:Vector.<NetworkInterface> = networkInfo.findInterfaces(); 
            if (interfaces != null) {
                trace("Interface count: " + interfaces.length);
                for each(var interfaceObj: NetworkInterface in interfaces) {
                    trace("\nname: " + interfaceObj.name);
                    trace("display name: " + interfaceObj.displayName);
                    trace("mtu: " + interfaceObj.mtu);
                    trace("active?: " + interfaceObj.active);
                    trace("parent interface: " + interfaceObj.parent);
                    trace("hardware address: " + interfaceObj.hardwareAddress);
                    if (interfaceObj.subInterfaces != null) {
                        trace("# subinterfaces: " + interfaceObj.subInterfaces.length);
                    }
                    trace("# addresses: " + interfaceObj.addresses.length);
                    for each(var address: InterfaceAddress in interfaceObj.addresses) {
                        trace(" cuplayer.com type: " + address.ipVersion);
                        trace(" cuplayer.com address: " + address.address);
                        trace(" cuplayer.com broadcast: " + address.broadcast);
                        trace(" cuplayer.com prefix length: " + address.prefixLength);
                    }
                }
            }
        }
    }
}

 

 

//修改篇,因为常用IPv4其他的不需要,注意的是,有时候会仅无线,或者笔记本

package com{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.events.ServerSocketConnectEvent;
    import flash.net.ServerSocket;
    import flash.net.Socket;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.utils.ByteArray;
    import flash.net.InterfaceAddress;
    import flash.net.NetworkInfo;
    import flash.net.NetworkInterface;
    import flash.display.MovieClip;

    public class SocketServer extends MovieClip {
        private var serverSocket:ServerSocket = new ServerSocket();
        private var clientSocket:Socket;
        public  var IP:String;
        private var _clients:Array = [];
        private var thisMc:MovieClip;
        private var vidEmpty:MovieClip;
        private var pic:MovieClip;
        public static var instant:SocketServer
        public function SocketServer(_thisMc:MovieClip,_vidEmpty:MovieClip,_pic:MovieClip) {
            thisMc = _thisMc;
            pic = _pic;
            vidEmpty = _vidEmpty;
            GetAddress();
            init();
            instant=this
        }
        //当客户端成功连接服务端
        private function onConnect( event:ServerSocketConnectEvent):void {
            clientSocket = event.socket;
            clientSocket.addEventListener( ProgressEvent.SOCKET_DATA, onClientSocketData );
            _clients.push(clientSocket);
        }
        //当有数据通信时
        private function onClientSocketData( event:ProgressEvent ):void {
            var buffer:ByteArray = new ByteArray();
            var client:Socket = event.currentTarget as Socket;
            client.readBytes( buffer, 0, client.bytesAvailable );

            var loaderStr:String = buffer.toString();
            trace( "Received from Client"+ clientSocket.remoteAddress + ":" + clientSocket.remotePort+"-- " + buffer.toString() );
            var getStr = buffer.toString();
            if (getStr=="home") {
                MainLoader.ns.resume();
                while (pic.numChildren>0) {
                    pic.removeChildAt(0);
                }
                pic.visible = false;
            } else {
                pic.visible = true;
                MainLoader.ns.pause();
                LoaderJPG.addJPG(getStr);
            }
        }
        //绑定服务器ip 开始监听端口
        private function init() {
            if (serverSocket.bound) {
                serverSocket.close();
                serverSocket = new ServerSocket();

            }

            //IP为本机IP
            serverSocket.bind(10086, IP);
            serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, onConnect );
            serverSocket.listen();
        }
        private function GetAddress() {
            var networkInfo:NetworkInfo = NetworkInfo.networkInfo;
            var interfaces:Vector.<NetworkInterface >  = networkInfo.findInterfaces();
            if (interfaces != null) {
                for each (var interfaceObj:NetworkInterface in interfaces) {
                    if (interfaceObj.active == true && interfaceObj.mtu > 0) {
                        for each (var address:InterfaceAddress in interfaceObj.addresses) {
                            if (address.ipVersion == "IPv4") {
                                trace(" cuplayer.com address: " + address.address);
                                IP = address.address;
                                trace(IP)
                                return;
                            }
                        }
                    }
                }
            }
        }


    }
}

 

 

posted on 2021-03-15 10:59  zqiang0803  阅读(182)  评论(0编辑  收藏  举报

导航