English 中文(简体)
电话网络
原标题:iphone network cfsocket callback multithread
  • 时间:2011-10-11 05:11:13
  •  标签:
  • iphone

我正试图撰写一份SOS申请,该申请聆听了TCP广播的节目和数据。 更先进的实施措施似乎表明,利用背景线和原始材料来实施这种特征,但我认为,这是我第一次尝试利用爱阵列来保持简单。

The problem is that when I run it, it s fine , but it run in main thread,don t run in a new thread. why doesn t the callback function run in a background thread?

该守则是:

- (void)connecToServer{
    int yes = 1;
    CFSocketContext CTX = {0,self,NULL,NULL,NULL};
    _socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketDataCallBack, TCPServerConnectCallBack, &CTX);    
    setsockopt(CFSocketGetNative(_socket), SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
    struct sockaddr_in addr4;    
    memset(&addr4, 0, sizeof(addr4));    
    addr4.sin_len = sizeof(addr4);    
    addr4.sin_family = AF_INET;    
    addr4.sin_port = htons(6242);    
    addr4.sin_addr.s_addr = inet_addr("");    
    address = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&addr4, sizeof(addr4));        
    CFRunLoopRef cfrl = CFRunLoopGetCurrent();    
    CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, _socket, 0);    
    CFRunLoopAddSource(cfrl, source, kCFRunLoopCommonModes);    
    CFRelease(source);
}

反馈职能:

static void TCPServerConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info){
    NSData *nsdata = (NSData*)data;
    NSUInteger len = [nsdata length];
    Byte *byteData = (Byte*)malloc(len);
    memcpy(byteData, [nsdata bytes], len);
    Byte *userData;
    NetWorkConnect *netWorkConnect = (NetWorkConnect*)info;
    switch(byteData[5]){
        case 2:
            userData = (Byte*)malloc(len-9);
            memcpy(userData, byteData+7, len-9);
            memset(&(netWorkConnect->oQuota), 0, sizeof(netWorkConnect->oQuota));
            [netWorkConnect performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
           break;
        default:
           break;
    }
}
最佳回答

You may want to use cocoaasyncsocket: it will make your life much easier. http://code.google.com/p/cocoaasyncsocket/

问题回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签