CFNetwork 数据包读取回调未被调用 (iPhone)
在 iPhone 模拟器上运行,我正在侦听端口上的数据包,但我的回调永远不会被调用(并且数据包肯定正在发送,用wireshark检查)。简化的代码如下:
#define IN_PORT (51112)
static void ReadCallback (CFSocketRef theSocket, CFSocketCallBackType theType, CFDataRef theAddress, const void *data, void *info)
{
NSLog(@"Data received");
}
@implementation MyListener
- (void) ListenOnPort:(uint16_t)port withCallback:(CFSocketCallBack)callback
{
CFSocketContext context = {0,self,NULL,NULL,NULL};
CFSocketRef cfSocket = CFSocketCreate(NULL, PF_INET, SOCK_DGRAM, PPROTO_UDP, kCFSocketReadCallBack, callback, &context);
if (cfSocket==NULL)
NSLog(@"CFSocketCreate failed");
struct sockaddr_in addr;
memset(&addr,0,sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)];
if(kCFSocketSuccess != CFSocketSetAddress(cfSocket, (CFDataRef)address))
NSLog(@"CFSocketSetAddress failed");
CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, cfSocket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopCommonModes);
CFRelease(rls);
}
@end
在代码的其他地方,MyListener 被实例化,并调用其方法 ListenOnPort,如下所示:
myListener = [[MyListener alloc] init];
[myListener ListenOnPort:IN_PORT withCallback:&ReadCallback];
没有发生故障,但从未收到数据。数据包的来源是同一局域网上的另一台计算机,就像我提到的,它们可以被wireshark(具有正确IP和端口号的udp)看到。
这是我第一次尝试使用这个框架。任何帮助表示赞赏。
Running on the iPhone simulator, I am listening on a port for packets, but my callback never gets called (and the packets are being sent for sure, checked with wireshark). Simplified code follows:
#define IN_PORT (51112)
static void ReadCallback (CFSocketRef theSocket, CFSocketCallBackType theType, CFDataRef theAddress, const void *data, void *info)
{
NSLog(@"Data received");
}
@implementation MyListener
- (void) ListenOnPort:(uint16_t)port withCallback:(CFSocketCallBack)callback
{
CFSocketContext context = {0,self,NULL,NULL,NULL};
CFSocketRef cfSocket = CFSocketCreate(NULL, PF_INET, SOCK_DGRAM, PPROTO_UDP, kCFSocketReadCallBack, callback, &context);
if (cfSocket==NULL)
NSLog(@"CFSocketCreate failed");
struct sockaddr_in addr;
memset(&addr,0,sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)];
if(kCFSocketSuccess != CFSocketSetAddress(cfSocket, (CFDataRef)address))
NSLog(@"CFSocketSetAddress failed");
CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, cfSocket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopCommonModes);
CFRelease(rls);
}
@end
Somewhere else in the code, MyListener is instantiated and its method ListenOnPort is called, like so:
myListener = [[MyListener alloc] init];
[myListener ListenOnPort:IN_PORT withCallback:&ReadCallback];
No failures occur, but Data is never received. Source of packets is another computer on the same lan, and like I mentioned, they are seen by wireshark (udp with correct ip and port number).
This is the first time I try to use this framework. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将:更改
为:(
模拟器在Intel上运行,因此您需要转换为网络顺序)
Try changing:
To:
(The simulator runs on Intel so you need to convert to network order)
尝试将 CFRunLoopGetCurrent() 替换为 CFRunLoopGetMain()
Try to replace CFRunLoopGetCurrent() with CFRunLoopGetMain()