Hi,

I am trying to create an AIR application that would listen to a port 
on the client's machine and print the messages. I know that this was 
not possible when running applications in the Flash sandbox but AIR 
applications are supposed to be free from such restrictions.

However, everytime I run the application, IOErrorEvent.IO_ERROR is 
raised and I receive the message:

[IOErrorEvent type="ioError" bubbles=false cancelable=false 
eventPhase=2 text="Error #2031: Socket Error. URL: localhost" 
errorID=2031]

I am using the sample code in the Socket documentation for Flex 
2.01/3.0 for this. I have tried replacing localhost by a multitude of 
hostnames but to no effect.

This is my first AIR app and any help would be greatly appreciated.

Application Code:

Consists of just 2 files, an actionscript class that extends Socket 
and adds listeners for all socket events and an mxml that 
instantiates it. Only the listener to IOErrorEvent.IO_ERROR gets 
called and nothing else happens.

Monitor.mxml
--------------------------------
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"; 
layout="absolute" creationComplete="init()">

        <mx:Script>
                <![CDATA[
                import com.monitor.CustomSocket;

                public function init():void
                {
                var socket:CustomSocket = new CustomSocket
("localhost", 9880);
                }
                        
                ]]>
        </mx:Script>
        
</mx:WindowedApplication>
------------------------------------

com/monitor/CustomSocket.as
-------------------------------------
package com.monitor
{
        import flash.errors.*;
        import flash.events.*;
        import flash.net.Socket;
        import mx.controls.Alert;
        
        [Bindable]
        public class CustomSocket extends Socket 
        {
            private var response:String;
        
            public function CustomSocket(host:String = null, 
port:uint = 0) {
                super(host, port);
                configureListeners();
            }
        
            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);
                Alert.show(event.toString(),"ioErrorHandler");
            }
        
            private function securityErrorHandler
(event:SecurityErrorEvent):void {
                trace("securityErrorHandler: " + event);
            }
        
            private function socketDataHandler
(event:ProgressEvent):void {
                trace("socketDataHandler: " + event);
                readResponse();
            }
        }
}
---------------------------------

Many Thanks,
Aman


Reply via email to