Sample Program for AFTP API

This sample code is made available by Microsoft Corporation on an as-is basis. Anyone receiving this code is considered to be licensed under Microsoft copyrights to use the Microsoft-provided source code in any way he or she deems fit, including copying it, compiling it, modifying it, and redistributing it, with or without modifications. No license under any Microsoft patents or patent applications is to be implied from this copyright license.

A user of this sample code should understand that Microsoft cannot provide technical support for the code and will not be responsible for any consequences of its use.

This sample program shows a simple exercise of using the AFTP programming interface. It gets a single file from a remote machine. The user must know the machine name and the file name. Comments are inserted in bold text throughout the sample.

System include files:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 

AFTP API include file:

#include "aftpapi.h"

Note If you want to use the header file as it was shipped, change the file name in this include statement to APPFFTP.H. Otherwise, rename the member APPFFTP.H to AFTPAPI.H for consistency with other platforms.

int main(int argc, char *argv[])
{
    AFTP_HANDLE_TYPE connection_id;    /* connection id               */
    AFTP_RETURN_CODE_TYPE aftp_rc;     /* return code                 */
    AFTP_SECURITY_TYPE sec_type;       /* security type               */
    unsigned char * LU_name;           /* partner LU name             */
    unsigned char * srcfilename;       /* source file name            */
    unsigned char * destfilename;      /* destination file name       */

printf( "\n" );

if( argc != 4 ) {

    printf( "Usage : aget <LU name> <source filename>"  \
            " <destination filename> \n");
    exit( -1 );
}
 
LU_name = argv[1];
srcfilename = argv[2];
destfilename = argv[3];
Create the connection object
aftp_create ( connection_id, &aftp_rc );

if ( aftp_rc != AFTP_RC_OK ) {
      printf ( "Error creating connection object.\n" );
      exit ( -1 );
}
 

Set the partner LU name as the destination.

aftp_set_destination (
        connection_id,
        (unsigned char AFTP_PTR)LU_name,
        (AFTP_LENGTH_TYPE)strlen ( LU_name ),
        &aftp_rc );

if( aftp_rc != AFTP_RC_OK ) {
     printf ( "Error setting the destination.\n" );
     exit ( -1 );
}
 

Set the security to NONE unless you need security.

aftp_set_security_type (
        connection_id,
        AFTP_SECURITY_NONE,
        &aftp_rc );

if ( aftp_rc == AFTP_RC_OK ) {
     printf ( "Setting security type to NONE.\n" );
} else {
     printf ( "Error setting security type.\n" );
}
 

Establish a connection with AFTPD server.

aftp_connect ( connection_id, &aftp_rc );

if ( aftp_rc != AFTP_RC_OK ) {
     printf ( "Error establishing the connection.\n" );
     exit ( -1 );
}
 

Set up file transfer mode.

aftp_set_write_mode (
       connection_id,
       AFTP_REPLACE,
       &aftp_rc );

if ( aftp_rc != AFTP_RC_OK ) {
      printf ( "Error setting write mode.\n" );
}
 

Extract the security type and display it.

aftp_extract_security_type (
       connection_id,
       &sec_type,
       &aftp_rc );

if ( aftp_rc == AFTP_RC_OK ) {
      printf ( "Security type is : %lu\n", sec_type );
} else {
      printf ( "Error extracting security type.\n" );
}
 

Transfer the file from the server to the client.

aftp_receive_file (
        connection_id,
        (unsigned char AFTP_PTR)destfilename,
        (AFTP_LENGTH_TYPE)strlen ( destfilename ),
        (unsigned char AFTP_PTR)srcfilename,
        (AFTP_LENGTH_TYPE)strlen ( srcfilename ),
        &aftp_rc );

if ( aftp_rc == AFTP_RC_OK ) {
     printf ( "File successfully transfered.\n" );
}
 

This is an example of how to show error reporting.

AFTP_LENGTH_TYPE return_length;
char error_string[ AFTP_MESSAGE_SIZE ];

printf ( "Error %lu transfering the file.\n", aftp_rc );
 

Specify a detail level according to how much information you want returned. In this case, return code information is requested.

aftp_format_error (
       connection_id,
       (AFTP_DETAIL_LEVEL_TYPE)AFTP_DETAIL_RC,
       (unsigned char AFTP_PTR)error_string,
       (AFTP_LENGTH_TYPE)( sizeof ( error_string )-1 ),
       &return_length,
       &aftp_rc );
 

Add a null terminator.

error_string[ return_length ] = '\0';
    printf ( "%s", error_string );
}
 

Close a connection with AFTPD server.

aftp_close ( connection_id, &aftp_rc );

if ( aftp_rc != AFTP_RC_OK ) {
     printf ( "Error closing the connection.\n" );
     exit ( -1 );
}
 

Destroy the connection identifier.

aftp_destroy ( connection_id, &aftp_rc );

if (aftp_rc != AFTP_RC_OK) {

     printf ( "Error destroying the connection id.\n" );
     exit ( -1 );
}

return(0);
}

/* END SAMPLE PROGRAM */