从Swift中的NSInputStream接收数据

时间:2014-10-14 12:36:10

标签: swift inputstream nsstream

我尝试在Swift中使用NSOutputStream和NSInputStream发送和接收数据。发送数据运行良好,但我对接收有一些疑问。

我找到了一个处理NSStreamEvent的解决方案,我试过了。

首先我的初始化连接函数:

func initNetworkCommunication(){  
    var host : CFString = "127.0.0.1"
    var port : UInt32 = 7001
    var readstream : Unmanaged<CFReadStream>?
    var writestream : Unmanaged<CFWriteStream>?
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readstream, &writestream)

    inputstream = readstream!.takeRetainedValue()
    outputstream = writestream!.takeRetainedValue()

    inputstream.delegate = self
    outputstream.delegate = self


    inputstream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
    outputstream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    inputstream.open()
    outputstream.open()   
}

这部分正在运作。我已将委托设置为self,因此我应该能够处理此类中的NSStreamEvents。

func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
    switch (eventCode){
    case NSStreamEvent.OpenCompleted:
        NSLog("Stream opened")
        break
    case NSStreamEvent.HasBytesAvailable:
        NSLog("HasBytesAvailable")
        break
    case NSStreamEvent.ErrorOccurred:
         NSLog("ErrorOccurred")
        break
    case NSStreamEvent.EndEncountered:
        NSLog("EndEncountered")
        break
    default:
        NSLog("unknown.")
    }
}

根据我的理解,每当某些数据到达时,它应该打印“HasBytesAvaible”,但它打印“未知”。每次。所以我玩了一下,它有效:

func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {

    var buffer = [UInt8](count: 4096, repeatedValue: 0)
    while (inputstream.hasBytesAvailable){
        let result: Int = inputstream.read(&buffer, maxLength: buffer.count)
    }

    switch (eventCode){
    case NSStreamEvent.OpenCompleted:
        NSLog("Stream opened")
        break
    case NSStreamEvent.HasBytesAvailable:
        NSLog("HasBytesAvailable")
        var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding)
        NSLog("output: %@", output)
        receiveMessage(output) //only adds the message to an array
        break
    case NSStreamEvent.ErrorOccurred:
        NSLog("ErrorOccurred")
        break
    case NSStreamEvent.EndEncountered:
          NSLog("EndEncountered")
        break
    default:
        NSLog("unknown.")
    }
}

这是以这种方式工作,但我想问你这是否是正确的方法?目前最好的做法是什么?

更新 几个星期前我再次研究过它并弄清楚了我的错误。所以这是工作代码。

func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
        switch (eventCode){
    case NSStreamEvent.ErrorOccurred:
        NSLog("ErrorOccurred")
        break
    case NSStreamEvent.EndEncountered:
        NSLog("EndEncountered")
        break
    case NSStreamEvent.None:
        NSLog("None")
        break
    case NSStreamEvent.HasBytesAvailable:
        NSLog("HasBytesAvaible")
        var buffer = [UInt8](count: 4096, repeatedValue: 0)
        if ( aStream == inputstream){

            while (inputstream.hasBytesAvailable){
                var len = inputstream.read(&buffer, maxLength: buffer.count) 
                if(len > 0){
                    var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding) 
                    if (output != ""){
                        NSLog("server said: %@", output!)
                    }
                }
            } 
        }
        break
    case NSStreamEvent.allZeros:
        NSLog("allZeros")
        break
    case NSStreamEvent.OpenCompleted:
        NSLog("OpenCompleted")
        break
    case NSStreamEvent.HasSpaceAvailable:
        NSLog("HasSpaceAvailable")
        break
  }

2 个答案:

答案 0 :(得分:3)

您错过了事件hasSpaceAvailable,我预计事件发生时会发生“未知&#34;”。它告诉您它已准备好接收更多数据。

通常,我避免在switch语句中使用default作为枚举,因为编译器会告诉你何时错过了什么。

答案 1 :(得分:3)

我正在使用hoedding编写的代码,并发现了一个错误。这一行:

var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding)

应替换为:

var output = NSString(bytes: &buffer, length: len, encoding: NSUTF8StringEncoding)

你必须只在output var中获取服务器返回的字符数,而不是缓冲区的全长,否则你将从以前的请求中获得垃圾。