[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: channel_poll() is blocking
[Thread Prev] | [Thread Next]
- Subject: Re: channel_poll() is blocking
- From: Aris Adamantiadis <aris@xxxxxxxxxxxx>
- Reply-to: libssh@xxxxxxxxxx
- Date: Wed, 12 May 2010 16:07:34 +0200
- To: libssh@xxxxxxxxxx
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
>
| Re: channel_poll() is blocking | Bart Simpson <b_a_r_t@xxxxxxx> |
| channel_poll() is blocking | Bart Simpson <b_a_r_t@xxxxxxx> |