(Phobos - SocketStream) Am I doing something wrong or is this a bug?
Steven Schveighoffer
schveiguy at yahoo.com
Wed Nov 4 03:12:40 PST 2009
On Tue, 03 Nov 2009 20:05:17 -0500, Zane <zane.sims at gmail.com> wrote:
> I assumed since the underlying TcpSocket was blocking, then calling read
> through the stream would also block....dunno. Anyway, so if I were to
> use a loop, how could I do this with read? The size of the read buffer
> must be initialized before the reading takes place, however, I do not
> know how much will be read for the next "chunk". If I am to receive
> these in arbitrarily sized chunks for concatenation, I don't see a
> sensible way of constructing a loop. Example?
Your interpretation of blocking sockets is incorrect. A socket blocks on a
read only if *no* data is available. If any data is available, it reads
as much as it can and returns.
A non blocking socket returns immediately, even if no data is available
(usually returning an error like EAGAIN).
A simple loop (keep in mind, I don't know phobos, and I didn't look up the
exact usage):
// ubyte[] buf is the array we want to fill, predetermined length
ubyte[] tmp = buf[];
while(tmp.length > 0)
{
auto bytesread = socket.read(tmp);
if(bytesread == 0) break; // EOF (also should check for error here if
necessary)
tmp = tmp[bytesread..$];
}
This kind of thing can *easily* be put into a wrapper function.
-Steve
More information about the Digitalmars-d
mailing list