移植ioctl()从unix移植到linux,错误与FIONBIO

mah*_*shg 4 c++ sockets linux ioctl

我想使用ioctl()来获取准备读取的字节数

我这样做的方式是:

mysocket=socket(....);
ioctl(mysocket, FIONBIO, &zero);    
connect(.....);
ioctl( mysocket, FIONREAD, &numBytes );
read(mysocket, buffer, numBytes);
Run Code Online (Sandbox Code Playgroud)

这在unix中工作正常,现在我必须将它移植到linux我一直得到ERROR

错误:未在此范围内声明'FIONBIO'

是否有一些特定于Linux的头文件?或者'FIOBIO'在linux中根本不起作用?

我有以下标题包括:

#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>
Run Code Online (Sandbox Code Playgroud)

我删除了

#include <sys/filio.h>
Run Code Online (Sandbox Code Playgroud)

因为它抛出了错误,说没有找到sys/filio.h

mfo*_*ini 18

你试过包含sys/ioctl.h吗?

这段代码适合我:

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

int main() {
    printf("FIONBIO value %d\n", FIONBIO);
}
Run Code Online (Sandbox Code Playgroud)

当我执行时grep -R FIONBIO /usr/include,它在这里找到:

/usr/include/asm-generic/ioctls.h:#define FIONBIO 0x5421
Run Code Online (Sandbox Code Playgroud)