简体   繁体   中英

AS3 Client Socket can't connect to a local network server

I have ac# server socket on a PC, and a client AS3 socket on another PC on the same local network as the first PC. the problem is when the c# and AS3 are on the same PC the connection is OK, but when i move the c# on another PC on the local network the AS3 can't reach it !!

Please refer given below Code :

package {
        import flash.display.Sprite;

        public class SocketExample extends Sprite {
            private var socket:CustomSocket;

            public function SocketExample() {
                socket = new CustomSocket("localhost", 80);
            }
        }
    }

    import flash.errors.*;
    import flash.events.*;
    import flash.net.Socket;

    class CustomSocket extends Socket {
        private var response:String;

        public function CustomSocket(host:String = null, port:uint = 0) {
            super();
            configureListeners();
            if (host && port)  {
                super.connect(host, port);
            }
        }

        private function configureListeners():void {
            addEventListener(Event.CLOSE, closeHandler);
            addEventListener(Event.CONNECT, connectHandler);
            addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
        }

        private function writeln(str:String):void {
            str += "\n";
            try {
                writeUTFBytes(str);
            }
            catch(e:IOError) {
                trace(e);
            }
        }

        private function sendRequest():void {
            trace("sendRequest");
            response = "";
            writeln("GET /");
            flush();
        }

        private function readResponse():void {
            var str:String = readUTFBytes(bytesAvailable);
            response += str;
        }

        private function closeHandler(event:Event):void {
            trace("closeHandler: " + event);
            trace(response.toString());
        }

        private function connectHandler(event:Event):void {
            trace("connectHandler: " + event);
            sendRequest();
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function socketDataHandler(event:ProgressEvent):void {
            trace("socketDataHandler: " + event);
            readResponse();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM