OS X:如何使用 NSRunLoop 观察套接字读取事件?

发布于 2024-08-21 22:27:41 字数 1102 浏览 14 评论 0原文

我正在写一个 Cocoa 应用程序。应用程序中有一个套接字,每当套接字变得可读时,我想从套接字读取数据,处理数据,并相应地更新用户界面。我想将读取事件检查集成到主循环中,即我想将套接字附加到主循环,并让主循环在该套接字变得可读时调用回调。

我已经编写了一个测试应用程序,但由于某种原因它不起作用:

#include <stdio.h>
#include <Foundation/NSAutoReleasePool.h>
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSPort.h>

@interface MyDelegate : NSObject <NSPortDelegate> {
}
- (void)handlePortMessage:(NSPortMessage *)portMessage;
@end

@implementation MyDelegate
- (void)handlePortMessage:(NSPortMessage *)portMessage {
    printf("Haiz\n");
}
@end

int
main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSSocketPort *server = [NSSocketPort alloc];
    MyDelegate *foo = [MyDelegate alloc];
    [server initWithTCPPort: 1234];
    [server setDelegate: foo];
    [[NSRunLoop mainRunLoop] addPort: server forMode: NSDefaultRunLoopMode];
    [[NSRunLoop mainRunLoop] run];
    [pool release];
    return 0;
}

该应用程序应该侦听本地主机端口 1234,并且每当有人连接到服务器或向服务器发送数据时,该应用程序应该打印“海兹”在控制台上。然而该应用程序什么也不做。套接字已创建,我可以远程登录到端口 1234,但应用程序不会在控制台上打印任何内容。

我做错了什么?

I'm writing a Cocoa app. There is a socket in the application, and whenever the socket becomes readable I want to read data from the socket, process the data, and update the user interface accordingly. I want to integrate the read event check in the main loop, i.e. I want to attach the socket to the main loop and have the main loop call a callback whenever that socket becomes readable.

I've written a test application, but for some reason it doesn't work:

#include <stdio.h>
#include <Foundation/NSAutoReleasePool.h>
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSPort.h>

@interface MyDelegate : NSObject <NSPortDelegate> {
}
- (void)handlePortMessage:(NSPortMessage *)portMessage;
@end

@implementation MyDelegate
- (void)handlePortMessage:(NSPortMessage *)portMessage {
    printf("Haiz\n");
}
@end

int
main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSSocketPort *server = [NSSocketPort alloc];
    MyDelegate *foo = [MyDelegate alloc];
    [server initWithTCPPort: 1234];
    [server setDelegate: foo];
    [[NSRunLoop mainRunLoop] addPort: server forMode: NSDefaultRunLoopMode];
    [[NSRunLoop mainRunLoop] run];
    [pool release];
    return 0;
}

The app is supposed to listen on localhost port 1234, and whenever someone connects to the server or sends data to the server, the app is supposed to print "Haiz" on the console. However the app does nothing at all. The socket is created and I can telnet to port 1234, but the app doesn't print anything to the console.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

三生一梦 2024-08-28 22:27:41

从文档中:

NSSocketPort 对象可以用作分布式对象连接的端点。

那不是你在这里做的事。

您想要 NSFileHandle来自 BSD 套接字 API 的套接字文件描述符,或 一个 CFSocket。这将使您将套接字置于运行循环中。

From the documentation:

An NSSocketPort object can be used as an endpoint for distributed object connections.

That's not what you're doing here.

You want either NSFileHandle around a socket file descriptor from the BSD sockets API, or a CFSocket. This will let you put the socket on the run loop.

自此以后,行同陌路 2024-08-28 22:27:41

您希望按照您正在执行的方式使用 NSSocketPort,然后创建一个 NSFileHandle 来接受套接字上的连接。您可以像您期望的那样在主线程上获得回调,首先是新连接,然后是这些连接上的数据。使用此 O'Reilly 文章 并忽略 HTTP 内容。

You want to use NSSocketPort in the way you're doing, but then create a NSFileHandle to accept connections on the socket. You can get callbacks on the main thread just like you're expecting, first for new connections, and then for data on those connections. Use this O'Reilly article and just ignore the HTTP stuff.

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