#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0);
#define SER_IP "192.168.9.91" //服务器IP ifconfig
#define SER_PORT 69 //1024-49151
//下载
int do_download(int cfd,struct sockaddr_in sin)
{
char filename[32]={0}; //定义文件名
char buf[516]={0};
ssize_t res = 0;
printf("请输入文件名>>>");
scanf("%s",filename);
while(getchar()!=10);
unsigned short code = 0;//操作码
unsigned short num = 0;//块编号
//组下载请求协议
/*
short *p1=(short *)buf;
*p1 = htons(1);
char *p2=buf+2;
strcpy(p2,filename);
char *p4=p2+strlen(p2)+1;
strcpy(p4,"octet");
*/
sprintf(buf,"%c%c%s%c%s%c",0,1,filename,0,"octet",0);
// int size=2+strlen(p2)+1 +strlen(p4)+1;
int size = strlen(filename)+strlen("octet")+4;
//发送下载请求协议
if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
int fp=-1;
socklen_t addrlen = sizeof(sin);
int ret =0 ;
unsigned short numlocal = 0; //初始化本地记录的块编号
while(1)
{
bzero(buf,sizeof(buf));
//接收数据包
res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
ret = -1;
break;
}
//解析数据包
code = ntohs(*(unsigned short *)buf); //解析操作码
num=ntohs(*(unsigned short*)(buf+2)); //解析块编号
if(3 == code)
{
//判断数据包中的块编号是否是正确的快编号
//需要与本机记录的块编号进行比较
if(num == numlocal+1)
{
numlocal++;
if(1 == num)
{
fp=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0064);
if(fp < 0)
{
ERR_MSG("open");
return -1;
}
}
//讲接收到的数据另存到filename中
if(write(fp,buf+4,res-4)<0)
{
ERR_MSG("write");
ret = -1;
break;
}
//回复ACK,由于ACK包和数据包前面4个字节只有操作码不一致
//可以直接修改数据包的操作码,然后讲前4个字节回复给服务器即可
buf[1]=4;
if(sendto(cfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
ret = -1;
break;
}
//直到数据小于512个字节,实际获取到的数据包大小为res
//数据大小 = res - 操作码 - 块编号
if(res-4 < 512)
{
printf("%s 文件下载完毕\n",filename);
break;
}
}
}
else if(5 == code )
{
fprintf(stderr,"DOWNLOAD_ERROR:%d : %s\n",ntohs(*(short*)(buf+2)),buf+4);
break;
}
}
close (fp);
return ret;
}
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("cfd = %d\n",cfd);
//填充地址信息结构体
//真实的地址信息结构体根据地址族指定 AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INEF
sin.sin_port =htons(SER_PORT); //端口号,1024-49151
sin.sin_addr.s_addr =inet_addr(SER_IP); //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':
break;
case '3':
goto END;
break;
default:
printf("输入错误,请重新输入\n");
}
while(getchar()!=10);
}
END:
//关闭文件描述符
close(cfd);
return 0;
}
TFTP客户端
最新推荐文章于 2024-01-24 21:16:03 发布