利用C语言实现FTP基本功能

以下函数实现的是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]
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值