【问题标题】:libssh how do I know if there has data to read?libssh 我怎么知道是否有数据要读取?
【发布时间】:2022-05-08 18:49:16
【问题描述】:

我正在使用 libssh。
调用ssh_channel_write后,我必须知道是否有数据要读取。 (但我不想检索数据。
如果没有数据要读取(例如10s后),我会再次调用ssh_channel_write
ssh_channel_readssh_channel_read_nonblocking 都不能这样做。 (并且使用 SSH_READ_PENDING 检查 ssh_get_status 也不起作用。)
有没有办法解决这个问题?

【问题讨论】:

    标签: ssh libssh


    【解决方案1】:

    它是非阻塞的......

    使用 ssh_select() 函数。它的工作方式与常规的 select() 非常相似,但使用的是通道而不是套接字。

    int ssh_select (ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd, fd_set *readfds, struct timeval *timeout);

    例如,单通道实现:

    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_channel_read_nonblocking来做这个工作的。
    【解决方案2】:

    使用回调更好,并且比 ssh_select 使用更少的 CPU。

    请看我的例子https://gitlab.com/marco.fortina/libssh-x11-client

    我使用了回调和 X11 转发。

    干杯,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-03
      • 2021-10-05
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      相关资源
      最近更新 更多