简体   繁体   English

当iOS应用程序进入后台时,TCP和UDP(与多播)连接会发生什么

[英]What happens to TCP and UDP (with multicast) connection when an iOS Application did enter background

I created couple experiments: 我创建了几个实验:

Setup 1: I created a TCP Sender app and a TCP Receiver app. 设置1:我创建了一个TCP Sender应用程序和一个TCP Receiver应用程序。

For this experiment, I started the TCP Sender on an iOS device and the TCP Receiver on another iOS device. 在本次实验中,我在iOS设备上启动了TCP Sender,在另一台iOS设备上启动了TCP Receiver。 And then both are verified to have made connection and sending and received data. 然后两者都经过验证已建立连接并发送和接收数据。 I then put the TCP Receiver app into background. 然后我将TCP Receiver应用程序置于后台。 The TCP Sender app indicated lost of connection and crashed (yes, I intended that way). TCP Sender应用程序指示连接丢失和崩溃(是的,我打算那样)。

Setup 2: I created a UDP Sender app and a UDP Receiver app. 设置2:我创建了一个UDP Sender应用程序和一个UDP Receiver应用程序。

Same as above, I started the UDP Sender app on an iOS device and the UDP Receiver app on another iOS device. 与上面相同,我在iOS设备上启动了UDP Sender应用程序,在另一个iOS设备上启动了UDP Receiver应用程序。 On the UDP Receiver app I subscribed to a multicast group, etc. I verified that the UDP Receiver app is receiving the data from that multicast group sent out by UDP Sender app. 在UDP Receiver应用程序上,我订阅了一个多播组等。我验证了UDP Receiver应用程序正在从UDP Sender应用程序发出的该多播组中接收数据。 I then put the UDP Receiver app into background. 然后我将UDP Receiver应用程序置于后台。 After 2 minutes, I get the UDP Sender app to send out another piece of data. 2分钟后,我得到UDP Sender应用程序发送另一条数据。 I then quit the UDP Sender app completely and turn off that iOS device. 然后我完全退出UDP Sender应用程序并关闭该iOS设备。 I then wait for another 2 minutes or more, and then bring up the UDP Receiver app from background. 然后我等待另外2分钟或更长时间,然后从后台启动UDP Receiver应用程序。 The UDP Receiver app did receive the data that was sent out by the UDP Sender app before it was terminated. UDP Receiver应用程序确实接收了UDP Sender应用程序终止之前发送的数据。

In setup1, my explanation is because TCP is a connection oriented. 在setup1中,我的解释是因为TCP是面向连接的。

In setup2, I understand UDP is connectionless. 在setup2中,我理解UDP是无连接的。 Any explanation why setup2 it worked the way in my experience? 任何解释为什么setup2在我的经验中起作用? (Still receiving data even in background mode) (即使在后台模式下仍然可以接收数据)

All that happens when you put an app into the background and then let it go suspended is that it stops getting scheduled by the kernel. 当你将一个应用程序放到后台然后让它被暂停时发生的一切就是它停止了内核的调度。 It doesn't immediately break any connections or tear down any sockets (unless you force it to.) 它不会立即断开任何连接或拆除任何插座(除非你强制它。)

In your UDP case, the kernel receives the packet and puts it into a kernel buffer, waiting for your app to receive it. 在您的UDP情况下,内核接收数据包并将其放入内核缓冲区,等待您的应用程序接收它。 Since your app process exists but is effectively stopped, the data will just sit in the kernel buffer. 由于您的应用程序进程存在但有效停止,因此数据将仅位于内核缓冲区中。 If you get too much data, it'll overrun the kernel buffer and get dropped. 如果你获得太多数据,它将超出内核缓冲区并被丢弃。 Otherwise, your app can receive it when (if) it's scheduled again. 否则,您的应用可以在(如果)再次安排时收到它。

In the TCP case, pretty much the same thing hapens. 在TCP案例中,几乎完全相同。

But (big but): the OS always has the option to tear down sockets for suspended apps if it wants to, based on memory pressure, etc. So while it won't necessarily do it gratuitously, it may do it. 但是(很大但是):操作系统总是可以选择根据内存压力等方式拆除已挂起的应用程序的套接字。因此,虽然它不一定无偿地执行,但它可能会这样做。

I'm not sure exactly why you're seeing the TCP connection severed quickly. 我不确定你为什么看到TCP连接快速切断。 It may be that the kernel heuristics for servering TCP connections is more aggressive than for UDP sockets since TCP connections require more state and more continuous processing than do UDP sockets. 可能是服务TCP连接的内核启发式比UDP套接字更具攻击性,因为TCP连接需要比UDP套接字更多的状态和更连续的处理。

See Technical Note TN2277 Networking and Multitasking . 请参阅技术说明TN2277网络和多任务处理

My opinion is because of the os, this should not be happening if you tried on an android os because IOs has restrictions on what can work on the background and what can't. 我的观点是因为操作系统,如果你尝试使用android操作系统,这不应该发生,因为IOs对可以在后台工作的东西有什么限制,什么不可以。

From what you said i think its because TCP requires more resources to send information. 从你所说的我认为它,因为TCP需要更多的资源来发送信息。 TCP uses data streams and UDP uses data blocks. TCP使用数据流,UDP使用数据块。 The problem is that TCP creates bigger packages of data while UDP uses 8 kb of data blocks. 问题是TCP创建更大的数据包,而UDP使用8 kb的数据块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM