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
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.