I need to read from an AF_UNIX socket to a buffer using the function read
from C, but I don't know the buffer size.
I think the best way is to read N
bytes until the read returns 0
(no more writers in the socket). Is this correct? Is there a way to guess the size of the buffer being written on the socket?
I was thinking that a socket is a special file. Opening the file in binary mode and getting the size would help me in knowing the correct size to give to the buffer?
I'm a very new to C, so please keep that in mind.
One common way is to use ioctl(..)
to query FIONREAD
of the socket which will return how much data is available.
int len = 0;
ioctl(sock, FIONREAD, &len);
if (len > 0) {
len = read(sock, buffer, len);
}