提问人:Newbee 提问时间:9/26/2012 最后编辑:Amal T SNewbee 更新时间:7/26/2023 访问量:1531
如何使用NSStreamEventOpenCompleted回调NSStreamDelegate?
How to callback NSStreamDelegate with NSStreamEventOpenCompleted?
问:
我一直在研究一个,我已经实现了回调,我已经初始化了输入和输出流。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
感谢您的帮助。
答:
0赞
Guy Kahlon
5/27/2015
#1
我使用非常相似的代码,它对我来说效果很好。 请尝试下面的代码。
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赞
stcui
8/17/2021
#2
如果它在新的 NSThread 中运行,请确保线程的运行循环在流设置后启动,例如 CFRunLoopRun();
0赞
Aseem
7/26/2023
#3
使用 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 协议来处理流事件。下面的代码演示如何处理这些事件:
类 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 替换为类的实际名称,这样您就应该可以处理应用程序中的流了!
评论
[NSRunLoop mainRunLoop]