我创建了一个会话和一个频道。应该每隔一秒发送一个请求(ssh_channel_request_exec
),我想读取此请求的答案(ssh_channel_read
)。但是,我找不到如何发出多个请求的示例,api只包含一个如何发出请求,读取答案和关闭通道的示例。
当我尝试按顺序请求和读取通道两次时,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,而是第二次显示。< / p>