⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp_client_receive.c

📁 embedded_ethernet_complete_code_rabbit
💻 C
字号:
// Download a file from an FTP server and print the file's contents to the
// stdio window.

// This file and other embedded Ethernet and Internet code and resources are
// available from www.Lvr.com.

// Use these for debugging:
// Allow debugging in FTP libraries.
#define FTP_DEBUG
// Write FTP debugging messages to the stdio window.
#define FTP_VERBOSE
// Allow debugging in TCP/IP libraries.
#define DCRTCP_DEBUG
// Write TCP/IP debugging messages to the stdio window.
#define DCRTCP_VERBOSE

// Select a network configuration from \lib\tcpip\tcp_config.lib
// in the Dynamic C distribution.
#define TCPCONFIG 1

// Set the size of the DHCP socket buffer.
// Not required for Dynamic C v8 and higher.
//#define DHCP_SOCK_BUF_SIZE 900

// The IP address or name of the FTP server to access.
// YOU MUST CHANGE THIS VALUE to match the IP address or name of an FTP server
// your Rabbit module can access.
// If you specify the server by name, you must define MY_NAMESERVER in
// tcpconfig.lib or in this application.

#define REMOTE_HOST		"192.168.111.5"

// The port to use for the control channel on the server.
// Set to 0 to use the default port (21).
#define REMOTE_PORT		0

// The user name and password for logging onto the server.
// YOU MUST CHANGE THESE VALUES to match the user name and password required
// to log onto the server the Rabbit will access.
#define REMOTE_USERNAME "embedded"
#define REMOTE_PASSWORD "ethernet"

// The file to request.
// YOU MUST CHANGE THIS VALUE to match the name of the file the Rabbit
// will request.
#define REMOTE_FILE		"testfile.txt"

// The directory to change to on the server (the location of the requested file).
// Use "/" for the root directory.
#define REMOTE_DIR		"/"

// Request to use passive mode, where the client requests to open the connection
// for the FTP data channel.
// Comment out this line to cause the server to request to open the connection
// for the data channel.
#define USE_PASSIVE

// If USE_PASSIVE is defined, PASSIVE_FLAG is defined as FTP_MODE_PASSIVE.
#ifdef USE_PASSIVE
	#define PASSIVE_FLAG  FTP_MODE_PASSIVE
#else
	#define PASSIVE_FLAG  0
#endif

// All C functions not declared as root go to extended memory.
#memmap xmem

// The dcrtcp library supports IP and TCP; the ftp_client library supports FTP
// client communications.
#use "dcrtcp.lib"
#use "ftp_client.lib"

// The buffer that will hold the retrieved file.
char file_buffer[2048];

int main() {

	// Initialize TCP/IP support and call a function to retrieve a file.

  int return_value;

  // Initialize the TCP/IP stack.
  return_value = sock_init();

	// If the return value isn't zero, the network isn't available.
  if (return_value == 0) {
    printf("Network support is initialized.\n");
    }
  else {
    printf("The network is not available.\n");
    exit(2);
	}

	// Retrieve the file.
   // If the function doesn't return zero, exit.
	if (retrieve_file()) {
		exit(1);
   }
	return 0;
} // end main()

int retrieve_file(void) {

	// Attempt to retrieve a file.
	longword file_size;
	int byte_in_file;
	int return_value;

	printf("Preparing to download %s...\n", REMOTE_FILE);

   // Request a file.
   // REMOTE_HOST is the IP address or name of the FTP server.
   // resolve() converts the IP address or name string to an IP address expressed
   // as a longword.
   // REMOTE_PORT is the port to use for the control channel on the server.
   // REMOTE_USERNAME and REMOTE_PASSWORD are the user name and password to
   // use when logging in to the server.
   // FTP_MODE_DOWNLOAD requests to download a file.
   // PASSIVE_FLAG requests to use passive mode.
   // REMOTE_FILE is the requested file.
   // REMOTE_DIR is the location of the requested file on the server.
   // file buffer is the buffer to store the file's contents in.
   // return_value is 0 on success or 1 on failure.
	return_value = ftp_client_setup(resolve(REMOTE_HOST),REMOTE_PORT,REMOTE_USERNAME,
			REMOTE_PASSWORD,FTP_MODE_DOWNLOAD|PASSIVE_FLAG,REMOTE_FILE,
			REMOTE_DIR,file_buffer,sizeof(file_buffer));

	if (return_value != 0) {
		printf("FTP setup failed.\n");
		exit(1);
   }

   // ftp_client_tick manages FTP communications.
   // The function returns 0 while communications are in progress.
	printf("Looping on ftp_client_tick()...\n");
	while( 0 == (return_value = ftp_client_tick()) );

	if( 1 == return_value ) {
      // If ftp_client_tick returns 1, get the number of bytes in the file and
	   // print the contents of the file to the stdio window.
		file_size = ftp_client_xfer();
		printf("The file has been received. File size: %d bytes.\n", file_size);
		printf("Contents of file:\n");

	for (byte_in_file = 0; byte_in_file <= (file_size - 1); byte_in_file++)
		printf("%c",file_buffer[byte_in_file]);
		printf("\n");
		return 0;
	} else {
   	// If ftp_client_tick returns something other than 0 or 1, print the
      // last code returned by the FTP server to the stdio window.
		printf("FTP download failed: status = %d, last code = %d\n", return_value, ftp_last_code());
		return 1;
	}
} // end retrieve_file


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -