CFNetwork 数据包读取回调未被调用 (iPhone)

发布于 2024-09-15 21:25:57 字数 1564 浏览 12 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2024-09-22 21:25:57

尝试将:更改

addr.sin_port = port;

为:(

addr.sin_port = htons(port);

模拟器在Intel上运行,因此您需要转换为网络顺序)

Try changing:

addr.sin_port = port;

To:

addr.sin_port = htons(port);

(The simulator runs on Intel so you need to convert to network order)

写给空气的情书 2024-09-22 21:25:57

尝试将 CFRunLoopGetCurrent() 替换为 CFRunLoopGetMain()

Try to replace CFRunLoopGetCurrent() with CFRunLoopGetMain()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文