iPhone 进入睡眠状态后,如何恢复 TCP Socket 连接

发布于 2024-07-15 13:19:03 字数 891 浏览 6 评论 0原文

..我的应用程序仍在运行!

有这个问题 关于它进入睡眠+醒来时会发生什么。 我知道它在调用:(

- (void)AppDidBecomeActive:(NSNotification *)notification {

    if(jsocket){
        [jsocket release];
    }
    jsocket = [[johnsocket alloc] init];

    if(![jsocket connectViaSocket:(NSString*)ipaddress port:@"3660"]){
        //port 3660 is the XML port!!! good thing to know... 3663 is the binary protocol port.  yay!
        NSLog(@"error connecting from rooms page");
    }
}

注意:我正在使用 Asyncsocket.m,它是帮助管理套接字等的伟大类..这就是 jsocket 的一个实例。这是一个 tcp 客户端应用程序。)

我实际上在这个函数中有一个警报弹出窗口,以确保它被调用..你看,iphone/ itouch 在插入 USB 时不会进入睡眠状态,这使得调试变得更加困难。

所以我的问题是,如何才能让它不崩溃?

..and my app is still running!

There is this question about what happens when it goes to sleep + wakes up. I know it's calling:

- (void)AppDidBecomeActive:(NSNotification *)notification {

    if(jsocket){
        [jsocket release];
    }
    jsocket = [[johnsocket alloc] init];

    if(![jsocket connectViaSocket:(NSString*)ipaddress port:@"3660"]){
        //port 3660 is the XML port!!! good thing to know... 3663 is the binary protocol port.  yay!
        NSLog(@"error connecting from rooms page");
    }
}

(note: I'm using Asyncsocket.m, which is a great class that help manage sockets and such.. that's what jsocket is an instance of. This is a tcp client application.)

I actually had an alert popup in this function to make sure it was being called.. you see, the iphone/itouch don't go to sleep while plugged into USB, making this a more difficult situation to debug.

So my question is, how can I make it not crash?

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

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

发布评论

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

评论(1

忱杏 2024-07-22 13:19:03

除非您对服务器有非常完全的控制,否则您无法在任意睡眠时间后简单地“唤醒”套接字。 远程服务器可能会在超时后关闭连接 - 无论如何,这是您应该假设的。

您将必须重新启动连接并从头开始将您的状态与服务器同步。

Unless you have very full control over the server, there is nothing you can do to simply "wake up" a socket after an arbirtary time asleep. the remote server will probably have closed the connection after a timeout - that is what you should assume anyway.

You will have to re-initiate the connection and sync your state with the server from scratch.

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