目录
项目介绍
FTP是File Transfer Protocol(文件传输协议)。
Linux网络编程下实现的FTP服务器项目分为客户端和服务端,该项目主要是实现客户端对服务器上的文件内容进行展示、显示路径、删除、上传、下载等功能,以及对客户端本身文件进行展示、显示路径、删除文件等功能。
功能指令介绍
客户端远程功能
- 展示服务端有哪些文件——ls
- 显示服务端当前路径——pwd
- 进入服务端哪个文件——cd
- 删除服务端文件——rm file
- 下载服务端文件——get file
- 上传文件到服务端——put file
客户端本地功能
- 展示本地文件——lls
- 显示本地当前路径——lpwd
- 进入本地文件——lcd
- 删除本地文件——lrm
- 退出本地程序——quit
用到的函数API
1、strtok函数
功能:切割字符串
char *strtok(char *str, const char *delim);
参数一:被切割的字符串参数二:要切割的字符
2、strncmp函数
功能:对比参数一和参数二是否一致,一致返回0,不一致非0
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
比上一个函数多了比较次数n
3、chdir函数
功能:跳转至以参数path 指定目录
int chdir(const char *path);
4、system函数
功能:利用shell调用命令,调用不会覆盖文件,而是在程序调用处继续执行
int system(const char * cmdstring)
5、popen函数
功能:可以像打开文件一样打开 shell 指令。后续可以使用 fread 读取内容到缓冲区 buf
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream); //打开后要用 pclose 关闭文件
头文件
#define LS 0
#define LLS 1
#define PWD 2
#define CD 3
#define RM 5
#define GET 6
#define PUT 7
#define LPWD 8
#define LCD 9
#define LRM 10
#define QUIT 120
struct myFTP
{
int set; // 客户端返回给服务器的宏命令
int mark; // 判断文件是否存在的标识
char cmd[128]; // 用户输入的命令
char data[1024]; // 存放根据指令进行相关读取操作的 结果
};
客户端代码
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#includ