• Linux Linux程序练习十一(网络编程大文件发送UDP版)


    //网络编程发送端--大文件传输(UDP)
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int main(int arg, char * args[])
    {
        if (arg < 3)
        {
            printf("please print two param ! 
    ");
            return -1;
        }
        int port = atoi(args[2]);
        int st = socket(AF_INET, SOCK_DGRAM, 0);
        if (st == -1)
        {
            printf("create socket failed ! error message :%s
    ", strerror(errno));
            return -1;
        }
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(args[1]);
        char buf[1024] = { 0 };
        int num = 0;
        //open the file stream
        FILE *pfr = NULL;
        int index=0;
        pfr = fopen("/home/test/2/1.dat", "r");
        if (pfr == NULL)
        {
            printf("open the file failed ! error message :%s
    ", strerror(errno));
            goto END;
        }
        while ((num = fread(buf, sizeof(char), sizeof(buf), pfr)) > 0)
        {
            if (sendto(st, buf, sizeof(char)*num, 0, (struct sockaddr *) &addr,
                    sizeof(addr)) == -1)
            {
                printf("sendto failed ! error message :%s
    ", strerror(errno));
                break;
            }
            printf("read %d num=%d
    ",index++,num);
        }
        fclose(pfr);
        END: close(st);
        return 0;
    }
    //网络编程接收端--大文件传输(UDP)
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int main(int arg, char *args[])
    {
        if (arg < 2)
        {
            printf("please print one param ! 
    ");
            return -1;
        }
        int port=atoi(args[1]);
        int st = socket(AF_INET, SOCK_DGRAM, 0);
        if (st == -1)
        {
            printf("create socket failed ! error message:%s 
    ",strerror(errno));
            return -1;
        }
        struct sockaddr_in addr;
        memset(&addr,0,sizeof(addr));
        addr.sin_family=AF_INET;
        addr.sin_port=htons(port);
        addr.sin_addr.s_addr=htonl(INADDR_ANY);
        if(bind(st,(struct sockaddr *)&addr,sizeof(addr))==-1)
        {
            printf("bind IP failed ! error message:%s 
    ",strerror(errno));
            goto END;
        }
        struct sockaddr_in client_addr;
        socklen_t client_addrlen=sizeof(client_addr);
        char buf[1024]={0};
        int num=0;
        int index=0;
        //define the file stream
        FILE * pfa=NULL;
        //open the file in append mode
        pfa=fopen("/home/test/3/1.dat","a");
        if(pfa==NULL)
        {
            printf("open the file failed ! error message :%s
    ",strerror(errno));
            goto END;
        }
        while(1)
        {
            memset(&client_addr,0,sizeof(client_addr));
            num=recvfrom(st,buf,sizeof(buf),0,(struct sockaddr *)&client_addr,&client_addrlen);
            if(num==-1)
            {
                printf("recvform failed ! error message :%s
    ",strerror(errno));
                break;
            }
            /*
             就算发送端关闭,recvfrom函数也不会返回0,而是会继续阻塞进程
             */
            /*
            else if(num==0)
            {
                printf("the other side socket is closed !
    ");
                break;
            }
            */
            printf("recv %d num=%d
    ",index++,num);
            fwrite(buf,sizeof(char),num,pfa);
            if(num<1024)
            {
                printf("recv last! 
    ");
                break;
            }
            memset(buf,0,sizeof(buf));
        }
        fclose(pfa);
        END:close(st);
        return 0;
    }
    .SUFFIXES:.c .o
    CC=gcc
    SRCS1=udprecv.c
    SRCS2=udpsend.c
    OBJS1=$(SRCS1:.c=.o)
    OBJS2=$(SRCS2:.c=.o)
    EXEC1=mrecv
    EXEC2=msend
    
    start:$(OBJS1) $(OBJS2)
        $(CC) -o $(EXEC1) $(OBJS1)
        $(CC) -o $(EXEC2) $(OBJS2)
        @echo "-------ok-----------"
    .c.o:
        $(CC) -Wall -g -o $@ -c $<
    clean:
        rm -f $(OBJS1)
        rm -f $(EXEC1)
        rm -f $(OBJS2)
        rm -f $(EXEC2)

    小结:UDP传输协议确实会出现丢包的情况,远没有TCP/IP协议来的安全,根据上图可以看出。
  • 相关阅读:
    第七周作业——用户体验
    第六周作业——Coder和Hacker的区别
    第五周作业——对lua的学习
    产品开发与竞争
    个人项目
    对代码规范的体悟
    第二周作业——浅谈产品同质化应对方法
    第一周作业
    第二次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5887805.html
Copyright © 2020-2023  润新知