SIDEBAR
»
S
I
D
E
B
A
R
«
Available socket bytes
May 13th, 2008 by dholm

Ever wondered how to find out how many bytes can be read from a socket? I did and it is really simple although possibly not portable.

ssize_t socket_get_available(int socket_fd)
{
        int available = -1;
        assert(0 <= socket_fd);

        if (ioctl(socket_fd, FIONREAD, &available) < 0) {
                return -1;
        }

        return (ssize_t) available;
}

4 Responses  
  • Martin writes:
    May 24th, 200821:32at

    Eeek! man ioctl_list:

    “0x0000541B FIONREAD int *”

    That you might be lucky and it happens to work on your platform is no excuse. ;)

  • Martin writes:
    May 24th, 200821:39at

    Also, since you’re returning -1 to indicate an error I think you want ssize_t. That is, if I remember correctly (take e.g. ssize_t read(…) vs size_t fread(…)).

  • Martin writes:
    May 24th, 200821:43at

    Hi btw! :)

  • dholm writes:
    May 26th, 200807:43at

    Long time, no see!
    Thanks for the suggestions! I fixed the implementation, apparently I don’t have the ioctl_list man page. ;)


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

SIDEBAR
»
S
I
D
E
B
A
R
«
»  Substance:WordPress   »  Style:Ahren Ahimsa