Subject: RE: libssh2_channel_read returning different format for same command

RE: libssh2_channel_read returning different format for same command

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

Oops. My bad.

I changed the read function and it's fine now.

string ACMManager::ReadOSSIResponse(LIBSSH2_CHANNEL *channel)
{
    string r = "";

    try
    {
            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-1));

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

           buffer[readBytes] = '\0';

            r.append(buffer);

            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;
}

From: libssh2-devel [mailto:libssh2-devel-bounces_at_cool.haxx.se] On Behalf Of Ty Oh
Sent: Freitag, 4. Januar 2013 14:37
To: libssh2-devel_at_cool.haxx.se
Subject: libssh2_channel_read returning different format for same command

Hi

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 = "";

    try
    {
            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';

            r.append(buffer);

            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,
Ty

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