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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ! \n");
        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\n", 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\n", 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\n", strerror(errno));
            break;
        }
        printf("read %d num=%d\n",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 ! \n");
        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 \n",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 \n",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\n",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\n",strerror(errno));
            break;
        }
        /*
         就算发送端关闭,recvfrom函数也不会返回0,而是会继续阻塞进程
         */
        /*
        else if(num==0)
        {
            printf("the other side socket is closed !\n");
            break;
        }
        */
        printf("recv %d num=%d\n",index++,num);
        fwrite(buf,sizeof(char),num,pfa);
        if(num<1024)
        {
            printf("recv last! \n");
            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 [email protected] -c $<
clean:
    rm -f $(OBJS1)
    rm -f $(EXEC1)
    rm -f $(OBJS2)
    rm -f $(EXEC2)

技术分享

小结:UDP传输协议确实会出现丢包的情况,远没有TCP/IP协议来的安全,根据上图可以看出。

 

以上是关于Linux Linux程序练习十一(网络编程大文件发送UDP版)的主要内容,如果未能解决你的问题,请参考以下文章

Linux(二十一)Shell编程

LINUX REDHAT第十一单元练习题

Linux学习之二十一-shell编程基础

Linux应用开发第十一章PWM编程应用开发

第十一章 网络文件共享服务之samba

Linux 第十一天