Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket error - sometimes?

so - I have this Socket (not XMLSocket, just Socket) client. I also have a custom PHP script on my server, that listens on port X. My client tries to connect to it.

Everything works fine, the security and communication, sync and whatever else. But - the Flash Player (AIR runtime actually) shoots an error when trying to connect, but ONLY when the server is not running... What? It is really weird - the error is actually handled by try catch (IOError), and even weirder, the line that is specified in the output as the error line is the line where I just CREATE the Socket...?

Hm...

Output:

Error #2044: Unhandled IOErrorEvent:. text=Error #2031: Socket Error.
    at ---.server::Client()[---/server/Client.as:167]
    at Function/<anonymous>()[---_fla.MainTimeline::frame1:430]
    at Function/<anonymous>()
    at Function/<anonymous>()[---_fla.MainTimeline::frame1:375]

Code:

try {
    Sock = new Socket(); // THIS is line 167
} catch (e:IOError){
    log("Could not connect!");
    status = "disconnected";
}

It does not really matter - the server is supposed to be still online, the errors won't show up... But ignoring an error is not good.

One more thing: when I comment out the line where I actually connect using Sock.connect(...) - it does not throw the error, but it obviously does not work... Also, the connect part is also in try catch (IOError) block...

WHY does Flash say the problem is on line 167 when it is obviously elsewhere? And / or what can be the problem?

like image 833
Aurel Bílý Avatar asked Sep 05 '10 12:09

Aurel Bílý


1 Answers

This might not seem obvious if you had not worked with Flash previously, but many Errors in the net api are asynchronous. Meaning, you cannot catch them with a catch block, because the error is not thrown when you execute the connect method, but at a later point.

In fact, the error message says you have an uncaught IOErrorEvent not an IOError.

This is covered here:

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/Socket.html#connect()

Basically, you have to add a handler for the IOErrorEvent (adding one for SecurityErrorEvent is a good idea as well). Something like this:

private function connect():void {
    socket = new Socket(host,port); 
    //  handle asynchronous errors in these handlers
    socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    try {
        socket.connect();
    } catch(ioError:IOError) {
    //  handle synchronous errors here  
    } catch(secError:SecurityError) {
    // and here
    }
}

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

private function securityErrorHandler(event:SecurityErrorEvent):void {
    trace("securityErrorHandler: " + event);
}
like image 112
Juan Pablo Califano Avatar answered Nov 26 '22 10:11

Juan Pablo Califano