OS X:如何使用 NSRunLoop 观察套接字读取事件?
我正在写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从文档中:
那不是你在这里做的事。
您想要
NSFileHandle来自 BSD 套接字 API 的套接字文件描述符,或一个 CFSocket。这将使您将套接字置于运行循环中。From the documentation:
That's not what you're doing here.
You want
either NSFileHandle around a socket file descriptor from the BSD sockets API, ora CFSocket. This will let you put the socket on the run loop.您希望按照您正在执行的方式使用 NSSocketPort,然后创建一个 NSFileHandle 来接受套接字上的连接。您可以像您期望的那样在主线程上获得回调,首先是新连接,然后是这些连接上的数据。使用此 O'Reilly 文章 并忽略 HTTP 内容。
You want to use
NSSocketPort
in the way you're doing, but then create aNSFileHandle
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.