MOHAMED
MOHAMED

Reputation: 43518

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?

Upvotes: 0

Views: 1041

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385106

Yes. That is its purpose. Read the documentation.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409166

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

Upvotes: 3

Related Questions