思维导图
TFTP数据上传
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<head.h>
#define SER_IP "192.168.125.149"
#define SER_PORT 69
int main(int argc, const char *argv[])
{
int fd=open("./1_armcli.c",O_RDONLY);
if(fd<0)
{
perror("fd");
return -1;
}
//套接字通信
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("sfd");
return -1;
}
//服务器端口,ip
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
socklen_t socklen=sizeof(sin);
//不绑定
//装载读写请求
char buf[516];
short *p1=(short*)buf;
*p1=htons(2); //读请求
char *p2=buf+2;
strcpy(p2,"1_armcli.c");
char *p3=p2+strlen(p2)+1;
strcpy(p3,"octet");
int len = 4+strlen(p2)+strlen(p3);
//发送请求
sendto(sfd,buf,len,0,(struct sockaddr*)&sin,len);
//数据包初始化
bzero(buf,sizeof(buf));
buf[0]=0x00;
buf[1]=0x03;
int num=0;
char ack[4];
while(1)
{
//ack
recvfrom(sfd,ack,sizeof(ack),0,(struct sockaddr*)&sin,&socklen);
//数据包快编号设置
buf[2]=ack[2];
buf[3]=ack[3]+1;
//上传数据
num=read(fd,buf+4,sizeof(buf)-4);
printf("%d\n",num);
sendto(sfd,buf,num+4,0,(struct sockaddr*)&sin,sizeof(sin));
perror("send");
if(num==0)
{
break;
}
}
//关闭文件
close(sfd);
return 0;
}