ftp部分功能的实现(可以实现断点重传和断点续传)

该博客介绍了如何仿照Linux系统下的ftp工具,实现包括断点重传和断点续传在内的部分功能。主要内容涵盖`main.c`、`ftp_client.c`、`ftp_client.h`、`tools.c`和`tools.h`等源文件的编写,旨在提供一个实用的ftp客户端实现方案。
摘要由CSDN通过智能技术生成

仿照Linux系统下的ftp功能进行编写。
main.c

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "ftp_client.h"

// 创建FTP客户端对象
FTPClient* ftp = NULL;

void sigint(int num)
{
   
	if(ftp->is_get)
	{
   
		printf("------------------------\n");
		close(ftp->cli_pasv);
		close(ftp->cli_sock);
		close(ftp->fd);
		set_mtime(ftp->file,ftp->mtime);
		printf("------------------------\n");
	}
	if(ftp->is_put)
	{
   
		close(ftp->cli_pasv);
		close(ftp->fd);
		mdtm_FTPClient(ftp);
		close(ftp->cli_sock);
	}
	exit(EXIT_SUCCESS);
}

int main(int argc,const char* argv[])
{
   
	signal(SIGINT,sigint);
	if(2 == argc)
	{
   
		ftp = create_FTPClient(argv[1],21);
	}
	else if(3 == argc)
	{
   
		short port = atoi(argv[2]);
		ftp = create_FTPClient(argv[1],port);
	}
	else
	{
   
		printf("use:ftp xxx.xxx.xxx.xxx [port]\n");
		return EXIT_SUCCESS;
	}

	// 输入用户名
	char user[20] = {
   };
	printf("Name:");
	get_str(user,sizeof(user));
	user_FTPClient(ftp,user);
	
	// 密码并登录
	char pass[20] = {
   };
	printf("Password:");
	get_passwd(pass,sizeof(pass),0);
	pass_FTPClient(ftp,pass);

	// 循环的输入命令、执行命令
	char cmd[256] = {
   };
	for(;;)
	{
   
		printf("ftp> ");
		get_str(cmd,sizeof(cmd));

		if(0 == strncmp("pwd",cmd,3))
		{
   
			pwd_FTPClient(ftp);
		}
		else if(0 == strncmp("cd ",cmd,3))
		{
   
			cd_FTPClient(ftp,cmd+3);
		}
		else if(0 == strncmp("mkdir ",cmd,6))
		{
   
			mkdir_FTPClient(ftp,cmd+6);
		}
		else if(0 == strncmp("ls",cmd,2))
		{
   
			ls_FTPClient(ftp);
		}
		else if(0 == strncmp("put ",cmd,4))
		{
   
			put_FTPClient(ftp,cmd+4);
		}
		else if(0 == strncmp("get ",cmd,4))
		{
   
			get_FTPClient(ftp,cmd+4);
		}
		else if(0 == strncmp("bye",cmd,3))
		{
   
			bye_FTPClient(ftp);
			destory_FTPClient(ftp);
			return EXIT_SUCCESS;
		}
		else if('!' == cmd[0])
		{
   
			system(cmd+1);
		}
		else
		{
   
			printf("指针未定义!!!\n");
		}
	}
}


ftp_client.c

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <utime.h>
#include <getch.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "ftp_client.h"

typedef struct sockaddr* SP;
int set_mtime(const char* path,const char* str)
{
   
	printf("mtime=%s\n",str);
	struct tm t = {
   };
	sscanf(str,"%4d%2d%2d%2d%2d%2d",
		&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值