Android/Linux ioctl FIONREAD失败

Android/Linux ioctl FIONREAD失败,android,linux,windows,porting,Android,Linux,Windows,Porting,我正在将一些(与套接字相关的)Windows C代码移植到Linux/Android的过程中,我遇到了ioctl命令的问题: unsigned long u; if(sockfd != -1 && !ioctl(sockfd, FIONREAD, &u)) { return((long) u); } ... // throw exception 当我检查errno时,我看到了EINVAL,但是我不明白为什么调用ioctl失败了。我甚至尝试将u声明

我正在将一些(与套接字相关的)Windows C代码移植到Linux/Android的过程中,我遇到了ioctl命令的问题:

unsigned long   u;

if(sockfd != -1 && !ioctl(sockfd, FIONREAD, &u))

{
    return((long) u);
}
    ...
// throw exception
当我检查errno时,我看到了EINVAL,但是我不明白为什么调用ioctl失败了。我甚至尝试将u声明为int,但仍然失败。我不知道怎么了。这段代码在Windows上运行得非常好(ioctlsocket而不是ioctl)

这里是Linux的一个特例:

socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(9099), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(3, 5)                            = 0
ioctl(3, FIONREAD, [1])                 = -1 EINVAL (Invalid argument)
write(2, "Exception code: 00000503, data: "..., 52Exception code: 00000503, data: 00000000 ((null):0)
) = 52
shutdown(3, 2 /* send and receive */)   = 0
close(3)                                = 0
exit_group(1)                           = ?

Linux不支持侦听套接字上的
FIONREAD/SIOCINQ
。见:

西奥辛克 返回接收缓冲区中排队的未读数据量。这个 套接字不能处于侦听状态,否则将显示错误(EINVAL) 返回。SIOCINQ在中定义。或者,你 可以使用中定义的同义FIONREAD。
简单地忽略EINVAL安全吗? SIOCINQ Returns the amount of queued unread data in the receive buffer. The socket must not be in LISTEN state, otherwise an error (EINVAL) is returned. SIOCINQ is defined in <linux/sockios.h>. Alternatively, you can use the synonymous FIONREAD, defined in <sys/ioctl.h>.