Debarshi Ray wrote:
> size_t
> recvbuf (int sockfd, void **buffer, size_t *size)
> {
>   size_t block = BUFSIZ;
>   size_t count = 0;
>   ssize_t nread;
> 
>   if (*buffer == NULL)
>     *size = block;
> 
>   for (;;)
>     {
>       *buffer = xrealloc (*buffer, *size);
>       nread = recv (sockfd, *buffer + count, block, 0);
> 
>       if (nread == -1)
>         error (EXIT_FAILURE, errno, "recv");
> 
>       count += nread;
> 
>       if (nread < block)
>         break;
>       else
>         {
>           block = (*size / 2 > BUFSIZ) ? *size / 2 : BUFSIZ;
>           *size += block;
>         }
>     }
> 
>   return count;
> }

What's the use-case of this function? You said that you want to "safely"
read from sockfd, but can you explain what you mean by that?

I don't see the point in performing the loop here, since
  - for SOCK_DGRAM sockets (e.g. UDP) the parts of the first message that
    don't fit in BUFSIZ bytes will be discarded; performing more recv()
    calls afterwards will not recover them, but will read a different packet
    each,
  - for SOCK_STREAM sockets (e.g. TCP) your function is reading all that is
    currently available. What's the point? Why not return a block of BUFSIZ
    bytes to the caller, then in the next call another block of BUFSIZ bytes
    and so on? BUFSIZ is large enough that this should hardly have a measurable
    effect on speed.

Bruno



Reply via email to