当前位置:网站首页>Homework 8.10 TFTP protocol download function
Homework 8.10 TFTP protocol download function
2022-08-11 03:15:00 【Unknown college student M】
TFTP协议 下载功能
实现代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
//Printing error new macro function
#define ERR_MSG(msg) do{\ fprintf(stderr, " __%d__ ", __LINE__);\ perror(msg);\ }while(0)
int main(int argc, const char *argv[])
{
if(argc < 4)
{
fprintf(stderr, "请输入IP port\n");
return -1;
}
//The port number string that will be obtained,转换成整形
int port = atoi(argv[2]);
//Create a newspaper socket
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//Bind the client's own address information structure ---> not required binding
//填充服务器的IP地址以及端口号 -->Because the client wants to actively send packets to the server
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = inet_addr(argv[1]);
struct sockaddr_in rcv_addrmsg; //Stores where received packets came from
socklen_t addrlen = sizeof(rcv_addrmsg);
char buf[128] = ""; //读写请求
char buf2[1024]= ""; //数据包
char buf3[16]=""; //ACK
char *ptr=buf;
short int* pa =(short int*)ptr;
*pa=htons(1); //Opcode for read and write requests
char *pb=ptr+2; //The filename for read and write requests
strcpy(pb,"5.png");
char *pc =pb+strlen(pb);
char *pd=pc+1;
strcpy(pd,"octet"); //The mode of read and write requests
size_t size=2+strlen(pb)+1+strlen("octet")+1; //Read and write request length
//将数据包发送给服务器,Therefore, the address information structure needs to be filled with server information
if(sendto(sfd, buf,size,0,(struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
//printf("sendto success\n");
short int num=1; //块编号
ssize_t res; //The number of bytes successfully read by the packet
int fd=open(argv[3],O_WRONLY|O_TRUNC|O_CREAT,0777); //打开指定文件
if(fd<0)
{
ERR_MSG("open");
return -1;
}
while(1)
{
bzero(buf2, sizeof(buf2)); // Packet empty
//Receive data packets sent by the server
if((res=recvfrom(sfd, buf2, sizeof(buf2), 0, (struct sockaddr*)&rcv_addrmsg,&addrlen))< 0)
{
ERR_MSG("recvfrom");
return -1;
}
char *ptr2=buf2;
short int *err;
if(ntohs(*(err=(short int*)ptr2))==5) //The opcode is equal to5,ERROR
{
printf("error:%s\n",(char*)(err+2)); //Print error message
return -1;
}
//Opcodes are not5,即为3,是数据包,ptr2Points to the first address of the data packet
write(fd,ptr2+4,res-4); //The data of the packet is written to the file
short int *pa2=(short int*)ptr2; //Get the block number sent by the server
if(ntohs(*(pa2+1))!=num) //Compare if the block numbers are the same,i.e. the order is correct
{
continue;
}
num++;
bzero(buf3, sizeof(buf3)); //清空ACK
char *ptr3=buf3; //ACK首地址
short int* pa3=(short int*)ptr3;
*pa3=htons(4); //ACK操作码
*(pa3+1)=*(pa2+1); //ACK块编号
//将ACK发送给服务器
if(sendto(sfd, buf3,4,0,(struct sockaddr*)&rcv_addrmsg, addrlen) < 0)
{
ERR_MSG("sendto");
return -1;
}
if(res<516) //The data in the packet is less than512字节
{
break; //退出循环
}
}
printf("下载完成\n");
//关闭套接字
close(sfd);
return 0;
}
运行结果
服务器
IP地址:192.168.31.22
端口:69
从服务器下载文件5.png




边栏推荐
- 21 Day Learning Challenge Week 1 Summary
- 按摩椅控制板的开发让按摩椅变得简约智能
- 音视频开发,为什么要学习FFmpeg?应该怎么入手FFmpeg学习?
- OpenCV founder: Open source must not be completely free!
- 论文精度 —— 2017 CVPR《High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis》
- Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案
- 构建程序化交易系统需要注意什么问题?
- Meaning of df and df -lh
- Google search skills - programmer is recommended
- 获取链表长度
猜你喜欢
互换性与测量技术-公差原则与选用方法
[BX]和loop
"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
2022-08-10 第六小组 瞒春 学习笔记
《如何戒掉坏习惯》读书笔记
阿里低代码框架 lowcode-engine 之自定义物料篇
Briefly, talk about the use of @Transactional in the project
Realization of vending machine function based on FPGA state machine
CSAPP Data Lab
基于FPGA状态机的自动售货机功能实现
随机推荐
MSP430如何给板子下载程序?(IAR MSPFET CCS)
增加对 Textbundle 的支持
多线程之ThreadPoolExecutor
Paper Accuracy - 2017 CVPR "High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis"
(CVPR-2017)在身体和潜在部位学习深度上下文感知特征以进行行人重识别
Realization of vending machine function based on FPGA state machine
"Beijing-Taiwan high-speed rail" debuted on Baidu map, can it really be built in 2035?
(Nips-2015)空间变换器网络
Entity to Vo conversion
Google search skills - programmer is recommended
Meaning of df and df -lh
"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
ES6进阶 字符串处理新特性
重庆纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
flink The object probably contains or references non serializable fields.
Window function application of sum and count
高度塌陷问题的解决办法
Some work experience after joining the digital ic design
【Pdf自动生成书签】