[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [RFE] function to read a file descriptor
From: |
Bruno Haible |
Subject: |
Re: [RFE] function to read a file descriptor |
Date: |
Thu, 21 Aug 2008 00:31:43 +0200 |
User-agent: |
KMail/1.5.4 |
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
- [RFE] function to read a file descriptor, Debarshi Ray, 2008/08/18
- 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 <=
- 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, 2008/08/21