#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#define IP "192.168.31.57"
#define PORT 69
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)int download(char filename[])
{
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("download :socket");
return -1;
}
//无需绑定
//填充服务端地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);
socklen_t addrlen = sizeof(sin);char buf[516] = "";
int size = sprintf(buf,"%c%c%s%c%s%c",0x00,0x01,filename,0,"octet",0);//发送下载请求到服务端
if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("download:sendto");
return -1;
}//发送成功后打开文件夹
int fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC, 0666);
if(fd <0)
{
ERR_MSG("open");
return -1;
}char mode_code[128] = "";
ssize_t recv_len;
while(1)
{
//接收客户端传来的数据包
bzero(buf,sizeof(buf));
recv_len = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen) ;
if(recv_len< 0)
{
ERR_MSG("download: recvfrom");
return -1;
}
printf("recv_len : %ld\n",recv_len);
//判断操作码是否是ERR
if(buf[1] == 0x05)
{
printf("接收错误,下载文件失败\n");
return -1;
}
if(buf[1] == 0x03)
{
printf("正在写入文件...\n");
//写入文件
char *p = buf+4;
if(write(fd,p,recv_len -4) < 0)
{
ERR_MSG("write");
return -1;
}
}
//发送应答包
int size = sprintf(mode_code,"%c%c%c%c",0x00,0x04,buf[2],buf[3]);
printf("%d\n",sin.sin_port);
if(sendto(sfd,mode_code,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
if(recv_len-4 < 512)
{
printf("下载成功\n");
return -1;
}
}
close(fd);
close(sfd);
}int upload(char filename[])
{char buf[516] = "";
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}//无需绑定,填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);socklen_t addrlen = sizeof(sin);
//打开需要上传的文件
int fd = open(filename,O_RDONLY);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}//发送上传的指令
int size = sprintf(buf,"%c%c%s%c%s%c",0x00,0x02,filename,0,"octet",0);
if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送上传请求\n");
ssize_t recv_size = 0;
while(1)
{
//接收应答包
bzero(buf,sizeof(buf));
recv_size = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
if(size < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(buf[1] == 0x05)
{
printf("发生错误,上传文件失败\n");
return -1;
}
if(buf[1] == 0x04)
{
buf[1] = 0x03;
short *p =(short *)(buf+2);
*p = htons(ntohs(*p)+1);//读取文件
size = read(fd,buf+4,sizeof(buf)-4);
if(size < 0)
{
ERR_MSG("read");
return -1;
}
if(size == 0)
{
printf("传输完毕\n");
return 0;
}
if(sendto(sfd,buf,size +4,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
}
}
}
int main (int argc, const char *argv[])
{
printf("*********请输入您要执行的操作\n");
printf("*********1:下载\n");
printf("*********2:上传\n");
int flag = 0;
scanf("%d",&flag);
while(flag != 1 && flag != 2)
{
getchar();
printf("无法识别你输入的操作码,请重新输入\n");
scanf("%d",&flag);
}
char filename[20] = "";
if(1 == flag)
{
printf("请输入您要下载的文件名");
scanf("%s",filename);
download(filename);
}
if(2 == flag)
{
printf("请输入您要上传的文件名");
scanf("%s",filename);
upload(filename);
}return 0;
}
tftp上传下载文件---客户端
最新推荐文章于 2024-11-05 17:47:47 发布