Hi Aris,
that sound logical for me, but there is a another problem.
I tried the same test program on different maschines and the result is very
strange.
result is only the return value of channel_poll(channel, 0) without the while
loop, etc.
case 1: windowsPc (company) ---> linuxServer (company) result: -1
case 2: windowsPc (home) ---> linuxServer (home) result: 0
case 3: linuxPc (home) ---> linuxServer (company) result: works!
case 4: linuxPc (home) ---> linuxServer (home) result: works!
(linux ---> linuxServer works always, windows ---> linuxServer fail,
but with different results)
Regards,
Thomas
Am 12.05.2010 16:07, schrieb Aris Adamantiadis:
Hi Thomas,
I discourage you to use channel_read_buffer as this function will be
deprecated soon. Please use channel_read instead.
Your example is blocking because you're looping several time inside the
loop. When there is no new data to read, channel_poll returns 0. That
means that you're doing a channel_read when there's nothing to read,
hence the blocking behaviour.
I recommend this rewrite:
while (channel_poll(channel, 0)> 0)
{
char buf[128];
rc = channel_read_buffer(channel, buf, sizeof(buf), 0);
if(rc<sizeof(buf))
buf[rc]='\0'; // 0-ends the string
else
buf[sizeof(buf)-1]='\0';
if (rc< 0)
{
channel_close(channel);
ssh_disconnect(session);
ssh_finalize();
return 1;
}
printf("%s\n", buf);
}
}
Regards,
Aris
Bart Simpson a écrit :
Hello,
I wrote a little test program to exec a command on a remote host and
read the result from channel.
Open session, authentification with public keys, exec command, etc. work
all fine.
But when I try to read something from the channel with this code part
from the examples of libssh,
the function channel_poll() is blocking. After a few minutes -1 returned.
Do anyone know, whats wrong?
...
if (channel_is_open(channel))
{
while (channel_poll(channel, 0)>= 0)
{
buf = buffer_new();
rc = channel_read_buffer(channel, buf, 0, 0);
if (rc< 0)
{
buffer_free(buf);
channel_close(channel);
ssh_disconnect(session);
ssh_finalize();
return 1;
}
printf("%s\n", (char *) buffer_get(buf));
buffer_free(buf);
}
}
...
Regards,
Thomas