gpt4 book ai didi

Android/Linux ioctl FIONREAD 失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:05 25 4
gpt4 key购买 nike

我正在将一些(套接字相关的)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 的 strace:

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。参见 tcp(7) :

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>.

关于Android/Linux ioctl FIONREAD 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com