www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples | github

Archive Index This month's Index

Subject: libssh2_channel_read returning different format for same command

libssh2_channel_read returning different format for same command

From: Ty Oh <tyoh_at_cct-solutions.com>
Date: Fri, 4 Jan 2013 14:37:24 +0100


I am using libssh2_channel_write and libssh2_channel_read to communicate with a server that accepts commands.
The server returns tab and newline delimited data.

I'm facing a problem where the returned data is not always in the same format for the same command.
e.g. It will have some extra tabs or newlines and sometimes a backspace \x08 etc.

Because, the data is tab and newline delimited, it is impossible to adjust and fix the read data.

As far as I can see.

- The results are random.

- The first read is always good.

- Only occurs to large data returned. E.g. 20k

- Sometimes it will go wrong on the second time the command was sent, or sometimes on the 17th repeat etc.

I'm certain that this is not a server problem.
I am in the process of porting a released to production application from C# to Ubuntu GCC, and this problem never occurred.

Anyone had such issues?

Here's a snip of the read function.

string SSHManager::ReadResponse(LIBSSH2_CHANNEL *channel)
    string r = "";

            int bufSize = 8192;
        ssize_t readBytes;

        //keep reading until terminator is received. \nt\n
        while (true)
            //new the char array every loop.
            char *buffer = new char[bufSize];

            //keep reading
            readBytes = libssh2_channel_read(channel, buffer, bufSize);

            if (readBytes < 1)
                return r;
            else if (readBytes == 0)
                        return "Error. Socket Closed"; //socket closed.

            if(readBytes < bufSize)
                        buffer[readBytes+1] = '\0';


            delete[] buffer;

            if (r.length() > 2)
                        if(r.find("\nt\n") != string::npos)
                                    break; //terminator received
    catch (int e)
            ERRORP("An exception occurred. Exception Number %d", e);

    return r;

Thanks and Best regards,

libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2013-01-04

the libssh2 team