[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [RFE] function to read a file descriptor
From: |
Ralf Wildenhues |
Subject: |
Re: [RFE] function to read a file descriptor |
Date: |
Thu, 21 Aug 2008 07:31:50 +0200 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
* Debarshi Ray wrote on Wed, Aug 20, 2008 at 09:40:13PM CEST:
> Oops sorry! This one is hopefully better.
I don't think you want to recv more than BUFSIZ bytes; and then you
shouldn't need to call realloc for every recv. Only when the next recv
answer will not fit in the current buffer size any more, do you need to
allocate more memory.
Also, if this is to be a library function, it should not call xrealloc
nor error at all but indicate an error to the calling function.
Cheers,
Ralf
> 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;
> }
- Re: [RFE] function to read a file descriptor, (continued)
- Re: [RFE] function to read a file descriptor, Bruno Haible, 2008/08/18
- Re: [RFE] function to read a file descriptor, Debarshi Ray, 2008/08/18
- Re: [RFE] function to read a file descriptor, Ralf Wildenhues, 2008/08/19
- Re: [RFE] function to read a file descriptor, Debarshi Ray, 2008/08/19
- Re: [RFE] function to read a file descriptor, Ralf Wildenhues, 2008/08/20
- Re: [RFE] function to read a file descriptor, Debarshi Ray, 2008/08/20
- Re: [RFE] function to read a file descriptor, Bruno Haible, 2008/08/20
- Re: [RFE] function to read a file descriptor, Debarshi Ray, 2008/08/21
- Re: [RFE] function to read a file descriptor, Bruno Haible, 2008/08/21
- Re: [RFE] function to read a file descriptor,
Ralf Wildenhues <=