2023/3/14 TFTP协议
文件的上传下载
1.下载功能完整
2.上传功能,上传大小上限位128kb,待修改
#include "head.h"
#define SER_PORT 69
#define CLI_PORT 5555
#define CLI_IP "192.168.31.59"
#define MAXSIZE 516
//下载文件
int do_download(int cfd,struct sockaddr_in sin);
//上传文件
int do_upload(int cfd,struct sockaddr_in sin);
int main(int argc, const char *argv[])
{
if(argc < 2)
{
fprintf(stderr,"请输入服务器IP\n");
return -1;
}
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd = %d __%d__\n",cfd,__LINE__);
//填充客户器的地址信息结构体
//真实的地址信息结构体根据地址族执行,AF_INET:man 7 ip
struct sockaddr_in cin;
cin.sin_family = AF_INET;//必须填AF_INET
cin.sin_port = htons(CLI_PORT);//客户端的端口号的网络字节序,1024~49151
cin.sin_addr.s_addr= inet_addr(CLI_IP);//IP地址,ifconfig
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//填充服务器自身的地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET
sin.sin_port = htons(SER_PORT);//服务器端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(argv[1]);//服务器端IP地址的网络字节序,ifconfig
int choose = 0;
//发送下载请求
/*
//组协议包
unsigned short int *p1 = buf;
*p1 = htons(1);
char *p2 = buf + 2;
strcpy(p2,"5.png");
char* p3 = p2 + strlen(p2);
*p3 = 0;
char* p4 = p3 +1;
strcpy(p4,"octet");
int size = strlen("5.png") + strlen("octet") + 4;
*/
while(1)
{
printf("==========1.下载文件===========\n");
printf("==========2.上传文件===========\n");
printf("==========3. 退出=============\n");
printf("输入操作:");
scanf("%d",&choose);
while(getchar() != 10);
switch(choose)
{
case 1:
do_download(cfd,sin);
break;
case 2:
do_upload(cfd,sin);
break;
case 3:
//关闭所有文件
close(cfd);
return 0;
default:
printf("输入错误,重新输入\n");
break;
}
}
return 0;
}
int do_download(int cfd,struct sockaddr_in sin)
{
char buf[MAXSIZE] = "";
ssize_t res = 0;
char dest[128] = "";
memset(buf,0,sizeof(buf));
printf("输入想要下载的文件:");
scanf("%s",dest);
int size = sprintf(buf,"%c%c%s%c%s%c",0,1,dest,0,"octet",0);
if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success __%d__\n",__LINE__);
socklen_t addrlen = sizeof(sin);
int fd = -1;
while(1)
{
if(-1 == fd)
{
fd = open(dest,O_RDWR|O_CREAT|O_APPEND,0664);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
}
//接收数据包
memset(buf,0,sizeof(buf));
res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(write(fd,buf+4,res-4) < 0)
{
ERR_MSG("write");
return -1;
}
//返回ACK
buf[1] = 4;
if(sendto(cfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
if(res < 516)
{
fprintf(stdout,"下载完毕\n");
break;
}
}
close(fd);
}
int do_upload(int cfd,struct sockaddr_in sin)
{
char buf[MAXSIZE] = "";
ssize_t res = 0;
char dest[128] = "";
int block = 0;
memset(buf,0,sizeof(buf));
printf("输入想要上传的文件:");
scanf("%s",dest);
while(getchar() != 10);
int size = sprintf(buf,"%c%c%s%c%s%c",0,2,dest,0,"octet",0);
if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success __%d__\n",__LINE__);
socklen_t addrlen = sizeof(sin);
int fd = -1;
while(1)
{
if(-1 == fd)
{
fd = open(dest,O_RDONLY);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
}
block++;
//接收ack
memset(buf,0,sizeof(buf));
res = recvfrom(cfd,buf,4,0,(struct sockaddr*)&sin,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
//发送数据
buf[1] = 3;
buf[3] = block;
res = read(fd,buf+4,sizeof(buf)-4);
if(res < 0)
{
ERR_MSG("read");
return -1;
}
if(sendto(cfd,buf,res+4,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
if(res < 512)
{
printf("上传完毕\n");
break;
}
}
close(fd);
}