2

我正在使用为 UDP 配置的 CFSocket 通过 wifi 发送数据。问题是当我收到一个空的UDP数据包时,wifi突然停止工作。在那之后我不能再接收或发送任何东西了。有谁知道似乎是什么问题?这是我的配置和回调代码:

void socketCallback(CFSocketRef cfSocket, CFSocketCallBackType type, CFDataRef address, const void *data, void *userInfo)
{
    NSData * inputData = (__bridge NSData*)data;
    NSUInteger inputDataCapacity = inputData.length * 2;

    if(inputDataCapacity > minimalFrameSize*2){
        //DO STH
    }
}

-(void) initNetworkCommunication
{
    CFSocketContext socketContext = {0, (__bridge void *)(self), NULL, NULL, NULL};
    _cfSocket = CFSocketCreate(kCFAllocatorDefault, AF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketAcceptCallBack | kCFSocketDataCallBack, (CFSocketCallBack)socketCallback, &socketContext);

    if ( _cfSocket == NULL) {
//        NSLog(@"CfSocketCreate failed");
    }
    else {
        if( _cfSocket ) {
            _addrData = [Utils createIpAddressDataRef:COMM_INT_LISTENER_ADDR];

            int flag = 1;
            CFSocketNativeHandle socket_handle = (CFSocketNativeHandle)CFSocketGetNative(_cfSocket);
            setsockopt(socket_handle, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag));

            //set address of the socket to listen to
            CFSocketSetAddress (_cfSocket, _addrData);

            _cfSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, _cfSocket, 0);
            CFRunLoopAddSource(CFRunLoopGetCurrent(), _cfSource, kCFRunLoopDefaultMode);

            CFRelease(_cfSource);
            CFRelease(_cfSocket);
        }
    }
}

有时在 wifi“崩溃”发生后,我在使用 CFSocketSendData 时会收到“EXC_BAD_ACCESS”。

4

0 回答 0