Sending UDP packets on iPhone is out of the new new 3G connection, but it works differently

I had a strange problem sending UDP packets from iPhone via 3G. Almost every time my application starts after a longer period of network inactivity (for example, after the phone wakes up from sleep mode or just connects to 3G), my application cannot send any UDP packets. I get error status kCFSocketErrorfrom CFSocketSendData. The application logic then retries sending the packet periodically every five seconds, but no matter how long I run , it continues to fail. However, when I switch the application to the background and open, for example, a web page in Safari (while the application is still not working in the background), the application suddenly starts working. Basically, it looks like another network activity (from Safari, Maps, etc.), "The real network is starting." Do I need to do something special in terms of network initialization?

This is a simplified piece of code that I use to send UDP packets:

CFSocketRef cfSocket = CFSocketCreate(NULL, 0, SOCK_DGRAM, IPPROTO_UDP, kCFSocketNoCallBack, NULL, NULL);
if (!cfSocket)
{
    // snip: some error handling
}

struct sockaddr_in addr;
// snip: init addr

CFDataRef cfAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));

CFSocketError sendPacketResult = CFSocketSendData(cfSocket, cfAddr, cfPacketData, 0.0);
if (sendPacketResult != kCFSocketSuccess)
{
    // try again in 5 seconds
}

Everything works fine, without any Wi-Fi issues.

To be fair, I think I sometimes saw (but very rarely) the same behavior in standard iPhone applications (Safari, Maps, ...). Sometimes they refuse to connect (even thinking that I see a 3G icon), and I need to close and reopen them. But this is very rare.

: , : iPhone, EPERM (errno == 1) () UDP?.

+5
1

3G TCP/IP IOS , , , . BSD (, UDP, UDP Apple), 3G. Apple CFSocketStream Apple, .

, , , CFSocketStream . . , CFSocketStream , .

, , . .

+4

All Articles