1

I can use select() to determine if a call to recv() would block, but once I've determined that their are bytes to be read, is their a way to query how many bytes are currently available before I actually call recv()?

dicroce
  • 45,396
  • 28
  • 101
  • 140

2 Answers2

5

If your OS provides it (and most do), you can use ioctl(..,FIONREAD,..):

int get_n_readable_bytes(int fd) {
    int n = -1;
    if (ioctl(fd, FIONREAD, &n) < 0) {
        perror("ioctl failed");
        return -1;
    }
    return n;
}

Windows provides an analogous ioctlsocket(..,FIONREAD,..), which expects a pointer to unsigned long:

unsigned long get_n_readable_bytes(SOCKET sock) {
    unsigned long n = -1;
   if (ioctlsocket(sock, FIONREAD, &n) < 0) {
       /* look in WSAGetLastError() for the error code */
       return 0;
   }
   return n;
}

The ioctl call should work on sockets and some other fds, though not on all fds. I believe that it works fine with TCP sockets on nearly any free unix-like OS you are likely to use. Its semantics are a little different for UDP sockets: for them, it tells you the number of bytes in the next datagram.

The ioctlsocket call on Windows will (obviously) only work on sockets.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
nickm
  • 486
  • 2
  • 2
2

No, a protocol needs to determine that. For example:

  • If you use fixed-size messages then you know you need to read X bytes.
  • You could read a message header that indicates X bytes to read.
  • You could read until a terminal character / sequence is found.
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • Actually, even if you could determine the bytes available, you'd still need a protocol to know how to interpret those bytes... I was asking for a very specific purpose... – dicroce Oct 14 '10 at 14:29
  • Fair enough; just trying to expand on the short answer of "No". – Justin Ethier Oct 14 '10 at 14:35