I have two ways to set file descriptor non-blocking.
fcntl(conn_fd, F_SETFD, (fcntl(conn_fd, F_GETFD, 0)|O_NONBLOCK));
or
fcntl(conn_fd, F_SETFD, (fcntl(conn_fd, F_GETFD)|O_NONBLOCK));
and
void setnonblocking(int sock) {
int opt;
opt = fcntl(sock, F_GETFL);
if (opt < 0) {
printf("fcntl(F_GETFL) fail.");
}
opt |= O_NONBLOCK;
if (fcntl(sock, F_SETFL, opt) < 0) {
printf("fcntl(F_SETFL) fail.");
}
}
why the function setnonblocking can set file descriptor non-blocking .but the other can't.I use this when epoll get a new connection.
O_NONBLOCK
is a file status flag, not a file descriptor flag.
Maybe, in your fcntl(conn_fd, F_SETFD, (fcntl(conn_fd, F_GETFD)|O_NONBLOCK));
, you need to change F_GETFD
to F_GETFL
and F_SETFD
to F_SETFL
, as you need to modify the file status flags, not the file descriptor flags.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.