首页 > 代码库 > Linux Linux程序练习十一(网络编程大文件发送UDP版)

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 .oCC=gccSRCS1=udprecv.cSRCS2=udpsend.cOBJS1=$(SRCS1:.c=.o)OBJS2=$(SRCS2:.c=.o)EXEC1=mrecvEXEC2=msendstart:$(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协议来的安全,根据上图可以看出。

 

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