Receiving Data from NSInputStream in Swift

后端 未结 2 2067
既然无缘
既然无缘 2021-01-31 11:15

I try to send and receive data with NSOutputStream and NSInputStream in Swift. The sending of data is working well, but i have some questions about the receiving.

I foun

相关标签:
2条回答
  • 2021-01-31 11:32

    You're missing the event hasSpaceAvailable, which I expect is occurring when it says "unknown". It's telling you that it is ready to receive more data.

    Generally, I avoid using default in switch statements for enums, since the compiler will tell you when you've missed something.

    0 讨论(0)
  • 2021-01-31 11:41

    I am using the code written by hoedding, and noticed a mistake. The line:

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

    should be replaced by:

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

    You have to get in the output var only the number of characters returned by the server, and not the full length of the buffer, or you will get garbage from previous requests.

    0 讨论(0)
提交回复
热议问题