porting ioctl() calls from unix to linux, error with FIONBIO

maheshg picture maheshg · Mar 7, 2012 · Viewed 23.8k times · Source

i want to use ioctl() to get the number of bytes ready to be read

the way I did it is:

mysocket=socket(....);
ioctl(mysocket, FIONBIO, &zero);    
connect(.....);
ioctl( mysocket, FIONREAD, &numBytes );
read(mysocket, buffer, numBytes);

this was working fine in unix, now i have to port it to linux i keep getting ERROR

error: 'FIONBIO' was not declared in this scope

Is there some header file specific for linux? or 'FIOBIO' doesnt work at all in linux?

I have following headers included:

#include <cstring>
#include <sys/socket.h>
#include <stropts.h>
#include <fcntl.h>
#include <stddef.h>
#include <sys/un.h>
#include <sys/types.h>
#include <stdlib.h> 
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/select.h>
#include <fstream>

and i removed

#include <sys/filio.h>

since it was throwing errors saying sys/filio.h not found

Answer

mfontanini picture mfontanini · Mar 7, 2012

Have you tried including sys/ioctl.h?

This code works for me:

#include <sys/ioctl.h>
#include <stdio.h>

int main() {
    printf("FIONBIO value %d\n", FIONBIO);
}

When I execute grep -R FIONBIO /usr/include, it's found here:

/usr/include/asm-generic/ioctls.h:#define FIONBIO 0x5421