赞
踩
依照FTP协议规范设计,实现基本功能:列出内容列表、下载文件、上传文件、重命名文件、删除文件等等,代码仅做参考:
(此代码运行在linux系统上,如需支持windows系统,需对头文件稍作修改。)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
-
- #define BUFFER_SIZE 1024
-
- // 连接FTP服务器
- int connect_server(const char* server_ip, int port) {
- int sockfd;
- struct sockaddr_in server_addr;
-
- // 创建套接字
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- perror("socket");
- return -1;
- }
-
- // 设置服务器地址
- memset(&server_addr, 0, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(port);
- if (inet_pton(AF_INET, server_ip, &(server_addr.sin_addr)) <= 0) {
- perror("inet_pton");
- return -1;
- }
-
- // 连接服务器
- if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
- perror("connect");
- return -1;
- }
-
- return sockfd;
- }
-
- // 发送FTP命令
- void send_ftp_command(int sockfd, const char* command) {
- char buffer[BUFFER_SIZE];
- sprintf(buffer, "%s\r\n", command);
- send(sockfd, buffer, strlen(buffer), 0);
- }
-
- // 接收FTP响应
- void receive_ftp_response(int sockfd) {
- char buffer[BUFFER_SIZE];
- memset(buffer, 0, sizeof(buffer));
- recv(sockfd, buffer, sizeof(buffer)-1, 0);
- printf("%s", buffer);
- }
-
- // 登录FTP服务器
- void login_ftp_server(int sockfd, const char* username, const char* password) {
- send_ftp_command(sockfd, "USER %s", username);
- receive_ftp_response(sockfd);
-
- send_ftp_command(sockfd, "PASS %s", password);
- receive_ftp_response(sockfd);
- }
-
- // 列出FTP服务器的内容列表
- void list_contents(int sockfd) {
- send_ftp_command(sockfd, "LIST");
- receive_ftp_response(sockfd);
- }
-
- // 下载文件
- void download_file(int sockfd, const char* filename) {
- send_ftp_command(sockfd, "RETR %s", filename);
- receive_ftp_response(sockfd);
- }
-
- // 上传文件
- void upload_file(int sockfd, const char* filename) {
- send_ftp_command(sockfd, "STOR %s", filename);
- receive_ftp_response(sockfd);
- }
-
- // 删除文件或文件夹
- void delete_file(int sockfd, const char* filename) {
- send_ftp_command(sockfd, "DELE %s", filename);
- receive_ftp_response(sockfd);
- }
-
- // 重命名文件
- void rename_file(int sockfd, const char* oldname, const char* newname) {
- send_ftp_command(sockfd, "RNFR %s", oldname);
- receive_ftp_response(sockfd);
-
- send_ftp_command(sockfd, "RNTO %s", newname);
- receive_ftp_response(sockfd);
- }
-
- int main() {
- const char* server_ip = "127.0.0.1";
- int port = 21;
- const char* username = "your_username";
- const char* password = "your_password";
-
- // 连接FTP服务器
- int sockfd = connect_server(server_ip, port);
- if (sockfd < 0) {
- exit(1);
- }
-
- // 登录FTP服务器
- login_ftp_server(sockfd, username, password);
-
- // 执行FTP操作:列出内容列表、下载文件、上传文件、重命名文件、删除文件
- list_contents(sockfd);
- download_file(sockfd, "file.txt");
- upload_file(sockfd, "file.txt");
- rename_file(sockfd, "file.txt", "newfile.txt");
- delete_file(sockfd, "newfile.txt");
-
- // 关闭连接
- close(sockfd);
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。