问题描述
我正在运行这个基于asio的简单程序:
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);
一切都可以在我的主机上正常运行,但是当该程序在iOS(iphone和iphonesimulator都)下运行时,我的嗅探器会检测到一些意外的数据包:
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?
与原始 BSD套接字和 NSURLConnection .如果我使用Safari或Chrome浏览器访问服务器,则没有此类数据包.
Same behaviour with raw BSD-sockets and NSURLConnection. There is no such packets if I use Safari or Chrome to access server.
- "normal" packets dump: status line only, expanded
- dump with unexpected packets: status line only, expanded
- 具有
getpid
的版本: http://pastebin.com/w1niwMMU
- version with
getpid
: http://pastebin.com/w1niwMMU
推荐答案
我不知道这是错误还是功能,但是发生的情况是,当您的应用程序退出且返回值为0时,它会重新-由launchd启动.因此,您的应用程序运行了两次,因此尝试了两次TCP连接.
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.
您可以通过从主函数返回之前添加,例如说sleep(10)
来验证这一点;如果您观看过程监视器,则会看到它启动,运行10秒钟,然后退出,重新启动,再运行10秒钟.
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.
如果您返回非零退出代码或只是不退出(因为iOS应用程序通常不退出),则不会发生这种情况.
If you return a non-zero exit code or simply don't exit (as iOS apps usually don't), this won't happen.
这篇关于iPhone在不同端口上意外复制TCP数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!