Socket error - sometimes?
Asked Answered
W

3

5

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?

Wiggler answered 5/9, 2010 at 12:7 Comment(0)
O
9

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);
}
Outstand answered 5/9, 2010 at 16:4 Comment(1)
Oh - I though about adding that handler, but then I though that it is too late - I can add them only after initializing... But yeah - the error is made by the .connect(). Thanks anyways.Tweeze
G
0

I came across a similar error. It was occurring because I had removed the handler ioErrorHandler (removeEventListener) before issuing a sock.close(). sock.close() can throw an IoErrorEvent.

Even though the ioErrorEvent was thrown by sock.close(), the debugger showed it as unhandled error at the line where the socket constructor was called.

Calling the removeEventListener() after the sock.close() solved the problem.

Hope this helps.

Geomorphology answered 2/7, 2013 at 4:39 Comment(0)
B
0

I got the error in the same situation with wizard, and fixed it by change the order of close and remove event listener.

Boylan answered 13/2, 2014 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.