iPhone unexpected duplication of TCP packets on different ports

让人想犯罪 __ 提交于 2019-12-08 07:53:04

问题


I'm running this simple asio-based program:

Address address = Address::from_string(host);
Tcp::endpoint ep(address, port);
Tcp::resolver::iterator endpoint_iterator = resolver.resolve(ep);
Tcp::socket socket(io_service);
asio::connect(socket, endpoint_iterator);

Everything works fine on my host machine, but when this program run under iOS (both iphone and iphonesimulator) my sniffer detect some unexpected packets:

# client: 192.168.1.10, server: 192.168.1.100
# everything fine...
5.359761000 192.168.1.10  192.168.1.100  52712 [SYN]
5.359989000 192.168.1.100 192.168.1.10   52712 [SYN, ACK]
5.364103000 192.168.1.10  192.168.1.100  52712 [ACK]
5.364103000 192.168.1.10  192.168.1.100  52712 [FIN, ACK]
5.364386000 192.168.1.100 192.168.1.10   52712 [FIN, ACK]
5.366095000 192.168.1.10  192.168.1.100  52712 [ACK]
# Here comes unexpected part...
5.632569000 192.168.1.10  192.168.1.100  52716 [SYN]
5.632891000 192.168.1.100 192.168.1.10   52716 [SYN, ACK]
5.638314000 192.168.1.10  192.168.1.100  52716 [ACK]
5.638633000 192.168.1.10  192.168.1.100  52716 [FIN, ACK]
5.639017000 192.168.1.100 192.168.1.10   52716 [FIN, ACK]
5.644743000 192.168.1.10  192.168.1.100  52716 [ACK]

The strangest part is that unexpected packets received after main (successfully) exit. I've detect this packets on both client (when use simulator) and server side.

Where did they comes from? How can I fix it?

Update

Same behaviour with raw BSD-sockets and NSURLConnection. There is no such packets if I use Safari or Chrome to access server.

Update 2

  • "normal" packets dump: status line only, expanded
  • dump with unexpected packets: status line only, expanded

Update 3

  • version with getpid: http://pastebin.com/w1niwMMU

回答1:


I don't know if this is a bug or a feature, but what's happening is that when your app exits with a return value of 0, it gets re-started by launchd. So, your app is running twice and therefore tries its TCP connection twice.

You can verify this by adding, say sleep(10) just before you return from your main function; if you watch a process monitor, you'll see it launch, run for 10 seconds and then quit, get re-launched and run for another 10 seconds.

If you return a non-zero exit code or simply don't exit (as iOS apps usually don't), this won't happen.



来源:https://stackoverflow.com/questions/21605916/iphone-unexpected-duplication-of-tcp-packets-on-different-ports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!