Linux Linux program Exercise 11 (Network programming large file send UDP version)

Source: Internet
Author: User
Tags htons

//Network Programming Sender--Large file Transfer (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/inch.h>#include<arpa/inet.h>intMainintArgChar*args[]) {    if(Arg <3) {printf ("Please print the param! \ n"); return-1; }    intPort = atoi (args[2]); intST = socket (af_inet, SOCK_DGRAM,0); if(St = =-1) {printf ("Create socket failed! Error message:%s\n", Strerror (errno)); return-1; }    structsockaddr_in addr; memset (&AMP;ADDR,0,sizeof(addr)); Addr.sin_family=af_inet; Addr.sin_port=htons (port); Addr.sin_addr.s_addr= Inet_addr (args[1]); Charbuf[1024x768] = {0 }; intnum =0; //Open the file streamFILE *PFR =NULL; intindex=0; PFR= fopen ("/home/test/2/1.dat","R"); if(PFR = =NULL) {printf ("Open the file failed! Error message:%s\n", Strerror (errno)); GotoEND; }     while(num = fread (buf,sizeof(Char),sizeof(BUF), PFR) >0)    {        if(SendTo (St, buf,sizeof(Char) *num,0, (structSOCKADDR *) &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;}
//Network programming Receiver--Large file Transfer (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/inch.h>#include<arpa/inet.h>intMainintArgChar*args[]) {    if(Arg <2) {printf ("Please print one param! \ n"); return-1; }    intPort=atoi (args[1]); intST = socket (af_inet, SOCK_DGRAM,0); if(St = =-1) {printf ("Create socket failed! Error message:%s \ n", Strerror (errno)); return-1; }    structsockaddr_in addr; memset (&AMP;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, (structSOCKADDR *) &addr,sizeof(addr)) ==-1) {printf ("bind IP failed! Error message:%s \ n", Strerror (errno)); GotoEND; }    structsockaddr_in client_addr; socklen_t Client_addrlen=sizeof(CLIENT_ADDR); Charbuf[1024x768]={0}; intnum=0; intindex=0; //define the file streamFILE * pfa=NULL; //Open the file in Append modePfa=fopen ("/home/test/3/1.dat","a"); if(pfa==NULL) {printf ("Open the file failed! Error message:%s\n", Strerror (errno)); GotoEND; }     while(1) {memset (&AMP;CLIENT_ADDR,0,sizeof(CLIENT_ADDR)); Num=recvfrom (St,buf,sizeof(BUF),0,(structSOCKADDR *) &client_addr,&Client_addrlen); if(num==-1) {printf ("Recvform failed! Error message:%s\n", Strerror (errno));  Break; }        /*even if the sender is closed, the Recvfrom function will not return 0, but will continue to block the process*/        /*else if (num==0) {printf ("The other side sockets is closed!\n");        Break }        */printf ("recv%d num=%d\n", index++, num); Fwrite (BUF,sizeof(Char), NUM,PFA); if(num<1024x768) {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 [email protected]-C $<Clean:rm-F $ (OBJS1) RM-F $ (EXEC1) RM-F $ (OBJS2) RM-F $ (EXEC2)

Summary: The UDP transport protocol does appear to drop packets, far from the TCP/IP protocol security, as can be seen.

Linux Linux program Exercise 11 (Network programming large file send UDP version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.