Linux network- TFTP download

04_tftp_download.c

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdlib.h>

#define SER_PORT 69 //1024-49151
#define SER_IP "192.168.8.177"  //ifconfig

#define ERR_MSG(msg) do {\
    fprintf(stderr,"line: __%d__ ",__LINE__);\
    perror(msg);\
} while (0)

int main(int argc, char const *argv[])
{
    
    
    //创建报式套接字
    //1.创建宝石套接字   socket
    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_port = htons(SER_PORT);
    sin.sin_addr.s_addr = inet_addr(SER_IP);
    socklen_t addrlen = sizeof(sin);

    


    //发送下载请求给服务器,服务器IP widowsIP   port:69
    char buf[516]={0};
    char ack_buf[4]={0};
    short *p1 = (short *)buf;
    *p1 = htons(1);  //下载
    char *p2 = buf + 2;
    strcpy(p2,"5.png"); //文件名 ==>终端拿 1_udpSer.c
    //strcpy(p2,"1_udpSer.c"); 
    char  *p3 = p2+strlen(p2);
    *p3 = 0;

    char *p4 = p3+1;
    strcpy(p4,"octet");

    char *p5 = p4+strlen(p4);
    *p5 = 0;

    int size = 2+strlen(p2) + 1 + strlen(p4) + 1;
    if(sendto(sfd,buf,size,0,(struct sockaddr *)&sin,sizeof(sin)) < 0)
    {
        ERR_MSG("send to");
        return -1;
    }
    printf("send protocol success\n");

    ssize_t ret = 0;
    int fd = open("5.png",O_WRONLY|O_CREAT|O_TRUNC,0664); //权限不能少
    //int fd = open("1_udpSer.c",O_WRONLY|O_CREAT|O_TRUNC);
    if (fd < 0)
    {
        ERR_MSG("open");
        return -1;
    }
    
    while (1)
    {
        bzero(buf,sizeof(buf));
        //循环接收数据包  recvfrom
        ret = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,&addrlen);
        printf("ret = %ld\n",ret);
        if (ret < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }
        /*
        测试代码
        printf("data package operate code: %d ,block no: %d ",ntohs(*(short *)buf),ntohs(*((short *)buf+1)));
        break;
        */
        //从数据包中提取数据,存储到文件中
        ssize_t ret1 = write(fd,buf+4,ret-4);
        /*
        测试
        for (int i = 0; i < ret-4; i++)
        {
            printf("%c",(buf+4)[i]);
        }
        break;
        */
        printf("ret1 = %ld\n",ret1);
        //回复应答包 sendto
        *(short *)ack_buf = htons(4);
        *((short *)ack_buf+1) = *((short *)buf+1);
        if(sendto(sfd,ack_buf,sizeof(ack_buf),0,(struct sockaddr *)&sin,addrlen) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf("sendto success\n");
        //知道数据大小小于 512字节,结束循环,下载完毕
        if (ret-4 < 512)
        {
            printf("下载完毕\n");
            break;
        }
    }

    printf("file size = %ld\n",lseek(fd,0,SEEK_END));


    return 0;
}

效果图

 服务器处

 客户端处

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值