Re: NSInputStream created from NSData - expected it to close at end of data, did not happen
Re: NSInputStream created from NSData - expected it to close at end of data, did not happen
- Subject: Re: NSInputStream created from NSData - expected it to close at end of data, did not happen
- From: email@hidden
- Date: Sat, 11 Jun 2011 12:06:49 +0200
Am 11.06.2011 um 05:01 schrieb Ken Thomases:
> On Jun 10, 2011, at 7:35 PM, email@hidden wrote:
>
>> I want the TCPServer to return some data block to each client that connects. I basically just want to put the bytes of the NSData object one after another on the outputstream that is connected to the socket.
>> Problem: I would have to save the position in the data for each client to return the correct bytes to the client. At that point I thought that's what streams are for. To save your current position in a blob of bytes that should be read in order.
>> Solution: Create a new NSInputStream (independent from the input stream of the TCPServer. That's why I talked about the data input stream, the input stream that reads the bytes of the data object) for each incoming connection. Guessing that after the NSInputStream based on the data is open it won't have any problems reading the data I only wait for the output stream to have space available and whenever it has I read some bytes from the data input stream and write them on the server output stream. I completely ignore the server input stream (except for logging the received data, when searching for bugs).
>
> When the client can accept data, you can just read from the input stream. If you get a zero-length read, then the input stream is exhausted.
> You may not get an event for that.
Yes that's my problem. And that's why I close the stream now as soon as it does not have any more bytes available.
> In fact, the input stream need not be scheduled on a run loop and, if it's not, the delegate won't get any calls.
But I did schedule it in a run loop and I do get NSStreamEventOpenCompleted and NSStreamEventHasBytesAvailable. I just don't get and NSStreamEventEndEncountered. Which would not be a big problem for the datastream. But:
> The events and the delegate are for asynchronous events in the stream, and there are none for a stream based on a data object.
I also have the streams connected to the TCP-connection. When I close the datastream (and I checked with the debugger, that I actually do) I also close the output stream of the TCP connection on the server side. At that point I would expect the input stream connected to the socket on the client side to be closed as well. I don't know whether it is closed at that point, but at least I do not get an NSStreamEventEndEncountered event on the client side.
And I can't think of a reason for that.
The TCP connection of the client and server is local (127.0.0.1 as host address for the client, with some 3xxxx port) and the data transfer seems to work, I checked with the debugger, that I get the bytes I expect to receive. The only thing that is missing is the NSStreamEvenEndEncountered.
That's why I think the problem is either stream related or related to my (wrong?) understanding of streams.
Best regards,
Joachim
_______________________________________________
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