NSStream和套接字,未调用NSStreamDelegate方法 [英] NSStream and Sockets, NSStreamDelegate methods not being called

查看:192
本文介绍了NSStream和套接字,未调用NSStreamDelegate方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照指南

I've followed the guide Setting Up Socket Streams and have effectively duplicated that code in my class. No matter what I try the delegate methods just don't seem to get called.

在头文件中(基本上):

In the header file I have (basically):

@interface myClass : NSObject <NSStreamDelegate> {
    NSInputStream *inputStream;
    NSOutputStream *outputStream;
}
- (void)connect;
@end;

连接方法:

- (void)connect {
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)@"host.example.com", 1234, &readStream, &writeStream);

    inputStream = (NSInputStream *)readStream;
    outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];
}

还尝试使用CFStreamCreatePairWithSocketToCFHost()[NSStream getStreamsToHost:port:inputStream:outputStream:-所有结果都完全相同.

Also tried using CFStreamCreatePairWithSocketToCFHost() and [NSStream getStreamsToHost:port:inputStream:outputStream: - all with exactly the same result.

我在connect方法的开头设置了一个断点,逐步执行每一行,并且每个指针都有效,并且似乎指向正确的对象.

I've set a breakpoint at the beginning of the connect method, stepped through every line and every pointer is valid and seems to point to the correct object.

在GDB中,在setDelegate调用之后,po [inputStream delegate]会按预期打印<myClass: 0x136380>,因此它已正确设置了委托.

In GDB, after the setDelegate calls, po [inputStream delegate] prints <myClass: 0x136380> as expected, so it has set the delegate correctly.

对于我一生,我无法弄清为什么它拒绝在我的课程中调用stream:handleEvent:方法:

For the life of me I can't work out why it refuses to call the stream:handleEvent: method on my class:

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
    NSLog(@"got an event");
}

希望我错过了一些非常简单和明显的内容,第二双眼睛可以发现我的错误.

Hopefully I've missed something really simple and obvious and a second pair of eyes can spot my mistake.

在此先感谢所有有耐心并花时间阅读本文的人!

Thanks in advance to anyone who has the patience and taken the time to read this far!

推荐答案

在这样的行中:

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

我没有使用[NSRunLoop currentRunLoop],而是将其更改为[NSRunLoop mainRunLoop].

Instead of using [NSRunLoop currentRunLoop] I changed it to [NSRunLoop mainRunLoop].

之所以不起作用,是因为我是通过+[NSThread detachNewThreadSelector:toTarget:withObject:]在后台线程中设置套接字的.

The reason this did not work is because I was setting up the sockets in a background thread via +[NSThread detachNewThreadSelector:toTarget:withObject:].

通过这种方式创建了一个新的运行循环,该循环在阅读

Doing it that way created a new run loop, which after reading the run loop developer documentation I discovered that you need to tell the NSRunLoop to run manually.

在与主线程相同的运行循环中运行它可以提高性能,尽管我可以通过编写包装类并在后台线程上运行所有网络I/O来挤出更多性能.

Running it in the same run loop as the main thread was fine on performance, though I was able to squeeze a bit more performance out by writing a wrapper class and running all network I/O on a background thread.

这篇关于NSStream和套接字,未调用NSStreamDelegate方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆