Poll STDIN and Other Fds
Hello I want to monitor FD's which are from libssh connection apis. but also want to monitor STDIN, and parse the comma开发者_JAVA百科nds from STDIN.
Can I do this using poll in C++.
Not in standard C++, which doesn't have FD's or poll()
. But on unix (POSIX), stdin
is a valid FD with value 0.
I've not used libssh before bust a read of the feature list tells me it has full poll support. So, you should be able to poll() to monitor your ssh connections as well as the STDIN file descriptor directly.
e.g. to poll stdin for input something like
int timeout = 100; // wait 100ms
struct pollfd fd;
fd.fd = STDIN;
fd.events = POLLIN;
fd.revents = 0;
int ret = poll(&fd, 1, timeout);
if (ret > 0 && (fd.revents & POLLIN != 0)) {
// got some data
} else {
// check for error
}
The first argument to poll is an array of "struct pollfd". I've only specified a length of 1 (second argument). But you can allocate an array for as many as you need with the other file descriptors from libssh included in the array you want to monitor.
精彩评论