(Phobos - SocketStream) Am I doing something wrong or is this a bug?

BCS none at anon.com
Wed Nov 4 09:20:27 PST 2009


Hello Zane,

> BCS Wrote:
> 
>> Hello Zane,
>> 
>>> BCS Wrote:
>>> 
>>>> Hello Zane,
>>>> 
>>>>> While trying to learn sockets in Phobos, I ran into a problem.  I
>>>>> was trying to download the google logo via an HTTP GET.  when I do
>>>>> a socketStream.read, the returned "bytes read" value indicates
>>>>> that less bytes were read in than were available (4097 bytes to be
>>>>> exact).  The google logo is 8558 bytes in size as indicated by the
>>>>> HTTP headers received.  Here is my code:
>>>>> 
>>>> The network connection might not have all the data yet. Try calling
>>>> it in a loop kind of like your header reading code.
>>>> 
>>> I do not see how this can be the case.  Per the Phobos spec for
>>> InputStream, "Read a block of data big enough to fill the given
>>> array buffer. "  This sounds to me like it should do just that, fill
>>> my array....and not return until done.  readLine needs to be looped
>>> because it only reads up to a carriage return / newline combination
>>> (and there are multiple lines).  Can anyone confirm that my
>>> speculations are correct?  Otherwise, the documentation seems
>>> misleading and I will need help with a correct implementation.
>>> 
>> If I were you, I'd just try the looped version because there might be
>> a bug. I seem to recall that the stream stuff in some places gets
>> defined to return on out of data conditions. (BTW this would be
>> consistent with the liked named "read" function from posix.)
>> 
>> Looking into the code (Stream.read() and SocketStream.readBlock()) it
>> doesn't block if there isn't enough data.
>> 
>> If this is a code bug or a documentation deficiency is another
>> question.
>> 
> 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?

ubyte[] buffer = new ubyte[size];
auto window = buffer;

while(!ss.eof() && window.length > 0)
{
   int i = ss.read(windows);
   window = window[i..$];
}


that, or take a look at readExect.





More information about the Digitalmars-d mailing list