In every example and discussion I run across in the context of BSD socket programming, it seems that the recommended way to set a file descriptor to nonblocking I/O mode is using the O_NONBLOCK
flag to fcntl()
, e.g.
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
I've been doing network programming in UNIX for over ten years, and have always used the FIONBIO ioctl()
call to do this:
int opt = 1;
ioctl(fd, FIONBIO, &opt);
Never really gave much thought to why. Just learned it that way.
Does anyone have any commentary on the possible respective merits of one or the other? I imagine the portability locus differs somewhat, but do not know to what extent as ioctl_list(2)
doesn't speak to that aspect of individual ioctl
methods.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…