套接字错误 - 有时?错误

2023-09-08 14:06:56 作者:南城旧少年

所以 - 我有这个插座(不的XMLSocket,只是插座)客户端。我也有我的服务器,侦听端口十,我的客户端试图连接到它的自定义PHP脚本。

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.

一切工作正常,安全和通信,同步和任何其他。但是 - Flash播放器(AIR运行时实际)射门尝试连接时出现错误,但只有当服务器运行的不是...什么?这实在是不可思议 - 错误是由尝试catch(IO错误),甚至是怪异实际处理中的错误行输出中指定的线是我刚刚创建的Socket行...

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...?

嗯...

输出:

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:

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.

一件事:当我注释掉线,我真正连接使用Sock.connect(...) - 它不会引发错误,但它显然是行不通的......另外,连接部分也在尝试捕捉(IO错误)块...

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...

为什么闪存说的问题是线167时,它显然是在其他地方?和/或有什么可以是问题?

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

推荐答案

这可能还不是很明显,如果你没有工作与Flash previously,但在网络API很多错误都是异步的。意思是,你不能用一个catch块赶上他们,因为当你执行的连接方法,但在以后的误差不抛出。

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.

其实,错误消息说你有一个未捕获的 IOErrorEvent 不是的IOError

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

这是这里介绍:

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

基本上,你必须添加为IOErrorEvent处理程序(添加一个用于SecurityErrorEvent是一个好主意为好)。事情是这样的:

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