简体   繁体   中英

Does FD_ISSET() return the file descriptor which received data

I have the following code

fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
fdr = raw_socket(client_config.ifindex);
if (fd >= 0) FD_SET(fd, &rfds);
if (fdr >= 0) FD_SET(fdr, &rfds);

max_fd = fd > fdr ? fd : fdr;
retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);

if (FD_ISSET(fd, &rfds)) {
    ....
} else if (FD_ISSET(fdr, &rfds)) {
    ....
}

If we receive data from the fd socket, does the FD_ISSET(fd, &rfds) returns true and FD_ISSET(fdr, &rfds) returns false?

From the POSIX standards reference page on select :

FD_ISSET(fd, fdsetp) shall evaluate to non-zero if the file descriptor fd is a member of the set pointed to by fdsetp, and shall evaluate to zero otherwise.

So exactly what the result of FD_ISSET (which is really not a function but a macro so technically it doesn't "return" anything) is not mentioned, just that it's either zero or non-zero.

To answer your question, yes. If fd is readable then FD_ISSET(fd, &rfds) will be non-zero (true) and FD_ISSET(fdr, &rfds) will be zero (false) (unless it's also readable, so don't use else if there, both might be true).

Yes. That is its purpose. Read the documentation.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM