当前位置:网站首页>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




边栏推荐
猜你喜欢

正式发布丨VS Code 1.70

互换性测量技术-几何误差

ESP32的环境配置(arduino arduino2.0 VScode platform哪个好用?)

CSAPP Data Lab

UNI-APP_iphone苹果手机底部安全区域

索引的创建、查看、删除

How does MSP430 download programs to the board?(IAR MSPFET CCS)

ES6 advanced string processing new features
![[idea error] Invalid target distribution: 17 solution reference](/img/39/cec5d854b1d941e866c7166b75aa22.png)
[idea error] Invalid target distribution: 17 solution reference

ES进阶 函数功能语法新特性详解
随机推荐
2022-08-10 第六小组 瞒春 学习笔记
Detailed explanation of new features of ES advanced function syntax
音频编解码,利用FAAC来实现AAC编码
Official release丨VS Code 1.70
oracle的基数会影响到查询速度吗?
"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案
DNS分离解析和智能解析
DOM树的遍历-----修改样式,选择元素,创建和删除节点
浮点数在内存中的存储方式
①CAS SSO单点登录框架源码深度分析
Multi-threaded ThreadPoolExecutor
"How to kick a bad habit to read notes?
Ten Advanced Concepts of SQL Development
IDE compilation error: Dangling metacharacter
正式发布丨VS Code 1.70
leetcode: 358. Reorder strings at K distance intervals
作业8.10 TFTP协议 下载功能
基于FPGA状态机的自动售货机功能实现
基于改进YOLOv5轻量化的烟火检测