I'm writing a C program that connects to another machine over a TCP socket and reads newline-delimited text over that TCP connection.
I use poll
to check whether data is available on the file descriptor associated with the socket, and then I read characters into a buffer until I get a newline. However, to make that character-by-character read efficient, I'm using a stdio FILE
instead of using the read system call.
When more than one short line of input arrives over the socket quickly, my current approach has a bug. When I start reading characters, stdio
buffers several lines of data in userspace. Once I've read one line and processed it, I then poll
the socket file descriptor again to determine whether there is more data to read.
Unfortunately, that poll
(and fstat
, and every other method I know to get the number of bytes in a file) don't know about any leftover data that is buffered in userspace as part of the FILE
. This results in my program blocking on that poll
when it should be consuming data that has been buffered into userspace.
How can I check how much data is buffered in userspace? The specs specifically tell you not to rely on setvbuf
for this purpose (the representation format is undefined), so I'm hoping for another option.
Right now, it seems like my best option is to implement my own userspace buffering where I have control over this, but I wanted to check before going down that road.
EDIT:
Some comments did provide a way to test if there is at least one character available by setting the file to be nonblocking and trying to fgetc
/fungetc
a single character, but this can't tell you how many bytes are available.