使用TFTP协议和UDP/IP模型写一个客户端实现与服务器之间文件的下载与上传

时间:2024-03-21 22:09:08

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<pthread.h>
#include<sys/wait.h>
#include<signal.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<time.h>
#define ERR_MSG(msg) do{\
	fprintf(stderr, "line:%d ", __LINE__);\
	perror(msg);\
}while(0)

#define SER_IP "192.168.116.201"   //服务器IP:w

#define SER_PORT 69

int do_download(int cfd,struct sockaddr_in sin){
	char filename[20] = "";
	char buf[516] = {0};
	printf("请输入要下载的文件名>>> ");
	scanf("%s", filename);
	while(getchar()!=10);
	short* p1 = (short*)buf;
	*p1 = htons(1);
	char* p2 = buf+2;
	strcpy(p2, filename);
	char* p4 = p2+strlen(p2)+1;
	strcpy(p4, "octet");

	int size = 4+strlen(p2)+strlen(p4);
	//发送下载请求
	if(sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("sendto");
		return -1;
	}
	printf("发送成功\n");
	socklen_t addrlen=sizeof(sin);
	ssize_t res=0;
	unsigned short num=1;
	int fd=-1;
	//int fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0664);//打开对应文件

	while(1){
		bzero(buf,sizeof(buf));
		res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
		if(res<0){
			ERR_MSG("recvfrom");
			return -1;
		}
		if(buf[1]==3){//数据包头
			if(htons(num)==*(unsigned short*)(buf+2)){//数据包编号确认
				num++;
				if(fd==-1){
					fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0664);
				}
				printf("数据包编号正确\n");
				int ssize=write(fd,buf+4,res-4);//将数据内容写入文件
				buf[1]=4;
				//*(unsigned short*)(buf+2)=htons(num);
				int sres=sendto(cfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin));//发送应答包
				if(res<516){//退出
					printf("download success\n");
					break;
				}
			}
		}else if(buf[1]==5){
			printf("收到错误包\n");
			fprintf(stderr, "DOWNLOAD_ERROR:%d : %s\n", ntohs(*(short*)(buf+2)), buf+4);
			break;
			continue;
		}

	}
	close(fd);//关闭文件
	return 0;
}

int do_upload(int cfd, struct sockaddr_in sin)
{
	//组包准备发送下载请求
	char buf[516]="";
	char name[20]="";
	printf("请输入要上传的文件名>>> ");
	scanf("%s",name);
	while(getchar()!=10);

	unsigned short *p1=(short*)buf;   //操作码
	*p1=htons(2);
	char *p2=buf+2;          //文件名
	strcpy(p2,name);
	char *p3=p2+strlen(p2); //第一个0
	*p3=0;
	char *p4=p3+1;   //模式
	strcpy(p4,"octet");

	size_t size=2+strlen(p2)+1+strlen(p4)+1; //操作码+文件名+0+模式+0

	//发送上传请求
	if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("sendto");
		return -1;
	}

	//创建下载文件并清空

	socklen_t addrlen = sizeof(sin);
	ssize_t res = 0;
	unsigned short num = 0;  //记录本地的块编号
	int fd=open(name,O_RDONLY);
	if(fd < 0)
	{
		fprintf(stderr,"未找到该文件\n");//原因可能是没有该文件
		return -1;
	}
	//发送上传请求
	while(1)
	{
		
		res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
		if(4==res){//接受应答包
			if(buf[1]==4){
				if(htons(num)==*(short*)(buf+2)){
					num++;
				printf("正确接受应答包%d %d\n",__LINE__,ntohs(*(short*)(buf+2)));
				}
			}else{
				fprintf(stderr,"接受错误\n");
				return -1;
			}
		}
		else if(5 == buf[1])    //错误包
		{
			printf("错误: %d %s\n",ntohs(*(short*)(buf+2)),buf+4);
			close(fd);
			return -1;
		}else if(res<0){
			ERR_MSG("recvfrom");
			return -1;
		}

		//发送数据
		bzero(buf+4,512);
		ssize_t size_r=read(fd,buf+4,512);
		buf[1]=3;
		//使用应答包的块编号
		*(unsigned short*)(buf+2)=htons(num);
		res = sendto(cfd,buf,size_r+4,0,(struct sockaddr*)&sin,addrlen);//发送数据包
		if(res < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		if(size_r<512)break;//文件读取完毕
	}
		
		
		close(fd);
		return 0;
}
int main(int argc, const char *argv[])
{
	int cfd = socket(AF_INET, SOCK_DGRAM, 0); 
	if(cfd < 0)
	{   
		ERR_MSG("socket");
		return -1; 
	}   

	struct sockaddr_in sin;
	sin.sin_family      = AF_INET;              
	sin.sin_port        = htons(SER_PORT);      
	sin.sin_addr.s_addr = inet_addr(SER_IP);    

	char c = 0;
	while(1)
	{   
		system("clear");
		printf("---------------------------\n");
		printf("----------1. 下载----------\n");
		printf("----------2. 上传----------\n");
		printf("----------3. 退出----------\n");
		printf("---------------------------\n");
		c = getchar();
		while(getchar() != 10);

		switch(c)
		{
		case '1':
			do_download(cfd, sin);
			break;
		case '2':
			do_upload(cfd,sin);
			break;
		case '3':
			goto END;
			break;
		default:
			printf("输入错误,请重新输入\n");
		}

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

END:
	close(cfd);
	return 0;
}

效果图:

1下载

客户端

服务器端

下载5.png成果 

2上传

客户端

 

服务器端

上传成功