以下函数实现的是FTP中的ls-l,pwd,cd,get(下载),put(上传)功能,函数有点长,且感觉很多地方设计的不合理,以后慢慢优化。而且原本打算加入断点续传和断点下载功能,但文件时间始终找不到合适的修改方法。找到方法了也会把这两个功能加进去。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <getch.h>
#include <arpa/inet.h>
#include <stdbool.h>
typedef struct sockaddr* SP;
bool snd_rcv(char* buf,int sock,int ret,const char* warn) // 封装函数,功能是将命令发送到服务器并对服务器返回的信息进行判断
{
if(strlen(buf))
{
send(sock,buf,strlen(buf),0);
}
recv(sock,buf,4096,0);
printf("recv: %s\n",buf);
int len;
sscanf(buf,"%d",&len);
if(len != ret)
{
printf("%s\n",warn);
return false;
}
return true;
}
int order_pasv(char* buf,int sock) // 封装函数,功能是打开一个下载通道,返回值返回打开后的下载通道套接字,关闭时需要在外部手动关闭
{
sprintf(buf,"PASV\n");
if(!snd_rcv(buf,sock,227,"下载通道连接失败"))
{
return -1;
}
unsigned short int ip1,ip2,ip3,ip4,port1,port2;
sscanf(buf,"227 Entering Passive Mode (%hu,%hu,%hu,%hu,%hu,%hu)",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
char ip[16]