2016-07-26 156 views
3

我已经创建了一个会话和一个通道。一个请求(ssh_channel_request_exec)应该每隔一秒发送一次,我想读这个请求的答案(ssh_channel_read)。但是,我找不到如何进行多个请求的示例,api仅包含如何提出请求,阅读答案并关闭频道的示例。libssh - 多次发送请求并从通道中读取答案

当我尝试按顺序两次从通道请求和读取数据时,ssh_channel_request_exec第二次返回错误。是否有必要为每个请求打开一个新频道?

int ret = ssh_channel_request_exec(m_channel, request.c_str()); 
if (ret != SSH_OK) 
{ 
    ssh_channel_close(m_channel); 
    ssh_channel_free(m_channel); 
    return false; 
} 

std::stringstream sstream; 
nbytes = ssh_channel_read(m_channel, buffer, sizeof(buffer), 0); 
while (nbytes > 0) 
{ 
    sstream.write(buffer, nbytes); 
    nbytes = ssh_channel_read(m_channel, buffer, sizeof(buffer), 0); 
} 

if (nbytes < 0) // 'nbytes' becomes zero the first time, the channel is not closed the second time! 
{ 
    ssh_channel_close(m_channel); 
    ssh_channel_free(m_channel); 
    return false; 
} 

response = sstream.str(); 

API还包含从在示例信道读取后使用的ssh_channel_send_eof功能。有什么好处? api只说“在通道上发送文件结尾,这不会关闭通道,您仍然可以读取但不能写入。”

一种用于ssh_channel_is_eof校验(API:“检查远程已发送EOF”)示出了信道没有EOF的第一次,但第二时间,它是。

回答