tftp协议下的下载和上传

该程序使用UDP套接字进行文件的下载和上传操作。用户可以选择下载或上传文件,通过发送特定协议到服务器。在下载过程中,程序接收数据包并检查序列号以确保数据完整性。在上传时,程序发送文件请求并等待服务器确认。
摘要由CSDN通过智能技术生成

代码如下:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr, "line:%d\n", __LINE__);\
	perror(msg);\
}while(0)

#define IP "192.168.8.246"   //windowsIP
#define PORT 69           //1024~49151

int do_download(int sfd, struct sockaddr_in sin);
int do_upload(int sfd,struct sockaddr_in sin);

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}


	//绑定客户端自身的地址信息结构体-->非必须绑定,建议不写


	//填充服务器自身的地址信息结构体,AF_INET: man 7  IP
	//供于下方的sendto使用,因为sendto必须要指明发给谁
	struct sockaddr_in sin;
	sin.sin_family 		= AF_INET;
	sin.sin_port 		= htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);

	char choose = 0;
	while(1)
	{
		system("clear");

		printf("-----------------------\n");
		printf("------1.download-------\n");
		printf("------2.upload---------\n");
		printf("------3.exit-----------\n");
		printf("-----------------------\n");
		printf("请输入>>>");
		choose = getchar();
		while(getchar()!=10);

		switch(choose)
		{
		case '1': 	
			do_download(sfd, sin);
			break;
		case '2':
			do_upload(sfd,sin);
			break;
		case '3':
			goto END;
		}

		printf("输入任意字符清屏>>>");
		while(getchar()!=10);
	}


END:
	//关闭套接字
	close(sfd);

	return 0;
}

int do_download(int sfd, struct sockaddr_in sin)
{
	//发送下载请求
	char buf[516] = "";
	ssize_t res = 0;

	char filename[20] = "";
	printf("请输入要下载的文件名>>>");
	scanf("%s", filename);
	while(getchar()!=10);

	int size = sprintf(buf, "%c%c%s%c%s%c", 0, 1, filename, 0, "octet", 0);

	//发送下载协议
	if(sendto(sfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("sendto");
		return -1;
	}

	//打开一个文件,用于存储下载到的数据
	int fd=-1, flag = 0;

	socklen_t addrlen = sizeof(sin);
	unsigned short num = 0;
	while(1)
	{
		bzero(buf, sizeof(buf));
		//接收数据包
		res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
		if(res < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}

		//	printf("buf[0]:%d buf[1]:%d 操作码:%#x\n", buf[0], buf[1], ntohs(*(short*)buf));

		//由于操作码为网络字节序,所以有效操作码的数据应该存储在buf[1]的位置
		//所以只需要判断buf[1]是3 还是 5即可
		if(3 == buf[1]) 	//数据包
		{
			if(0 == flag)
			{
				fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664);
				if(fd < 0)
				{
					ERR_MSG("open");
					return -1;
				}
				flag = 1;
			}

			//UDP是无连接的不可靠的,数据包可能会重复接收到的通信;
			//可以在本地记录每次收到的包的编号,
			//如果本地记录的包的编号与数据包发送回来的快编号不一致,则不做处理

			if(htons(num+1) == *(unsigned short*)(buf+2))
			{
				//将数据保存到文件中,
				//最后一次下载到的数据肯定小于512,所以不能直接写512
				if(write(fd, buf+4, res-4) < 0)
				{
					ERR_MSG("write");
					break;
				}

				//回复ACK
				buf[1] = 4;
				if(sendto(sfd, buf, 4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
				{
					ERR_MSG("sendto");
					break;
				}

				//判断数据是否<512 
				if(res-4 < 512)
				{
					printf("文件 %s 下载完毕\n", filename);
					break;
				}

				num++;
			}
		}
		else if(5 == buf[1]) 	//错误包
		{
			printf("ERROR: %d %s\n", ntohs(*(short*)(buf+2)), buf+4);
			break;
		}
	}

	close(fd);
	return 0;
}
int do_upload(int sfd,struct sockaddr_in sin)
{
	//发送上传请求
	char buf[516] = "";
	ssize_t res = 0;
	ssize_t res_r = 512;

	char filename[20] = "";
	printf("请输入要上传的文件名>>>");
	scanf("%s", filename);
	while(getchar()!=10);
	
	//判断文件是否存在
	int fd = open(filename,O_RDONLY);
	if(fd < 0)
	{
		ERR_MSG("open");
		return -1;
	}
	int size = sprintf(buf, "%c%c%s%c%s%c", 0, 2, filename, 0, "octet", 0);
	
	//发送上传请求到69号端口
	if(sendto(sfd,buf,size,0,(struct sockaddr *)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("sendto");
		return -1;
	}

	socklen_t addrlen = sizeof(sin);
	unsigned short num = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));

		//接受数据包
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin, &addrlen);
		if(res < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		
		//分析操作码和块编号
		if(4 == ntohs(*(short *)buf) && num == ntohs((*(short *)(buf+2))))
		{
			num++;
		}
		else if(4 != ntohs(*(short *)buf) && num == ntohs((*(short *)(buf+2))))
		{
			fprintf(stderr,"%d:操作码错误\n",__LINE__);
			break;
		}
		else if(4 == ntohs(*(short *)buf) && num != ntohs((*(short *)(buf+2))))
		{
			fprintf(stderr,"%d:块编号错误\n",__LINE__);
			break;
		}
		else if(4 != ntohs(*(short *)buf) && num != ntohs((*(short *)(buf+2))))
		{
			fprintf(stderr,"%d:操作码、块编号错误\n",__LINE__);
			break;	
		}
		if(res_r < 512)
		{
			printf("上传完成\n");
			break;
		}
		//组数据包,且数据包中的块编号从1开始,数据内容从文件中读取512个字节
		*((short *)buf) = htons(3);
		*((short *)(buf + 2)) = htons(num);
		res_r = read(fd,buf+4,512);
		if(res < 0)
		{
			ERR_MSG("read");
			return -1;
		}
		//发送到服务器
		if(sendto(sfd,buf,res_r+4,0,(struct sockaddr *)&sin,sizeof(sin)) < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		//重新回到循环起始位置,接收服务器回复的ack
	}
	close(fd);
}

终端执行结果:

下载:

 上传:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鱼YY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值