在linux系统下实现了FTP的部分功能
如显示、创建文件夹、进入文件夹等
具体实现代码如下
首先是头文件
#ifndef FTP_H
#define FTP_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#define BUF_SIZE 4096
// 网络通信结构
typedef struct NetWork
{
int sock_c; // 命令通道socket符
int sock_p; // 数据通道socket符
int status;
char* buf;
}NetWork;
// 打开网络通信对象
NetWork* nw_open(const char* ip);
// 登入
int log_in(NetWork* nw);
// 功能判断
int funcjg(const char* fun,size_t f_len);
// ls -l功能
int l_ls(NetWork* nw);
// 显示当前目录
void p_pwd(NetWork* nw);
// 新建目录
int m_mkd(NetWork* nw,const char* motion);
// 进入目录
int c_cwd(NetWork* nw,const char* motion);
// 上传文件
int s_stor(NetWork* nw,const char* motion);
#endif// FTP_H
然后是头文件内函数的功能实现
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdbool.h>
#include <getch.h>
#include "ftp.h"
typedef struct sockaddr* SP;
int ret = 0;
// 错误检查
static void check_status(NetWork* nw,int status,bool die)
{
size_t ret_size = recv(nw->sock_c,nw->buf,BUF_SIZE,0);
if(0 == ret_size)
{
perror("recv");
exit(EXIT_FAILURE);
}
nw->buf[ret_size] = '\0';
printf("%s",nw->buf);
sscanf(nw->buf,"%d",&nw->status);
if(nw->status != status && die)
{
exit(EXIT_FAILURE);
}
}
// 发送命令
void send_cmd(NetWork* nw)
{
int ret_size = send(nw->sock_c,nw->buf,strlen(nw->buf),0);
if(0>= ret_size)
{
perror(