Reputation: 3460
Okay so, this is a general question about reads from a socket.
I'm looking at an Objective C app that someone built which uses a TCP socket library and sets up a handler for "didReadData" which takes the data and sends it to a JSON parser.
Now... am I right when I say this is basically totally wrong? Isn't it the case that socket reads could return some, all, or none of the data, depending on lots of factors like network latency? So isn't it the case that you have to keep reading from the socket until you get to the end of the data -- however that is delimited -- and then parse it? So, for JSON for example, if you were sending it over HTTP you'd send a content-length header which would tell the server (or client) when it can stop reading from the socket? So -- if you're not using http and using a raw socket, you'd have to have some other delimiter or mechanism of determining where the end of the message is.
I am wondering if this particular app has only been tested in situations where client and server are on the same host -- so I'm wondering if the programmer thinks that it works because, just coincidentally, the first read has always up until now returned all the data.
Upvotes: 3
Views: 349
Reputation: 90531
It is possible to implement a streaming parser. I don't know if the parser in use in the code in question is such, but it's not impossible.
For example, Apple's NSXMLParser
allows for streaming parsing.
(Edited to remove mention of NSJSONSerialization
because, although it can read from a stream, it does so synchronously.)
Upvotes: 1
Reputation: 465
You are right.
The stream is endless and a message has to be marked as such by a delimiter. If the app you are testing relies on the stream object to identify messages without a delimiter this will be the problem.
The question is if this functionality is somehow integrated in the socket library. What library are you using?
Upvotes: 2