如何使用NSStreamEventOpenCompleted回调NSStreamDelegate?

49

我一直在处理一个NSStreamDelegate,我已经实现了回调函数,并像这样初始化输入和输出流...

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStringRef host = CFSTR("74.125.224.72");
    UInt32 port = 2270;

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &inputStream, &writeStream);

    if (writeStream && inputStream) {

        inputStream = (__bridge  NSInputStream *)readStream;
        [inputStream setDelegate:self];
        [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [inputStream open];

        outputStream = (__bridge  NSOutputStream *)writeStream;
        [outputStream setDelegate:self];
        [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [outputStream open];
     }
即使已经打开了两个流,callback(stream:(NSStream *)theStream handleEvent:)回调函数对于这两个流也没有以NSStreamEventOpenCompleted的事件被调用。有人能帮我看看我错在哪里吗?或者NSStreamEventOpenCompleted不会被调用的可能性是什么?我在文档中看到过,如果打开失败,它不会调用此操作,那么为什么流的打开会失败呢?有任何想法吗?谢谢你的帮助。

2
尝试将它们设置在主RunLoop中,[NSRunLoop mainRunLoop] - itsji10dra
你是在主线程上还是在一些后台线程上执行这个操作? - Sven
NSLog(@"状态:%@",(NSString*) [outputStream streamError]); 检查输入流是否相同。可能是http://stackoverflow.com/questions/12238828/how-to-use-delegate-in-nsstream的重复内容最好使用https://github.com/robbiehanson/CocoaAsyncSocket。 - Pandey_Laxman
3个回答

0

我使用非常相似的代码,它对我来说运行良好。 尝试下面的代码。

   NSString* host = @"192.168.2.105";
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    UInt32 port = 8008;

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)(host), port, &readStream, &writeStream);

    if (writeStream && readStream) {

        self.InputStream = (__bridge  NSInputStream *)readStream;
        [self.InputStream setDelegate:self];
        [self.InputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [self.InputStream open];

        self.OutputStream = (__bridge  NSOutputStream *)writeStream;
        [self.OutputStream setDelegate:self];
        [self.OutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [self.OutputStream open];
    }

如果这个方法对你不起作用,我可以给你发送一个小应用程序,它实现了TCP客户端和服务器的示例。

0

如果它在新的NSThread中运行,请确保在流设置之后启动线程的运行循环,例如CFRunLoopRun();


0
使用Stream类来初始化和打开输入输出流,并处理流事件:
import Foundation
// Initialize input and output streams

var inputStream: InputStream?

var outputStream: OutputStream?

let host = "74.125.224.72"
let port: UInt32 = 2270

    
// Create streams for the given host and port
Stream.getStreamsToHost(withName: host, port: Int(port), inputStream: &inputStream, outputStream: &outputStream)


// Check if both streams are successfully initialized
if let inputStream = inputStream, let outputStream = outputStream {
    // Set the delegate to handle stream events
    inputStream.delegate = self
    outputStream.delegate = self


    // Schedule streams in the current run loop for default mode
    inputStream.schedule(in: .current, forMode: .default)
    outputStream.schedule(in: .current, forMode: .default)

    // Open both input and output streams
    inputStream.open()
    outputStream.open()
}

请在您的类中遵循NSStreamDelegate协议来处理流事件。以下代码演示了如何处理这些事件:
class YourClassName: NSStreamDelegate {
// Other class methods and properties here...

// Handle stream events
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
    switch eventCode {
    case .openCompleted:
        // Stream opened successfully
        print("Stream opened successfully.")
    case .hasBytesAvailable:
        // Handle incoming data on the input stream
        // ...
    case .hasSpaceAvailable:
        // Handle available space on the output stream
        // ...
    case .errorOccurred:
        // Handle stream error
        print("Stream error occurred.")
    case .endEncountered:
        // Handle end of stream
        print("End of stream encountered.")
    default:
        break
    }
}

// Other methods and code here...

}

通过实施上述代码,您将能够在Swift中创建和打开输入输出流并处理流事件。请记住将YourClassName替换为您的类的实际名称,然后您就可以在应用程序中使用流了!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接