하나의 세션과 하나의 채널을 만들었습니다. 요청 (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 있지 않은 것을 알 수 있지만, 두 번째는이다.