我正在将一些(与套接字相关的)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) = ?
答案 0 :(得分: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>.