在客户端,我们可以使用 CFStreamCreatePairWithSocketToHost 来获取 ReadStream 和 WriteStream。
服务器得到clientSocket后,我们如何得到ReadStream和WriteStream?
import UIKit
class Connection: NSObject, NSStreamDelegate {
var inputStream: NSInputStream!
var outputStream: NSOutputStream!
func connect() {
var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(nil, "193.10.1.22", 17, &readStream, &writeStream)
self.inputStream = readStream!.takeRetainedValue()
self.outputStream = writeStream!.takeRetainedValue()
self.inputStream.delegate = self
self.outputStream.delegate = self
self.inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
self.outputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
self.inputStream.open()
self.outputStream.open()
}
}