Subject: Re: Small binary files are getting transferred but not the larger ones in libssh2 SFTP

Re: Small binary files are getting transferred but not the larger ones in libssh2 SFTP

From: kali muthu <direct2kali_at_gmail.com>
Date: Thu, 10 Mar 2011 17:19:55 +0530

This is my C++ code for transferring the files..

int sshInitStatus = libssh2_init(0);
    if(sshInitStatus != 0)
    {
        return;
    }

    WSADATA wsaData;
    int startupStatus = WSAStartup(MAKEWORD(1,1),&wsaData);
    if(startupStatus != 0)
    {
        return;
    }

    /// Socket Creation
    m_socketID = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(22);
    sin.sin_addr.s_addr = inet_addr(m_SFTPServerIP.toAscii().data());

    /// Socket Connection
    int connectionStatus = ::connect(m_socketID,(struct sockaddr*)(&sin),
                                   sizeof(struct sockaddr_in));
    if(connectionStatus != 0)
    {
        return;
    }

    /// Session Initialization
    m_sshSession = libssh2_session_init();
    if(!m_sshSession)
    {
        //std::cout<<"Unable to initialize a session.";
    }
    libssh2_session_set_blocking(m_sshSession,0);

    /// Session Startup
    int handshakeStatus = -37;

    while(handshakeStatus == LIBSSH2_ERROR_EAGAIN)
    {
        handshakeStatus = libssh2_session_startup(m_sshSession,m_socketID);
    }

    if(handshakeStatus != 0)
    {
        return;
    }
int authenticationStatus = -37;

    while(authenticationStatus == LIBSSH2_ERROR_EAGAIN)
    {
        authenticationStatus =
libssh2_userauth_password(m_sshSession,m_username.toAscii().data(),

 m_password.toAscii().data());
    }
    if(authenticationStatus != 0)
    {
        return;
    }

    do
    {
        /// Init SFTP Session
        m_sftpSession = libssh2_sftp_init(m_sshSession);
        if(m_sftpSession == NULL)
        {
            int lLastSFTPError = libssh2_session_last_errno(m_sshSession);
            if( lLastSFTPError == LIBSSH2_ERROR_EAGAIN)
            {
                waitSocket();
            }
        }
    }while(m_sftpSession == NULL);
 do
        {
            m_sftpHandle =
libssh2_sftp_open(m_sftpSession,"/usr/LongBinaryFile",

 LIBSSH2_FXF_READ,LIBSSH2_SFTP_OPENFILE);
            if(m_sftpHandle == NULL)
            {
                int lLastSFTPError =
libssh2_session_last_errno(m_sshSession);
                if( lLastSFTPError == LIBSSH2_ERROR_EAGAIN)
                {
                    waitSocket();
                }
            }
        }while(m_sftpHandle == NULL);

        int readStatus = LIBSSH2_ERROR_EAGAIN;

        std::ofstream
lTargetFile("transferredOne",std::ios::out|std::ios::binary);

        if(lTargetFile.is_open())
        {
            do
            {
                char buffer[1024]={'\0'};
                do
                {
                    readStatus =
libssh2_sftp_read(m_sftpHandle,buffer,1024);
                }while(readStatus == LIBSSH2_ERROR_EAGAIN);

                if(buffer[0] == '\0')
                {
                    std::cout<<"Empty Buffer";
                }

                if(readStatus>0)
                {
lTargetFile<<buffer;
                }
                else
                {
                    break;
                }
            }while(true);
        }
        lTargetFile.close();
        libssh2_sftp_close(m_sftpHandle);

And I could see that most of the time the execution goes to the
block if(buffer[0] == '\0') and that is the reason for the resulting binary
files has less size than the original.

I tried your example also and still the resulting files has lesser size than
the original.

On Mon, Mar 7, 2011 at 3:16 AM, Daniel Stenberg <daniel_at_haxx.se> wrote:

> On Thu, 3 Mar 2011, kali muthu wrote:
>
> I am using the libssh2_sftp_read() to read the remote server files which
>> are binary. I am using the session in non-blocking mode. I can able to
>> transfer text files and smaller binary files.
>>
>
> Can you show us a full source code of your app that doesn't work? We do
> provide the sftp_nonblock.c example that shows a SFTP download using the
> non-blocking API...
>
> --
>
> / daniel.haxx.se
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
>

-- 
Regards,
Kali

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2011-03-10