Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:46:56+00:00 2026-05-28T19:46:56+00:00

EDIT I had a problem a while ago writing a program in C which

  • 0

EDIT

I had a problem a while ago writing a program in C which utilizes scp to transfer and download files to a server. The program had to be written for windows. Initially I attempted to use the libCurl library which then I encountered different problems and it didn’t work well. After switching to libssh and the issue was fixed.


I’m posting a segment of my sample code which downloads a file from a remote server. hopefully this could be helpful to anyone who lands here.

int scp_recv_file(ssh_session in_session,
                 char * in_remotefile, char * in_localfile)
{
  ssh_scp   t_scp = NULL;
  int       t_rc, t_filesize, t_filemode = -1;
  char      *t_filename, *t_buffer;

  t_scp = ssh_scp_new
    (in_session, SSH_SCP_READ | SSH_SCP_RECURSIVE, in_remotefile);


  if (t_scp == NULL)
  {
    fprintf(stderr, "Error allocating scp session: %s\n",
            ssh_get_error(in_session));
    return SSH_ERROR;
  }

  t_rc = ssh_scp_init(t_scp);
  if (t_rc != SSH_OK)
  {
    fprintf(stderr, "Error initializing scp session: %s\n",
            ssh_get_error(in_session));
    ssh_scp_free(t_scp);
    return t_rc;
  }

  //create ssh pull a file request
  t_rc = ssh_scp_pull_request(t_scp);
  if (t_rc != SSH_SCP_REQUEST_NEWFILE)
  {
    fprintf(stderr, "Error receiving information about file: %s\n",
            ssh_get_error(in_session));
    return SSH_ERROR;
  }

  t_filesize = ssh_scp_request_get_size(t_scp);
  t_filename = strdup(ssh_scp_request_get_filename(t_scp));
  t_filemode = ssh_scp_request_get_permissions(t_scp);

  printf("Receiving file %s, size %d, permisssions 0%o\n",
          t_filename, t_filesize, t_filemode);

  t_buffer = malloc(t_filesize);
  if (t_buffer == NULL)
  {
    fprintf(stderr, "Memory allocation error\n");
    return SSH_ERROR;
  }

  ssh_scp_accept_request(t_scp);
  t_rc = ssh_scp_read(t_scp, t_buffer, t_filesize);
  if (t_rc == SSH_ERROR)
  {
    fprintf(stderr, "Error receiving file data: %s\n",
            ssh_get_error(in_session));
    free(t_buffer);
    return t_rc;
  }
  printf("Done\n");

  //write buffer to file
  writeToFile(in_localfile, t_buffer, t_filesize);
  //write(1, buffer, size);

  //free allocated memory
  free(t_buffer);
  free(t_filename);


  t_rc = ssh_scp_pull_request(t_scp);
  if (t_rc != SSH_SCP_REQUEST_EOF)
  {
    fprintf(stderr, "Unexpected request: %s\n",
            ssh_get_error(in_session));
    return SSH_ERROR;
  }



  //close scp and free
  ssh_scp_close(t_scp);
  ssh_scp_free(t_scp);
  return SSH_OK;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:46:57+00:00Added an answer on May 28, 2026 at 7:46 pm

    Switched to lilbssh. You can manage the scp stages manually and works ok. However, I don’t know if libssh2 is better or not. Any comments?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This question was written in 2008, which was like 3 internet ages ago.
While writing a post about project euler's 14th problem I ran into a difference
So I ran into a problem while building a class in which I was
I've had the same problem with sql for a while and as I sit
EDIT: Ok I had a problem with one of the string concatenation functions, has
Over a year ago I had been working on a WCF service, which I
I've had this problem for a while, and no one I've talked to on
A while ago I had to upgrade some servers from Python 2.4 to Python
I'm developing applications for android devices and had a problem while developing lately. I
::EDIT:: Ok folks, it seems that I'm an idiot after all. The problem had

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.