Receiving Data from NSInputStream in Swift
Solution 1
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.
Solution 2
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.
Related videos on Youtube
hoedding
Student of information technology at University of Karlsruhe.
Updated on September 14, 2022Comments
-
hoedding over 1 year
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 found a solution with handling the NSStreamEvent, which i have tried.
First of all my function for initializing the connection:
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() }
This part is working. I have set the delegates to self, so i should be able to handle the NSStreamEvents in this class.
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.") } }
In my understanding everytime when some data arrives, it should print "HasBytesAvaible", but it printed "unknown." everytime. So i played around a bit and it worked with this:
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.") } }
It's working this way, but i wanted to ask you whether this is the right way ? What is the best practice at this point ?
UPDATE I worked on it again a few weeks ago and figured out my mistakes. So here is the working code.
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 }
-
hoedding about 9 yearsI figured it out a few weeks ago. I will accept your answer here, because you're right. This was my mistake.
-
Alex about 9 yearsGlad you sorted it – I only realised the date of your question after I'd answered!
-
Shrikant K over 8 years@Alexander this solution will work for multiple response at a time ? in (UPDATE)
-
Alex about 8 years@K.Shrikant I think you'll get better answers if you create a new question