AS3客户端套接字无法连接到本地网络服务器

时间:2014-05-01 22:39:53

标签: c# actionscript-3 sockets serversocket

我在PC上有一个c#服务器套接字,在与第一台PC相同的本地网络上的另一台PC上有一个客户端AS3套接字。 问题是当c#和AS3在同一台PC上时连接正常,但是当我在本地网络上的另一台PC上移动c#时,AS3无法到达它!

1 个答案:

答案 0 :(得分:1)

请参阅以下代码:

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();
        }
    }