2016-07-26 56 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的第一次,但第二時間,它是。

回答