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

📄 ftp_client_send.c

📁 embedded_ethernet_complete_code_rabbit
💻 C
字号:
// Uplaod a file to an FTP server.

// 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 send.
// YOU MUST CHANGE THIS VALUE to match the name of the file the Rabbit
// will send.
#define REMOTE_FILE		"testfile.txt"

// The directory to change to on the server (the location to store the 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_FLAD 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 size of the buffer that will hold the file being sent.
char file_buffer[10];

int main() {

	// Initialize TCP/IP support and call a function to send a file.
	int return_value;
	create_file();

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

	// Send the file.
	// If the function doesn't return zero, exit.
	if (send_file())
		exit(1);

	return 0;
} // end main()

create_file(void) {
	// Place data in the file_buffer array.
	// The array will be sent as a file to the FTP server.
	file_buffer[0]='t';
	file_buffer[1]='e';
	file_buffer[2]='s';
	file_buffer[3]='t';
	file_buffer[4]=' ';
	file_buffer[5]='d';
	file_buffer[6]='a';
	file_buffer[7]='t';
	file_buffer[8]='a';
	file_buffer[9]='\0';
} // end create_file

int send_file(void)
{
	int return_value;
	printf("Calling ftp_client_setup() to upload %s...\n", REMOTE_FILE);

	// Send 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_UPLOAD requests to upload a file.
	// PASSIVE_FLAG requests to use passive mode.
	// REMOTE_FILE is the name of the file being sent.
	// REMOTE_DIR is the location to store the file on the server.
	// file buffer is the buffer that contains the file.
	// 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_UPLOAD|PASSIVE_FLAG,REMOTE_FILE,
			REMOTE_DIR,file_buffer,sizeof(file_buffer));
	if (return_value != 0)	{
		printf("FTP setup failed.\n");
		exit(2);
	}

	printf("Looping on ftp_client_tick()...\n");

   // ftp_client_tick manages FTP communications.
   // The function returns 0 while communications are in progress.
	while( 0 == (return_value = ftp_client_tick()) );

	if( 1 == return_value ) {

      // If ftp_client_tick returns 1, get the number of bytes transferred and
	   // print the result to the stdio window.
		printf("FTP upload completed successfully.  %d bytes.\n", ftp_client_filesize());
		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 upload failed: status = %d, last code = %d\n", return_value, ftp_last_code());
		return 1;
	}
} // end send_file



⌨️ 快捷键说明

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