AIR SocketServer 清理?应用程序退出时如何关闭?

发布于 2025-01-02 03:09:31 字数 612 浏览 5 评论 0原文

我有一个简单的 AIR 应用程序,它使用 SocketServer 类侦听套接字。

当我第一次启动应用程序时,我有以下代码:

if( serverSocket.bound ) 
        {
            serverSocket.close();
            serverSocket = new ServerSocket();

        }
        serverSocket.bind( parseInt( localPort ), localIP );
        serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, onConnect );
        serverSocket.listen();

但是我发现,如果我在进行一些快速更改后尝试测试我的电影,我无法绑定到相同的地址:端口。我猜测不知何故该端口仍然被占用。我收到“在无效套接字上尝试操作”错误。

但如果我稍等一下然后测试我的电影,我可以绑定到地址:端口。

close() 没有生效。当用户决定退出应用程序时,我可以尝试 close() 吗?是否有某种事件允许我在应用程序退出之前执行某些操作?

I have a simple AIR app that listens on a socket using the SocketServer class.

I have the following code when I first start up the app:

if( serverSocket.bound ) 
        {
            serverSocket.close();
            serverSocket = new ServerSocket();

        }
        serverSocket.bind( parseInt( localPort ), localIP );
        serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, onConnect );
        serverSocket.listen();

However I find that if I try to test my movie after making some quick changes, I can't bind to the same address:port. I'm guessing that somehow that the port is still occupied. I get a "Operation attempted on invalid socket" error.

But if I wait a bit and then test my movie, I can bind to the address:port.

The close() is not taking affect. Can I try to close() this when the user decides to exit the app? Is there some sort of event that allows me to do something before the app quits?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橪书 2025-01-09 03:09:31

首先,该套接字与您上次运行应用程序期间拥有的套接字实例不同。其次,您要查找的事件是

NativeApplication.nativeApplication.addEventListener( Event.EXITING, this.handler_exit );

Call serverSocket.close();在事件处理程序中。也可能最好关闭在接受客户端时打开的所有客户端套接字。

First of all the socket is not the same socket instance you had during the previous run of your application. Second, the event you are looking for is

NativeApplication.nativeApplication.addEventListener( Event.EXITING, this.handler_exit );

Call serverSocket.close(); in the event handler. Also might be good to close all client sockets which were opened upon accepting clients.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文