Determing the number of bytes ready to be recv()'d
问题 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()? 回答1: 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