#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
int shangchuan(int cfd,struct sockaddr_in sin)
{
char name[20] = "";
printf("请输入需要上传的文件名");
scanf("%s",name);
while(getchar() != '\n');
int fd = open(name, O_RDONLY);
if(fd < 0)
{
if(errno == ENOENT)
{
printf(">>>文件不存在,请重新输入<<<\n");
return -2;
}
else
{
fprintf(stderr,"_%d_",__LINE__);
perror("open");
return -1;
}
}
char buf[516] = "";
short *p1 = (short*)buf;
*p1 = htons(2);
char *p2 = buf + 2;
strcpy(p2,name);
char *p4 = p2 + strlen(name)+1;
strcpy(p4,"octet");
int len = 2 + strlen(p2) +1 +strlen(p4)+1;
//int size = sprintf(buf, "%c%c%s%c%s%c", 0, 2, name, 0, "octet", 0);
if(sendto(cfd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("sendto");
return -1;
}
printf("请求成功\n");
unsigned short kuai = 0;
int recv_len;
socklen_t addrlen = sizeof(sin);
while(1)
{
bzero(buf,516);
recv_len = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
if(recv_len < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("recvfrom");
return -1;
}
if(4 == buf[1])
{
if(kuai == ntohs(*(unsigned short*)(buf+2)))
{
buf[1] = 3;
kuai++;
*(unsigned short*)(buf+2) = htons(kuai);
int res = read(fd,buf+4,512);
if(res < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("read");
return -1;
}
else if(0 == res)
{
printf("-----文件上传完毕-----\n");
break;
}
if(sendto(cfd,buf,res+4,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("sendo");
return -1;
}
}
}
else if(5 == buf[1])
{
printf("-----ERROR:%s-----\n", buf+4);
break;
}
}
close(fd);
return 0;
}
int do_download(int cfd,struct sockaddr_in sin)
{
char name[20] = {0};
printf("请输入要下载的文件名");
scanf("%s",name);
while(getchar() != '\n');
char buf[516] = "";
short *p1 = (short*)buf;
*p1 = htons(1);
char *p2 = buf + 2;
strcpy(p2,name);
char *p4 = p2 + strlen(name)+1;
strcpy(p4,"octet");
int len = 2 + strlen(p2) +1 +strlen(p4)+1;
if(sendto(cfd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("sendto");
return -1;
}
printf("请求成功\n");
unsigned short kuai = 1;
ssize_t recv_len;
socklen_t addrlen = sizeof(sin);
int fd = -1;
while(1)
{
bzero(buf,516);
recv_len = recvfrom(cfd,buf,516,0,(struct sockaddr*)&sin,&addrlen);
if (recv_len < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("recvfrom");
return -1;
}
if(3 == buf[1])
{
fd = open(name,O_WRONLY|O_CREAT|O_APPEND,0664);
if(fd < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("open");
return -1;
}
if(htons(kuai) == *(unsigned short*)(buf+2))
{
if(write(fd,buf+4,recv_len-4) < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("write");
return -1;
}
buf[1] = 4;
if(sendto(cfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin))< 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("sendto");
return -1;
}
if(recv_len < 516)
{
printf("下载完成\n");
break;
}
kuai++;
}
}
}
close(fd);
return 0;
}
int main(int argc,char* argv[])
{
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
fprintf(stderr,"_%d_",__LINE__);
perror("socket");
return -1;
}
printf("报式套接字创建成功\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(69);
sin.sin_addr.s_addr = inet_addr("192.168.177.51");
int loop = 1;
int choose = 0;
while(loop)
{
printf("----------------\n");
printf("----1. 下载----\n");
printf("----2. 上传----\n");
printf("----3. 退出----\n");
printf("----------------\n");
printf("请输入 >>> ");
scanf("%d", &choose);
while(getchar() != '\n'); // 循环吸收垃圾字符,直到遇到'\n'结束吸收
switch(choose)
{
case 1:
do_download(cfd,sin);
break;
case 2:
shangchuan(cfd,sin);
break;
case 3:
loop = 0;
break;
default:
printf("输入有误,请重新输入\n");
}
}
close(cfd);
}
tftp下载和上传
最新推荐文章于 2025-06-18 11:22:33 发布