NSInputStream created from NSData - expected it to close at end of data, did not happen
NSInputStream created from NSData - expected it to close at end of data, did not happen
- Subject: NSInputStream created from NSData - expected it to close at end of data, did not happen
- From: email@hidden
- Date: Thu, 02 Jun 2011 11:10:29 +0200
Hi all,
I wanted to have a very basic TCP server that could return arbitrary bytes when a client connected to it to test whether some protocol (on top of TCP) code I wrote works as expected. To do so I adapted the CocoaEcho example (http://developer.apple.com/library/mac/samplecode/CocoaEcho/Introduction/Intro.html ). I took the TCPServer part as is and created a new subclass that is initialized with an NSData object to be sent to all connecting clients.
For every new connection I create a new NSInputStream from the NSData object for this server using "inputDataStream = [NSInputStream inputStreamWithData:theData]" and associate that with the output stream for that connection.
Whenever the output stream has space available I read bytes from the inputDataStream (just assuming that bytes will be available, since it reads from an NSData object in memory), that seems to work so far.
When the inputDataStream is closed I also close the output stream for that connection.
But the inputDataStream seems to never get closed. I would have expected it to be closed when it reaches the end of the underlying data.
I have a breakpoint in the stream:handleEvent: method and checked that I do read all the bytes from the inputDataStream. But after I read the last byte from the inputDataStream (after which [inputDataStream hasBytesAvailable] returns false) the stream:handleEvent method is not called again (although the run loop still runs).
Can you explain to me why the stream is not closed in this case?
I could probably just close the inputDataStream after I read bytes from it and it does not have any more bytes available assuming that this means I reached the end of the NSData object, but I don't really like that either.
Best regards,
Joachim
P.S. code attached if you think you need it
Attachment:
VCDataBlocksServer.m
Description: Binary data
Attachment:
VCDataBlocksServer.h
Description: Binary data
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden