我正在使用 libssh。
调用ssh_channel_write
后,我必须知道是否有数据要读取。 ( 但我不想检索数据。 )
如果没有数据要读取(例如10s后),我会调用ssh_channel_write
再次。
两个ssh_channel_read
和 ssh_channel_read_nonblocking
不能这样做。 (并且使用 SSH_READ_PENDING 检查 ssh_get_status
也不起作用。)
有没有办法解决这个问题?
最佳答案
这是非阻塞...
使用 ssh_select() 函数。它的工作方式与常规的 select() 非常相似,但使用的是 channel 而不是套接字。
int ssh_select (ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd, fd_set *readfds, struct timeval *timeout);
例如,单 channel 实现:
ssh_channel channels[2];
ssh_channel myChannel = ssh_channel_new (ssh_session session);
channels[0] = myChannel;
channels[1] = NULL;
struct timeval timeout = (0, 200000); // 0 seconds, 200 millis
int rc = ssh_select (channels, NULL, NULL, NULL, &timeout);
if (rc > 0) {// There is a pending data.
if (rc < 0) // the ssh_select() error.
if (rc == 0) // You've got a broken connection.
关于ssh - libssh 我怎么知道是否有数据要读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38549510/