`Socket.receive` providing arbitrary packet sizes and hanging without sending EOF

Ali Çehreli acehreli at yahoo.com
Thu Dec 14 00:09:39 UTC 2017


On 12/13/2017 11:39 AM, Unazed Spectaculum wrote:
 > ubyte[] receiveBytes(T)(T socket, size_t receiveCount)
 > {
 >      ubyte[] buffer = new ubyte[receiveCount];
 >      size_t count = socket.receive(buffer);

Don't trust code you find on newsgroups. :o) You have to check the 
returned value first. According to documentation, it can return 
Socket.ERROR:

   https://dlang.org/phobos/std_socket.html#.Socket.receive

 > there is always a superfluous chunk
 > which is awaiting data.

Can you show with complete code? Perhaps the stream is in blocking mode?

 > No matter what way I try; my code doesn't seem to know when to quit
 > regardless of the check. Also for the arbitrary packet sizes, I would've
 > expected that if I received N bytes X times, the first X-1 times would
 > be perfectly N not some unusual integer.
 > Simply put, say I'm receiving 1024 bytes 5 times. The length of each
 > item on the stack looks like:
 >
 > [720,
 >   490,
 >   1024,
 >   103
 > ]

Posix read(2) man page says

"It is not an error if this number is smaller than the number of bytes
requested; this may happen for example because fewer bytes are actually
available right now (maybe because we were close to end-of-file, or because
we are reading from a pipe, or from a terminal), or because read() was
interrupted by a signal."

Ali



More information about the Digitalmars-d-learn mailing list